Show the ticking of Clock

On August 23, 2012, in Animation, by , Views : 38932


Example program to show the ticking of a clock.
clock ticking

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include<graphics.h>
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<dos.h>
 
// Calculates new co-ordinates of a figure after 
// rotating it at an angle about a point (cx,cy)
void rotate( int figure[], int edges, double angle, int cx, int cy )
{
	double x, y;
	angle = -1 * (angle*3.14/180);
	double cos_a = cos(angle);
	double sin_a = sin(angle);
 
	for(int i=0; i < edges; i++)
	{
		x = figure[2*i] - cx;
		y = figure[2*i+1] - cy;
		figure[2*i] = floor( (x * cos_a) - (y * sin_a) + cx + 0.5 );
		figure[2*i+1] = floor( (x * sin_a)+(y * cos_a) + cy + 0.5 );
	}
}
 
void drawClock(int,int,int);
 
void main()
{
	int second_hand[4],minute_hand[4], hour_hand[4], edges = 2 ;
	double angle;
	int cx=300, cy=200;
	int gd = DETECT, gm;
	initgraph( &gd, &gm, "" );
	int max_y = getmaxy();
	clrscr();
	cleardevice();
 
	angle = -6;
 
	// Set the initial position of the second, minute and the hour hands.
 
	second_hand[0] = cx ;
	second_hand[1] = max_y - cy;
	second_hand[2] = cx;
	second_hand[3] = max_y - 320;
 
	hour_hand[0] = cx;
	hour_hand[1] = max_y - cy;
	hour_hand[2] = cx + 90;
	hour_hand[3] = max_y - 200;
 
	minute_hand[0] = cx;
	minute_hand[1] = max_y - cy;
	minute_hand[2] = cx;
	minute_hand[3] = max_y - 310;
 
	cleardevice();
	setbkcolor(WHITE);
 
	// Draw the clock
	drawClock(cx,max_y - cy,150);
	setlinestyle(SOLID_FILL,0,1);
 
	// Draw the minute and the hour hand
	drawpoly(2,minute_hand);
	drawpoly(2,hour_hand);
 
	int i=0;
	while(!kbhit()) {
		setcolor(RED);
		drawpoly(2,second_hand);
		setcolor(GREEN);
		drawpoly(2,minute_hand);
		setcolor(BLUE);
		drawpoly(2,hour_hand);
		delay(1000);   // set delay(10) to tick the clock fast
		setcolor(15);
		drawpoly(2,second_hand);
		rotate(second_hand,edges,angle,cx,max_y - cy);
		i++;
 
		// Reset the second hand and move the minute hand 
		// when the second hand has moved 60 times.
		if(i%60 == 0) {
			second_hand[0] = cx ;
			second_hand[1] = max_y - cy;
			second_hand[2] = cx;
			second_hand[3] = max_y - 320;
			drawpoly(2,minute_hand);
			rotate(minute_hand,edges,angle,cx,max_y - cy);
		}
 
		// Move the minute hand 
		// when the second hand has moved 720 (60*12) times.
		if(i%720 == 0) {
			i = 0;
			drawpoly(2,hour_hand);
			rotate(hour_hand,edges,angle,cx,max_y - cy);
		}
 
	}
 
	getch();
}
 
// Function to draw the clock 
void drawClock(int cx, int cy, int r) {
	int max_y = getmaxy();
	int center[2] = {cx, max_y - 340};
 
	setcolor(GREEN);
	setlinestyle(SOLID_FILL,0,3);
	circle(cx,cy,r);
 
	for(int i=0; i<60; i++) {
		if(i%5 == 0) {
			circle(center[0],center[1],2);
		} else {
			circle(center[0],center[1],1);
		}
		rotate(center,1,-6,cx,cy);
	}
}

Download the code using the below link:

Please 'Like' or '+1' us to get the download link! (If you have already liked us on facebook, then like us either on G+ or twitter)

Hope, it helps! 🙂



Tagged with:  

7 Responses to Show the ticking of Clock

  1. Neelesh says:

    Pls send me ….

  2. SJ says:

    Please provide comments for the program. It’ll help the beginners to understand the code.

  3. AYYAPPAN says:

    nice

Leave a Reply

Your email address will not be published. Required fields are marked *


*

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...

  • Dipin Krishna

Archives

All entries, chronologically...

Read more:
Final translated Image
2D Transformations: Translations

pendulum
Pendulum

ellipse
Ellipse using Mid-Point Algorithm

MinMaxClose