A Clock developped in C
Below is the program in C to build a beautiful clock synchronized with current time of your system. This program uses some basic understanding of C and graphics in C. You can design such animations or graphic design very easily. I would like to make you understand this directly through this program.
Download the source code here.
If you like this article, please | about below topic |
#include<stdio.h>#include<conio.h>#include<graphics.h>#include<math.h>#include<TIME.h>#include<dos.h>/*Above are the inclusion of header files for this program. Header files contains functions and definitions which will be used below. For Eg, to use gettime funtion, we need to include <TIME.h>
*/void main(){int gd=DETECT,gm;struct time t;//t is structure of type time.//members:- ti_min,ti_hour,ti_secdouble x1,y1,x2,y2,x3,y3,r=220.0,r1=160.0,r2=130.0,pi=3.1416,i,j,k;double cx,cy,secangle,minangle,hrangle; //co-ordinates of centerint a[8]={0,0,0,600,600,600,600,0};float min,hr,sec;//variable declarations and initializationsinitgraph(&gd,&gd,"");// initializes the graphic mode. If not used, it gives error graphics not initialized.cx=getmaxx()/2; //(cx,cy) is the co-ordinate of centercy=getmaxy()/2;//getmaxx() will get the maximum value of pixel in X-direction.//getmaxy() will get the maximum value of pixel in Y-directiongettime(&t);//This function gets current system time ans stores into the structure time t(declared above)min=t.ti_min;hr=t.ti_hour;sec=t.ti_sec;//storing current hrs, mins, seconds into other variables (just for access with ease)//calculate the displacement of all hands of a clock measured in angle.//angle increment for second hand movement=pi/30//angle increment for minute hand movement=pi/1800//angle increment for hour hand movement=pi/21600//calculate curent angle for second handsecangle=90-(sec*6);if (secangle <0)secangle=360+secangle;secangle=(secangle*pi)/180;//conversion from degree to radian as sin cos in C accepts arguments in radianminangle=90-(min*6); //calculate curent angle for minute handif (minangle <0)minangle=360+minangle;minangle=(minangle*pi)/180; //conversion from degree to radian//same for hour handhr=hr+(min/60.0); //adding minutes also, to the hour value.if (hr>12)hr-=12; // gettime returns hours in 24 hour formate, so subtract by 12.hrangle=90-(hr*30);if (hrangle<0)hrangle=360+hrangle;hrangle=(hrangle*pi)/180;for(i=secangle,j=minangle,k=hrangle;i>0,j>0,k>0;k=k-pi/21600,i=i-pi/30,j=j-pi/1800){/*setting initial position(angles) of all hands. i.e setting i,j,k with secangle, minangle and hrangle respectively . Note how the angle incremet is set in each second. For second hand the angle will change by pi/30 in one second and so on..*/delay(1000); //delay for 1 seconds and below code updates the position of clock's handscleardevice(); //erases all and clears the graphic screensetcolor(13); // sets the current drawing color using the value (here 13).setlinestyle(0,1,3);fillpoly(4,a); //draws an fills a polygon with the current style and drawing colorcircle(cx,cy,r+10);circle(cx,cy,r);// gotoxy(300,100);// printf("34");setcolor(2);line(cx,cy-r-10,cx,cy-r+10); //extra lines toline(cx,cy+r-10,cx,cy+r+10); // show 12,3,6,9line(cx-r-10,cy,cx-r+10,cy);line(cx+r-10,cy,cx+r+10,cy);setcolor(9);line(cx,cy,cx+r*cos(i),cy-r*sin(i)); //second handsetcolor(5);line(cx,cy,cx+r1*cos(j),cy-r1*sin(j)); //minute handsetcolor(6);line(cx,cy,cx+r2*cos(k),cy-r2*sin(k)); //hour handif(kbhit()!=0) //checks if a key is pressedbreak; // if a key is pressed, exits...}//end of kbhit();getch();}