-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalog_clock.c
More file actions
147 lines (124 loc) · 3.44 KB
/
analog_clock.c
File metadata and controls
147 lines (124 loc) · 3.44 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// line, circle, setcolor,initgraph
#include <graphics.h>
// sin, cos, M_PI
#include <math.h>
// getch
#include <conio.h>
// delay
#include <dos.h>
// time, localtime
#include <time.h>
// NULL
#include <stdio.h>
// system
#include <stdlib.h>
// color time bars
#define SEC_COLOR BLUE
#define MIN_COLOR GREEN
#define HOUR_COLOR RED
#define HOUR_POINTS_COLOR RED
#define SEC_MIN_POINTS_COLOR YELLOW
// center point
#define CENTER_POINT_COLOR MAGENTA
#define CENTER_POINT_SIZE 8
// dont change this
#define SHOW 1
#define HIDE 0
void draw_clock(float cx, float cy, float angle,
float radius, float delay_t,int state){
// get time
time_t t = time(NULL);
struct tm tm = *localtime(&t);
// x,y position for circles and lines
float x, y;
// highlight point, for counter
int h_point, i, hour;
// convert hour 24h to 12h
hour = tm.tm_hour;
hour += (tm.tm_hour>=12)?-12:0;
if(state){
// sec color
setcolor(SEC_COLOR);
outtextxy(10, 10, "second color");
// min color
setcolor(MIN_COLOR);
outtextxy(10, 30, "minute color");
// hour color
setcolor(HOUR_COLOR);
outtextxy(10, 50, "hour color");
}
// draw clock
for(i=-90; i < 270 ;i+=6){
x = cx + cos(i* M_PI / 180) * angle;
y = cy + sin(i* M_PI / 180) * angle;
// draw clock with time
if(state){
// dont hightlight this point
h_point = 0;
// center point
setcolor(CENTER_POINT_COLOR);
circle(cx, cy, radius+4);
// default bar color
setcolor(BLACK);
// time lines
// hour line; slice circle to 360/30 = 12
if(hour == (i + 90) / 30.0){
setcolor(HOUR_COLOR);
h_point = 1;
}
// min line; slice circle to 360/6 = 60
if(tm.tm_min == (i + 90) / 6){
setcolor(MIN_COLOR);
h_point = 1;
}
// sec line; slice circle to 360/6 = 60
if(tm.tm_sec == (i + 90) / 6){
setcolor(SEC_COLOR);
h_point = 1;
}
// draw lines
line(cx, cy, x, y);
// circles; if 360 % 30 == 0 its a hour point
if((i+90)%30==0){
if(!h_point) // highlight this point if a bar in this section
setcolor(HOUR_POINTS_COLOR);
circle(x, y, radius+5);
}
else {
if(!h_point) // highlight this point if a bar in this section
setcolor(SEC_MIN_POINTS_COLOR);
circle(x, y, radius+1);
}
} else {
// animations section
// lines
setcolor(RED);
line(cx, cy, x, y);
// circles
setcolor(CYAN);
circle(x, y, radius);
}
// its like sleep function
delay(delay_t);
}
}
void main(){
float x, y, cx, cy, angle, radius;
int graphdriver = DETECT, graphmode;
initgraph(&graphdriver, &graphmode, "..\\BGI");
// center screen
cx = getmaxx() / 2.0;
cy = getmaxy() / 2.0;
angle = 200;
radius = 1;
// clock style animation
draw_clock(cx, cy, angle, radius, 20, HIDE);
// draw clock
draw_clock(cx, cy, angle, radius, 10, SHOW);
// update clock ; if you press any key program stoped.
while(!kbhit())
draw_clock(cx, cy, angle, radius, 0, SHOW);
// wait for a key and exit
getch();
closegraph();
}