Advertisements
Home > Cpp Graphics > C++ Graphics – 21 – Analog Clock code/program showing current time

C++ Graphics – 21 – Analog Clock code/program showing current time


Analog Clock program by using Trigonometry (SIN & COS) functions:

Cpp 21 Clock

#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <math.h>
#include <dos.h>

#define PI 3.14

void getTime(int *h, int *m, int *s ){
	struct time t;
	gettime(&t);

	gotoxy(36,18);
	printf("%2d:%02d:%02d.%02d\n",
		 t.ti_hour,t.ti_min,t.ti_sec,t.ti_hund);

	*h = t.ti_hour;
	*m = t.ti_min;
	*s = t.ti_sec;
	}

void main(){
	int gd=DETECT, gm;
	initgraph(&gd, &gm, "\\tc");

	int xs, ys, xm, ym, xh, yh, h, m, s;

	while(!kbhit()){
		cleardevice();

		getTime(&h, &m, &s);

		settextstyle(1,0,0);
		setcolor(WHITE);
		outtextxy(300,15,"12");
		outtextxy(315,425,"6");
		outtextxy(105,220,"9");
		outtextxy(520,220,"3");
		settextstyle(5,0,0);

		setcolor(GREEN);
		outtextxy(275,300,"CLOCK");
		settextstyle(2 ,0,0);

		setcolor(LIGHTRED);
		outtextxy(310,295,"Manoj");

		xh = cos((h*30 + m / 2) * PI / 180 - PI / 2) * 150 + 320;
		yh = sin((h*30 + m / 2) * PI / 180 - PI / 2) * 150 + 240;
		xm = cos(m * PI / 30 - PI / 2) * 180 + 320;
		ym = sin(m * PI / 30 - PI / 2) * 180 + 240;
		xs = cos(s * PI / 30 - PI / 2) * 210 + 320;
		ys = sin(s * PI / 30 - PI / 2) * 210 + 240;

		setcolor(LIGHTBLUE);
		circle(320,240,220);

		setcolor(LIGHTRED);
		line(320,240,xh,yh);

		setcolor(LIGHTGREEN);
		line(320,240,xm,ym);

		setcolor(YELLOW);
		line(320,240,xs,ys);

		sleep(1);
		}
	}
Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: