This is an easy effort to make Analog Watch using c language. I think this article will help C programmer to get familiarized with BGI (Borland Graphics Interface) for DOS programs.
Description:
Four steps:
1. Initialize graphics
2. Get system time
3. Draw watch with current system time
4. Logical work
Initialize graphics:
‘initgraph’ method of ‘graphics.h’ header file is used to initializes graphics. Need to set three parameter of this function:
1. Graphics driver
2. Graphics Mode
3. Path of driver
In this project I initialize the graphics in following format
int d=DETECT,m; // variable used for graphics initialization
initgraph(&d,&m,""); //this method initialize graphics
If we write above two lines, this will automatically detect graphics and it become ready to draw. We need to paste *.bgi files to the directory of watch.exe file for detecting graphics driver path. We can set the path of bgi folder under double quote (like initgraph(&d,&m,"e:\\tc3\\bgi"); ).
Get system time:
We need to get and set current time of our system to start our watch.exe. For this we need to execute following code:
void save_biostime()
{
struct time t;
gettime(&t);
hour=t.ti_hour; //hour minute second private variable
minute= t.ti_min;
second= t.ti_sec;
}
Draw watch with current system time:
We simply use some bgi graphics function to draw line, circle and text. Following function is used for drawing watch:
void draw_watch()
{
int c=1;
setcolor(c);
circle(320,250,170);
setfillstyle(1,c);
floodfill(485,250,c);
setcolor(c);
circle(280,250,170); // draw circle
setfillstyle(1,c); //set fill style
floodfill(115,250,c); // filling the circle by desired color
setcolor(1);
circle(300,250,130);
circle(300,250,120);
setfillstyle(1,1);
floodfill(300,121,1);
setcolor(8);
circle(300,250,160);
circle(300,250,170);
setfillstyle(1,8);
floodfill(300,88,8);
setcolor(5);
circle(300,250,161);
circle(300,250,131);
setfillstyle(2,5);
floodfill(300,111,5);
setcolor(9);
circle(300,250,119);
setfillstyle(1,9);
floodfill(300,132,9);
putpixel(300,250,14);
setcolor(0);
outtextxy(292,134,"12");
outtextxy(410,247,"3");
outtextxy(394,191,"2");
outtextxy(353,151,"1");
outtextxy(298,360,"6");
outtextxy(353,344,"5");
outtextxy(394,304,"4");
outtextxy(184,248,"9");
outtextxy(200,302,"8");
outtextxy(240,344,"7");
outtextxy(236,152,"11");
outtextxy(196,192,"10");
}
Logical work:
We know an analog watch is comparable with a circle. We need to imagine three circle for second, minute and hour pins.
Here x = r * cos (t)
y= r * sin (t)
t is the angle between horizontal axis and current line. x is the horizontal component and y is the vertical component of radius r at an angle t. Thus we will convert Polar coordinate to Cartesian coordinate which helps us to draw each second movement of second pin.
We know each 1 sec = 6 degree movement for second pin. So we will iterate our loop by 6 degree.
We also have to convert degree to radian for ‘math.h’ functions.
Code for draw pin:
void draw_pin(double t1,double t2,double t3){
double c,d,e,f;
int p=1;
while(p)
for(int i=t3,a,b;i<=624;i+=6) { if(kbhit()!=0) {p=0; break; } a=100*cos(M_PI*i/180); //for second pin & pin length 100 b=100*sin(M_PI*i/180); //for second pin & pin length 100 c=90*cos(M_PI*t1/180); //for minute pin & pin length 90 d=90*sin(M_PI*t1/180); //for minute pin & pin length 90 e=80*cos(M_PI*t2/180); //for hour pin & pin length 80 f=80*sin(M_PI*t2/180); //for hour pin & pin length 80 setlinestyle(0,0,0); setcolor(14); line(300,250,300+a,250+b); setcolor(1); setlinestyle(0,0,3); line(300,250,300+c,250+d); setcolor(4); line(300,250,300+e,250+f); setlinestyle(0,0,0); delay(1000); setcolor(9); line(300,250,300+a,250+b); setlinestyle(0,0,3); line(300,250,300+c,250+d); line(300,250,300+e,250+f); t1+=0.1; t2+=0.008333333; t3=270; } } Lets try it…… CODE: /**********************************/ // Necessary Header file/***********************************/ #include
#include
#include
#include
#include
#include
#include
double minute,hour,tmp,second; // gloabal variable for time-component
void draw_watch()
{ int c=1;
setcolor(c);
circle(320,250,170);
setfillstyle(1,c);
floodfill(485,250,c);
setcolor(c);
circle(280,250,170);
setfillstyle(1,c);
floodfill(115,250,c);
setcolor(1);
circle(300,250,130);
circle(300,250,120);
setfillstyle(1,1);
floodfill(300,121,1);
setcolor(8);
circle(300,250,160);
circle(300,250,170);
setfillstyle(1,8);
floodfill(300,88,8);
setcolor(5);
circle(300,250,161);
circle(300,250,131);
setfillstyle(2,5);
floodfill(300,111,5);
setcolor(9);
circle(300,250,119);
setfillstyle(1,9);
floodfill(300,132,9);
putpixel(300,250,14);
setcolor(0);
outtextxy(292,134,"12");
outtextxy(410,247,"3");
outtextxy(394,191,"2");
outtextxy(353,151,"1");
outtextxy(298,360,"6");
outtextxy(353,344,"5");
outtextxy(394,304,"4");
outtextxy(184,248,"9");
outtextxy(200,302,"8");
outtextxy(240,344,"7");
outtextxy(236,152,"11");
outtextxy(196,192,"10");
}
void draw_pin(double t1,double t2,double t3){
double c,d,e,f; int p=1;
while(p)
for(int i=t3,a,b;i<=624;i+=6) {
if(kbhit()!=0)
{p=0; break; }
a=100*cos(M_PI*i/180); //for second pin & pin length 100
b=100*sin(M_PI*i/180); //for second pin & pin length 100
c=90*cos(M_PI*t1/180); //for minute pin & pin length 90
d=90*sin(M_PI*t1/180); //for minute pin & pin length 90
e=80*cos(M_PI*t2/180); //for hour pin & pin length 80
f=80*sin(M_PI*t2/180); //for hour pin & pin length 80
setlinestyle(0,0,0);
setcolor(14);
line(300,250,300+a,250+b);
setcolor(1);
setlinestyle(0,0,3);
line(300,250,300+c,250+d);
setcolor(4);
line(300,250,300+e,250+f);
setlinestyle(0,0,0);
delay(1000);
setcolor(9);
line(300,250,300+a,250+b);
setlinestyle(0,0,3);
line(300,250,300+c,250+d);
line(300,250,300+e,250+f);
t1+=0.1;
t2+=0.008333333;
t3=270;
}
}
void save_biostime()
{ struct time t;
gettime(&t);
hour=t.ti_hour; //hour minute second private variable
minute= t.ti_min;
second= t.ti_sec;
}
int main(){
int d=DETECT,m; // variable used for graphics initialization
initgraph(&d,&m,""); //this method initialize graphics
draw_watch(); // method to draw watch
save_biostime(); // method to get & save bios time
tmp=second/60; //converting second to minute
minute=minute+tmp; //setting value for minute pin
tmp=minute/60; //converting minute to hour
hour=hour+tmp; //setting value for hour pin
draw_pin(270+minute*6,270+hour*30,270+second*6); // drawing watch withcurrentbiostime
return 0;}