forked from Alialmanea/C
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clock.cpp
164 lines (149 loc) · 3.75 KB
/
clock.cpp
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include <graphics.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <conio.h>
#include<math.h>
#include<dos.h>
#include<string.h>
#include<iostream>
#include<ctime>
#define S_N_L (radius-10) // Second Needle Length
#define S_N_C RED // Second needle Color
#define M_N_L (radius-20) // Minute Needle Length
#define M_N_C LIGHTRED // Minute Needle Color
#define H_N_L (radius-(radius/2)) // Hour Needle Length
#define H_N_C CYAN // Hour Needle Color
float cx,cy;
float radius=100;
void draw_face(float radius);
void get_time(int &h,int &m,int &s);
void second_needle(int s);
void minute_needle(int m,int s);
void hour_needle(int h,int m,int s);
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
initgraph(&gdriver,&gmode,"d:\\tc\\bgi");
/***********************************/
cx=getmaxx()/2.0; // cx is center x value.
cy=getmaxy()/2.0; // cy is center y value.
/** Now the point (cx,cy) is the center of your screen. **/
float x,y;
int hour,minute,second;
draw_face(radius);
while(!kbhit())
{
get_time(hour,minute,second);
second_needle(second);
minute_needle(minute,second);
hour_needle(hour,minute,second);
circle(cx,cy,2);
delay(100);
}
getch();
closegraph();
return 0;
}
//*************** FUNCTIONS DEFINITIONS *****************//
void draw_face(float radius)
{
int theta=0; // theta is the angle variable.
float x,y;
/** Draw Clock Border. **/
circle(cx,cy,radius+24);
circle(cx,cy,radius+23);
/** Draw GREEN material border. **/
setcolor(BROWN); // I like a wooden frame!
/** Paint the border. **/
for(int i=0;i<9;i++)
circle(cx,cy,radius+13+i);
/** Set the color white. **/
setcolor(WHITE);
/** Draw outer-inner border. **/
circle(cx,cy,radius+12);
circle(cx,cy,radius+10);
/** Draw center dot. **/
circle(cx,cy,2);
int i=0;
/** DRAW NUMERIC POINTS **/
do{
/** Getting (x,y) for numeric points **/
x=cx+radius*cos(theta*M_PI/180);
y=cy+radius*sin(theta*M_PI/180);
/** Draw Numeric Points **/
circle(x,y,2);
/* Draw Dots around each numeric points **/
circle(x+5,y,0);
circle(x-5,y,0);
circle(x,y+5,0);
circle(x,y-5,0);
/** Increase angle by 30 degrees, which is the circular distance between each numeric points. **/
theta+=30;
/** Increase i by 1. **/
i++;
} while(i!=12); //LIMIT NUMERIC POINTS UPTO =12= Numbers.
i=0;
/** DRAW DOTS BETWEEN NUMERIC POINTS. **/
do{
putpixel(cx+radius*cos(i*M_PI/180),cy+radius*sin(i*M_PI/180),DARKGRAY);
i+=6;
}while(i!=360);
/** FACE COMPLETELY DRAWN. **/
}
//================
/** Function to get the current time. **/
void get_time(int &h,int &m,int &s)
{
time_t rawtime;
struct tm *t;
time(&rawtime);
t = gmtime(&rawtime);
h=t->tm_hour;
m=t->tm_min;
s=t->tm_sec;
}
//=================
/** Function to draw Second needle. **/
void second_needle(int s)
{
float angle=-90;
float sx,sy;
setcolor(0);
sx=cx+S_N_L*cos((angle+s*6-6)*M_PI/180);
sy=cy+S_N_L*sin((angle+s*6-6)*M_PI/180);
line(cx,cy,sx,sy);
setcolor(S_N_C);
sx=cx+S_N_L*cos((angle+s*6)*M_PI/180);
sy=cy+S_N_L*sin((angle+s*6)*M_PI/180);
line(cx,cy,sx,sy);
}
/** Function to draw Minute needle. **/
void minute_needle(int m,int s)
{
float angle=-90;
float sx,sy;
setcolor(0);
sx=cx+M_N_L*cos((angle+m*6-6)*M_PI/180);
sy=cy+M_N_L*sin((angle+m*6-6)*M_PI/180);
line(cx,cy,sx,sy);
setcolor(M_N_C);
sx=cx+M_N_L*cos((angle+m*6/*+(s*6/60)*/)*M_PI/180);
sy=cy+M_N_L*sin((angle+m*6/*+(s*6/60)*/)*M_PI/180);
line(cx,cy,sx,sy);
}
/** Function to draw Hour needle. **/
void hour_needle(int h,int m,int s)
{
float angle=-90;
float sx,sy;
setcolor(0);
sx=cx+H_N_L*cos((angle+h*30-(m*30/60))*M_PI/180);
sy=cy+H_N_L*sin((angle+h*30-(m*30/60))*M_PI/180);
line(cx,cy,sx,sy);
setcolor(H_N_C);
sx=cx+H_N_L*cos((angle+h*30+(m*30/60))*M_PI/180);
sy=cy+H_N_L*sin((angle+h*30+(m*30/60))*M_PI/180);
line(cx,cy,sx,sy);
}