Analog Clock in C

Luckychap 0 Tallied Votes 2K Views Share

Here is simple C program for analog clock.
Comipler: TurboC

/* 
 * Developed by: Vivek Kumar
 * Email:        vivek_kumar_bt@yahoo.co.in
 * Discription:  A simple Analog Clock 
 *
 */

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>
#include<math.h>
#include<dos.h>
#include<time.h>
void sline(int,int,int,int,int,int,float,int,int);
void hline(int,int,int,int,int,int,float,int,int);
void mline(int,int,int,int,int,int,float,int,int);
void grid(int,int,int,int);
int main()
{
 int gd=DETECT,gm,maxx,maxy,midx,midy;
 int sincolor=4,soutcolor=0,hincolor=1,houtcolor=0,mincolor=13,moutcolor=0;
 int bakcolor=14;
 int gcolor=0,grad=3;

 float thita,X,Y,h,s,m;
 initgraph(&gd,&gm,"c:\\tc\\bgi");

 maxx=getmaxx();
 maxy=getmaxy();
 midx=maxx/2;
 midy=maxy/2;
 float phi=3.1423,degree,dh,dm;
 int rad=125;
 int HX,HY,MX,MY,SX,SY,ML=rad-25,SL=rad-20,HL=rad-55;
 char str[10];
 cleardevice();
  setfillstyle(1,bakcolor);
 circle(midx,midy,rad-10);
 floodfill(midx,midy,15);
  setfillstyle(1,1);
 circle(midx,midy,rad+10);
 floodfill(midx+rad+5,midy,15);
 for(thita=270+45;thita<=360+135;thita=thita+(180/60))
 {
   degree=((2*phi)/180)*thita;
   X=midx+rad*(cos(degree));
   Y=midy+rad*(sin(degree));
   itoa(degree,str,10);
   setfillstyle(1,4);
   circle(X,Y,1);
   floodfill(X,Y,15);
 }
  for(thita=270+45;thita<=360+135;thita=thita+(180/12))
  {
   degree=((2*phi)/180)*thita;
   X=midx+rad*(cos(degree));
   Y=midy+rad*(sin(degree));
   setfillstyle(1,4);
   circle(X,Y,3);
   floodfill(X,Y,15);
  }
  struct time t;
  gettime(&t);
   s=270+45+t.ti_sec*(180/60);
   h=270+45+t.ti_hour*(180/12)+((t.ti_min*(180/60)))/12;
   m=270+45+t.ti_min*(180/60)+((t.ti_sec*(180/60))/60);
   degree=((2*phi)/180)*s;
   dh=((2*phi)/180)*h;
   dm=((2*phi)/180)*m;
   SX=midx+SL*(cos(degree));
   SY=midy+SL*(sin(degree));
   HX=midx+HL*(cos(dh));
   HY=midy+HL*(sin(dh));
   MX=midx+ML*(cos(dm));
   MY=midy+ML*(sin(dm));
  while(!kbhit())
  {
   grid(midx,midy,grad,gcolor);
   sline(midx,midy,SX,SY,bakcolor,bakcolor,degree,rad,SL);
   circle(SX,SY,2);
   hline(midx,midy,HX,HY,bakcolor,bakcolor,dh,rad,HL);
   mline(midx,midy,MX,MY,bakcolor,bakcolor,dm,rad,ML);
   gettime(&t);
   if(t.ti_sec==0)
   {
    gotoxy(44,4);
    printf("0 ");
   }
   gotoxy(35,4);
   printf("%d : %d : %d",t.ti_hour,t.ti_min,t.ti_sec);
   s=270+45+t.ti_sec*(180/60);
   h=270+45+t.ti_hour*(180/12)+((t.ti_min*(180/60)))/12;
   m=270+45+t.ti_min*(180/60)+((t.ti_sec*(180/60))/60);
   degree=((2*phi)/180)*s;
   dh=((2*phi)/180)*h;
   dm=((2*phi)/180)*m;
   SX=midx+SL*(cos(degree));
   SY=midy+SL*(sin(degree));
   HX=midx+HL*(cos(dh));
   HY=midy+HL*(sin(dh));
   MX=midx+ML*(cos(dm));
   MY=midy+ML*(sin(dm));

   sline(midx,midy,SX,SY,sincolor,soutcolor,degree,rad,SL);
   circle(SX,SY,2);
   hline(midx,midy,HX,HY,hincolor,houtcolor,dh,rad,HL);
   mline(midx,midy,MX,MY,mincolor,moutcolor,dm,rad,ML);
   delay(1000);
  }
 getch();
 return 0;
}

