Saturday, February 05, 2011

Analog Clock



/*
  * THIS PROGRAM DRAWS A REAL CLOCK WITH HOURS, MINUTES & SECONDS HANDS ROTATING AROUND TO  COUNT EXACT TIME (SYSTEM TIME)
  */
    
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<stdlib.h>
#include<math.h>

void drawclk();
void showtime(int, int, int, int, int, int);

void main(){
  int   driver = DETECT,mode,hh,mm,ss;
  int xcenter,ycenter;
  double xs,ys,xm,ym,xh,yh;
  double xs_old,ys_old,xm_old,ym_old,xh_old,yh_old;
  struct time t;
 
  initgraph(&driver,&mode,"c:\\tc\\bgi");

            gettime(&t);
            hh=t.ti_hour;
            mm=t.ti_min;
            ss=t.ti_sec;
            if(hh>12)
                        hh=hh-12;

  cleardevice();
 
  //Other end point of second hand
  xs=(90*cos(M_PI_2-ss*M_PI/30));
  ys=(90*sin(M_PI_2-ss*M_PI/30));
  //Other end point of minute hand
  xm=(70*cos(M_PI_2-mm*M_PI/30));
  ym=(70*sin(M_PI_2-mm*M_PI/30));
  //Other end point of hour hand
  xh=(40*cos(M_PI_2-hh*M_PI/6));
  yh=(40*sin(M_PI_2-hh*M_PI/6));

  //Transformation matrix of second hand's end point
  double Ts[2][2]={1,sin(-M_PI/30),-sin(-M_PI/30),1-sin((M_PI/30)*(M_PI/30))};
  //Transformation matrix of minute hand's end point
  double Tm[2][2]={1,sin(-M_PI/30/60),-sin(-M_PI/30/60),1-sin((M_PI/30/60)*(M_PI/30/60))};
  //Transformation matrix of second hand's end point
  double Th[2][2]={1,sin(-M_PI/30/60/12),-sin(-M_PI/30/60/12),1-sin((M_PI/30/60/12)*(M_PI/30/60/12))};




  //Drawing the clock with initial time
      showtime(xs,ys,xm,ym,xh,yh);

  int c;
  for(;;)
  {
   cleardevice();

   xs_old = xs;    ys_old = ys;
  xm_old = xm; ym_old = ym;
  xh_old = xh;   yh_old = yh;

    //Transforming end point of second's hand
    xs=xs_old*Ts[0][0]+ys_old*Ts[1][0];
    ys=xs_old*Ts[0][1]+ys_old*Ts[1][1];
    //Transforming end point of minute's hand
    xm=xm_old*Tm[0][0]+ym_old*Tm[1][0];
    ym=xm_old*Tm[0][1]+ym_old*Tm[1][1];
    //Transforming end point of hour's hand
    xh=xh_old*Th[0][0]+yh_old*Th[1][0];
    yh=xh_old*Th[0][1]+yh_old*Th[1][1];

    //Drawing the clock again
    showtime(xs,ys,xm,ym,xh,yh);
           
            if(kbhit()){
    c = getch();
     if(c == 'q')               /* ‘q’ -> to quit */
            exit(1);
     }
  }

  closegraph();
}

void drawclk()
{
            int i,j,X,Y,R=150,r=3;
            float t;

            X=getmaxx()/2;
            Y=getmaxy()/2;

            setcolor(YELLOW);
            fillellipse(X,Y,R,R);
            setcolor(MAGENTA);
            outtextxy(X-10,Y-80,"TITAN");
            setcolor(RED);

            circle(X,Y,R);
            circle(X,Y,r);

            for(i=0;i<360;i+=30)
            {
                        t=3.14/180*i;
                        moveto(X+(R-5)*cos(t),Y-(R-5)*sin(t));
                        lineto(X+(R-15)*cos(t),Y-(R-15)*sin(t));
            }
            for(i=0;i<360;i+=6){
                        t=3.14/180*i;
                        putpixel(X+(R-10)*cos(t),Y-(R-10)*sin(t),RED);
            }
}

void showtime(int xs, int ys,int xm, int ym, int xh, int yh)
{
 int xcenter = getmaxx()/2;
 int ycenter = getmaxy()/2;
  drawclk();
  setlinestyle(SOLID_LINE,1,1);
  setcolor(CYAN);
  line(xcenter,ycenter,xcenter+(int)xs,ycenter-(int)ys);  // drawing second’s hand
  setlinestyle(SOLID_LINE,1,1);
  setcolor(GREEN);
  line(xcenter,ycenter,xcenter+(int)xm,ycenter-(int)ym);          // drawing minute’s hand
  setlinestyle(SOLID_LINE,1,1);
  setcolor(DARKGRAY);
  line(xcenter,ycenter,xcenter+(int)xh,ycenter-(int)yh);             // drawing hour’s hand
  delay(1000);
}

No comments:

Post a Comment

Do you think this information useful or was not up to the mark? Comment if you have any advices or suggestions about the post.