clock.cpp

screenshot
This program will run in Borland Turbo C++ 3.0 (it uses BGI) along with the files:
ginit.cpp and gutility.cpp
//************************************************
//*clock.cpp                                     *
//*C/Turbo C++ 3.0                               *
//*Program is a animation of a analog clock,     *
//*whose hands reflect the current system time.  *
//*                                              *
//************************************************

#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <ctype.h>
#include <string.h>
#include <dos.h>
#include <math.h>
#include <graphics.h>
#include <time.h>

//references files on floppy disk (a:)
#include "a:gutility.cpp"              //Graphics utilities
#include "a:ginit.cpp"                 //Graphic initializers

                           //Function Prototypes
long double roundld(long double);      //function rounds long doubles

double xfigure(double,double,double,int,int);
//^^function figures handlength and ending x

double yfigure(double,double,double,int,int, double);
//^^function figures handlength and ending y

main()
{

opengraph();

int hr,sec,min;

struct time t,t2;

int xcenter,ycenter;                   //variable declarations
int startx,starty;
double lstartx,lstarty;
int xdist,ydist,endx,endy,ticklength;
int numdist,numx,numy,i;
double handlength,handvalue;
double radians;
char numstring[3];
int aspectx,aspecty;
double xyratio;

//numbers in the array the way they will be drawn
int cnumber[]={3,9,9,3,2,10,8,4,1,11,7,5,12,12,6,6};

int page=0;                            //page set to zero

                    //CONTINUES TO DRAW THE CLOCK ON THE OTHER PAGE
while (!kbhit())
{
  setvisualpage(page);
  setactivepage(1-page);

cleardevice();                         //clears the screen

int cnum=0;

int radius = getmaxx()/4;

lstartx=lstarty=0;                     //set variables to zero
xdist=ydist=endy=endx=ticklength=0;
numdist=numx=numy=i=0;
radians=startx=starty=0;
handvalue=handlength=0;
aspectx=aspecty=0;
xyratio=0;

setlinestyle(SOLID_LINE,SOLID_FILL,THICK_WIDTH);
setcolor(WHITE);                       //makes sure color is white
circle(xcenter,ycenter,radius);        //draws circle

gettime(&t);
hr  = t.ti_hour;                       //gets time elements
min = t.ti_min;
sec = t.ti_sec;

if (hr>12)
   hr -=12;

getaspectratio(&aspectx, &aspecty);
xyratio = double (aspectx)/(aspecty);


//TICK DRAWING AND NUMBER LOOP
for (int count=0; count<=90; count+=6)//tick at every 6 degrees
{
radians=(count/180.00)*3.141593;       //converting degrees to radians
lstartx= cos(radians)*radius + xcenter;//figures x on circle
lstarty=( ycenter - (sin(radians)*radius) *xyratio);//figures y on circle

startx=((int)roundld(lstartx));//round CALLS ROUNDLD FUNCTION
starty=((int)roundld(lstarty));//round CALLS ROUNDLD FUNCTION
     //FIGURES WHETHER A BIG TICK & A NUMBER OR A SMALL TICK
     if (count ==0 || count ==30 || count==60 || count==90)//if big tick
           {setlinestyle(SOLID_LINE,SOLID_FILL,THICK_WIDTH);
            ticklength=100/10;           //10%
            numdist=100/20;              //20% for clock numbers
            numx=startx-(startx-xcenter)/numdist;
            numy=starty+(ycenter-starty)/numdist;

            settextjustify(CENTER_TEXT,CENTER_TEXT);
            settextstyle(DEFAULT_FONT,HORIZ_DIR,2);//magnif. factor=2

            cnum=cnumber[i];
            itoa(cnum,numstring,10);//converts integer toa string
            outtextxy(numx,numy,numstring);//draws number in Q1

            numx=xcenter-(numx-xcenter);      //new numx
            cnum=cnumber[i+=1];
            itoa(cnum,numstring,10);//converts integer toa string
            outtextxy(numx,numy,numstring);//draws number in Q2

            numy=ycenter+(ycenter-numy);      //new numy
            cnum=cnumber[i+=1];
            itoa(cnum,numstring,10);//converts integer toa string
            outtextxy(numx,numy,numstring);//draws number in Q3

            numx=xcenter+(xcenter-numx);      //new (old)numx
            cnum=cnumber[i+=1];
            itoa(cnum,numstring,10);//converts integer toa string
            outtextxy(numx,numy,numstring);//draws number in Q4

            i+=1;//increments array for Q1
           }
      else
           {setlinestyle(SOLID_LINE,SOLID_FILL,NORM_WIDTH);
           ticklength =100/5;    //5%
           }//END LOOP

xcenter = getmaxx()/2;
ycenter = getmaxy()/2;
                           //DRAWING TICKS

endx=startx-(startx-xcenter)/ticklength;
endy=starty+(ycenter-starty)/ticklength;

line (startx,starty,endx,endy);        //draws in Quadrant1
xdist=startx-xcenter;                //gets distance from center
startx=xcenter-xdist;                //new startx
endx=xcenter-(endx-xcenter);         //new endx

line (startx,starty,endx,endy);        //draws in Quadrant2
ydist=ycenter-starty;                //gets distance from center
starty=ycenter+ydist;                //new starty
endy=ycenter+(ycenter-endy);         //new endy

line (startx,starty,endx,endy);        //draws in Quadrant3
startx=xcenter+xdist;                //new (old)startx
endx=xcenter+(xcenter-endx);         //new (old)starty

line (startx,starty,endx,endy);        //draws in Quadrant4
}//END OF TICK DRAWING AND NUMBER LOOP


//DRAWING HANDS

handlength = 100/75.00;//75%           SECONDS
handvalue = (sec*6)-90;
radians=(handvalue/180.00)*3.141593;   //converting degrees to radians
endx = ((int) xfigure(radians,handvalue,handlength,xcenter,radius) );
endy = ((int) yfigure(radians,handvalue,handlength,ycenter,radius,xyratio) );
setlinestyle(SOLID_LINE,SOLID_FILL,NORM_WIDTH);
setcolor(LIGHTRED);
line (xcenter,ycenter,endx,endy);

handlength = 100/60.00;//60%           MINUTES
handvalue = (min*6)-90;
radians=(handvalue/180.00)*3.141593;   //converting degrees to radians
endx = ((int) xfigure(radians,handvalue,handlength,xcenter,radius) );
endy = ((int) yfigure(radians,handvalue,handlength,ycenter,radius,xyratio) );
setlinestyle(SOLID_LINE,SOLID_FILL,THICK_WIDTH);
setcolor(MAGENTA);
line (xcenter,ycenter,endx,endy);

handlength = 100/45.00;//45%         HOURS
handvalue = (hr*30)+(min*.5)-90;//90 degrees plus hr*every 30 degrees
                      //^so hourhand moves constantly
radians=(handvalue/180.00)*3.141593;   //converting degrees to radians
endx = ((int) xfigure(radians,handvalue,handlength,xcenter,radius) );
endy = ((int) yfigure(radians,handvalue,handlength,ycenter,radius,xyratio) );
//linestyle the same
setcolor(CYAN);
line (xcenter,ycenter,endx,endy);


if (page==1)                    //CHANGES THE PAGE
   page=0;
else
   page=1;

}//END OF WHILE WITH THE KBHIT

closegraph();
return 0;

}

long double roundld( long double number)
{//THIS ROUNDS THE NUMBER
     long double testnum=0;
     int testnumi=0,nint=0;
               //testnum used to figure whether to round up or not
          testnum = ( (number*10) );
     testnumi=testnum;

     if ( (testnumi%10) >= 5 )
        nint = ceil(number);        //rounds up
     else
        nint = floor(number);       //rounds down

return nint;
}

double xfigure(double rad, double value, double length, int center, int r)
{    double end =0;
     int iend=0;
     end = ( (r*cos(rad))/length )+center;
     iend=( (int)roundld(end) );//calls function roundld
     return iend;
}

double yfigure(double rad, double value, double length, int center, int r, double aspect)
{    double end =0;
     int iend=0;
               //USING ASPECTRATIO
     if (value >= -90 || value <=90)//value in Quadrant2 or 1 (y is pos)
        {
        end = ( ((r*sin(rad)) )/length )*aspect+center;
        }
     else
        {
        end = ( ( (-1)* (r*sin(rad)) )/length )*aspect+center;
        }
     iend=( (int)roundld(end) );//calls function roundld

     return iend;
}