void sline(int midx,int midy,int SX,int SY,int incolor,int outcolor,float degree,int rad,int SL)
{
 int lx,ly,rx,ry,mx,my;
 float phi=3.1423;
 float width=0.04;
 lx=midx+(SL-10)*(cos(degree+width));
 ly=midy+(SL-10)*(sin(degree+width));
 rx=midx+(SL-10)*(cos(degree-width));
 ry=midy+(SL-10)*(sin(degree-width));
 mx=midx+(rad/2)*(cos(degree));
 my=midy+(rad/2)*(sin(degree));
 setcolor(outcolor);
 line(midx,midy,lx,ly);
 line(midx,midy,rx,ry);
 line(lx,ly,SX,SY);
 line(rx,ry,SX,SY);
 setfillstyle(1,incolor);
 floodfill(mx,my,outcolor);
}
void mline(int midx,int midy,int SX,int SY,int incolor,int outcolor,float degree,int rad,int ML)
{
 int lx,ly,rx,ry,mx,my;
 float phi=3.1423;
 float width=0.07;
 lx=midx+(ML-20)*(cos(degree+width));
 ly=midy+(ML-20)*(sin(degree+width));
 rx=midx+(ML-20)*(cos(degree-width));
 ry=midy+(ML-20)*(sin(degree-width));
 mx=midx+(rad/2)*(cos(degree));
 my=midy+(rad/2)*(sin(degree));
 setcolor(outcolor);
 line(midx,midy,lx,ly);
 line(midx,midy,rx,ry);
 line(lx,ly,SX,SY);
 line(rx,ry,SX,SY);
 setfillstyle(1,incolor);
 floodfill(mx,my,outcolor);
 line(midx,midy,SX,SY);
}
void hline(int midx,int midy,int SX,int SY,int incolor,int outcolor,float degree,int rad,int HL)
{
 int lx,ly,rx,ry,mx,my;
 float phi=3.1423;
 float width=0.155;
 lx=midx+(HL-30)*(cos(degree+width));
 ly=midy+(HL-30)*(sin(degree+width));
 rx=midx+(HL-30)*(cos(degree-width));
 ry=midy+(HL-30)*(sin(degree-width));
 mx=midx+(rad/2)*(cos(degree));
 my=midy+(rad/2)*(sin(degree));
 setcolor(outcolor);
 line(midx,midy,lx,ly);
 line(midx,midy,rx,ry);
 line(lx,ly,SX,SY);
 line(rx,ry,SX,SY);
 setfillstyle(1,incolor);
 floodfill(mx,my,outcolor);
 line(midx,midy,SX,SY);
}
void grid(int midx,int midy,int grad,int gcolor)
{
 for(int i=1;i<=grad;i++)
 {
 setcolor(gcolor);
 circle(midx-1,midy+1,i);
 }
}
ajay.krish123 -4 Junior Poster in Training

It is a very nice good looking analog clock with the different colours.

barneykool 0 Newbie Poster

Huh!!
u call this simple ?? :|

apoorva9191 0 Newbie Poster

its not at all simple...
i took a lot of time..
still in doubts...

Adak 419 Nearly a Posting Virtuoso

Well, phi isn't used in any of the "line" functions, so that can be deleted.

I don't understand why you used 3.1423. Wouldn't 3.1416 be closer?

It's a very nice program.

My critique of it would be that's it's logic is not as simple as it should be. You used good skills to solve the problem, but you didn't use the easiest way to do it.

Aside from having to move some variable declarations to the top of their respective functions, it compiled and ran flawlessly in Turbo C/C++ ver. 1.01.

Congratulations - well done! :)

kings_mitra -1 Junior Poster

Good analysis Adak...
But don't you think the thread is too old to congratulate..!!!

Adak 419 Nearly a Posting Virtuoso

Yeah, I didn't catch the OP date - I saw "Snippet" and it was on the forum first page, and off I went. ;)

Luckychap 68 Posting Pro in Training

Thanks a lot for all the comments specially Adak.
I agreed that it could be far simpler than it is right now and there are far better C compilers that can do same thing with more simplicity. Well I wrote it long back when I just started programming.

Thanks again to all.

challarao 0 Junior Poster in Training

Hey it is showing me some errors in dev-C compiler please help me.

Adak 419 Nearly a Posting Virtuoso

Might be best if you started a new thread, but I'll leave that decision up to you.

What is the error that the compiler is giving you? Turbo C is probably a bit different with some of it's macro's, and headers.

Luckychap 68 Posting Pro in Training

Hi challarao,

As Adak mentioned Turbo C is a bit different from new compilers like DevC. I guess graphics handling is all together different in Dev C. And this program is almost full of graphics.

I think this will not run on Dev C as it uses opengl for graphics.

challarao 0 Junior Poster in Training

The errors are:

graphics.h:No such file or directory
In function 'main':
'DETECT' undeclared(first use in this function)
(Each undeclared identifier is reported only once for each function appears in)
storage size of 't' isn't known
In function 'grid':
'for' loop initial declaration used outside c99 mode

Hey sorry for any inconvenience!

Adak 419 Nearly a Posting Virtuoso

Turbo C, as was mentioned just above, has a very unique graphics ability. You can't duplicate it in another compiler, (in anything like a realistic amount of time and effort).

So you can use this clock code as something to study and learn from, but you can't fix those errors and run it, in graphics mode, without Turbo C.

My suggestion is to make a digital clock in a console window, first. Then work to understand the graphics that you need to use to display an analog clock, in a modern window, with modern graphics.

All in a timely manner, of course! ;)

challarao 0 Junior Poster in Training

Errors are

graphics.h:No such file or directory
In function 'main':
'DETECT' undeclared(first use in this function)
(Each undeclared identifier is reported only once for each function appers in)
storage size of 't' isn't known
In function 'grid':
'for' loop initial declaration used outside c99 mode

sorry for any inconvinience

Adak 419 Nearly a Posting Virtuoso

Let's be clear - you can NOT run a graphics program from Turbo C, in your compiler. It's not a matter of C, it's a matter of support for and inclusion of the graphics.h file. Your compiler doesn't have it, and nobody else uses Turbo C's graphics header files (that I know of, and I've been using Turbo C for since the early 1990's).

You will have to use a graphics library or header, that's compatible with your compiler. Or download Turbo C and use it (from the Borland legacy program site).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.