-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClock.c
More file actions
215 lines (181 loc) · 5.31 KB
/
Copy pathClock.c
File metadata and controls
215 lines (181 loc) · 5.31 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/************************************************************************************************************/
//HEADERS
#include <stdio.h>
#include <math.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <time.h>
#include <sys/timeb.h>
#include <string.h>
/************************************************************************************************************/
//CONSTANTS
#define HOUR_HAND_LENGTH 50.0
#define MINUTE_HAND_LENGTH 70.7106781187
#define SECOND_HAND_LENGTH 78.1024967591
#define CLOCK_RADIUS 80.0
#define PI 3.14159
#define CLOCK_VOL 100.00
#define ANGLE_ONE_MIN PI/30.0
/************************************************************************************************************/
//STRUCTURE DEFINING POINT
typedef struct Point {
double x , y;
}point;
//ANGLES FOR EACH HAND
double secondAngle = 0 , minuteAngle = 0 , hourAngle = 0;
/************************************************************************************************************/
//FUNCTION TO INITIALIZE STUFF
void init(void) {
gluOrtho2D(-100 , 100 , -100 , 100);
glClearColor(0.0 , 0.0 , 0.0 , 0.0);
glMatrixMode(GL_PROJECTION);
glShadeModel (GL_SMOOTH);
glLoadIdentity();
}
/************************************************************************************************************/
//FUNCTION TO DRAW A POINT
void drawPoint(point p) {
glBegin(GL_POINTS);
glVertex2f(p.x , p.y);
glEnd();
}
/************************************************************************************************************/
//FUNCTION TO DRAW A LINE
void drawLine(double x , double y , double angle) {
glVertex2f(x , x);
glVertex2f(y*cos(angle) , y*sin(angle) );
}
/************************************************************************************************************/
void addDate() {
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
char * s = asctime (timeinfo);
char y[20];
int count_space = 0 , i;
for(i=0 ; i<strlen(s) ; ++i) {
if(s[i]==' ') {
count_space++;
y[i] = ',';
}
else{
y[i] = s[i];
}
if(count_space==3)
break;
}
y[i] = '\0';
glColor3f(1.0 , 1.0 , 0.0);
glRasterPos2f(0 , 90);
for(i=0 ; y[i]!='\0' ; ++i) {
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_10 , y[i]);
}
}
/************************************************************************************************************/
//FUNCTION TO DRAW THE BASIC CLOCK STRUCTURE
void drawMarks(void) {
point sp , ep , p;
int count = 0;
double i = PI/6.0;
sp.x = 0 , sp.y = 0;
ep.x = 0 , ep.y = CLOCK_RADIUS;
glPointSize(10.0);
drawPoint(sp);
for(i=0 ; i<=2*PI ; i+=PI/30.0) {
if(i==PI/2.0) {
p.x = CLOCK_RADIUS;
}
else if(i==3*PI/2.0) {
p.x = -CLOCK_RADIUS;
}
else {
p.x = ep.y*sin(i);
}
p.y = ep.y*cos(i);
if(count%5==0) {
glPointSize(7.0);
drawPoint(p);
}
else {
glPointSize(3.0);
drawPoint(p);
}
count++;
}
}
/************************************************************************************************************/
//MAIN DRAW FUNCTION
void drawClock(void) {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0 , 0.0 , 0.0);
glLineWidth(1.0);
drawMarks();
addDate();
glLineWidth(5.0);
glColor3f(1.0f, 0.0f, 1.0f);
glBegin(GL_LINES);
drawLine(0.0 , HOUR_HAND_LENGTH , -hourAngle+PI/2);
glEnd();
glLineWidth(3.0);
glColor3f(0.0f, 1.0f, 1.0f);
glBegin(GL_LINES);
drawLine(0.0 , MINUTE_HAND_LENGTH , -minuteAngle+PI/2);
glEnd();
glLineWidth(1.0);
glColor3f(0.0f, 0.0f, 1.0f);
glBegin(GL_LINES);
drawLine(0.0 , SECOND_HAND_LENGTH , -secondAngle+PI/2);
glEnd();
glFlush();
glutSwapBuffers();
}
/************************************************************************************************************/
//FUNCTION TO MAINTAIN ASPECT RATIO WHEN SCREEN IS RESIZED
void reshape(int w , int h) {
double aspectRatio;
//to avoid division by 0
if(h==0) {
h=1 ;
}
glViewport(0 , 0 , (GLsizei)w , (GLsizei)h );
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
aspectRatio = (double)w/(double)h;
if (w <= h)
glOrtho (-CLOCK_VOL , CLOCK_VOL , -CLOCK_VOL / aspectRatio , CLOCK_VOL / aspectRatio , 1.0, -1.0);
else
glOrtho (-CLOCK_VOL * aspectRatio , CLOCK_VOL * aspectRatio, -CLOCK_VOL, CLOCK_VOL , 1.0, -1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
/************************************************************************************************************/
//TIMER FUNCTION
void redraw() {
struct timeb tb;
time_t tim=time(0);
struct tm* t;
t=localtime(&tim);
ftime(&tb);
secondAngle = (double)(t->tm_sec+ (double)tb.millitm/1000.0)/30.0 * PI;
minuteAngle = (double)(t->tm_min)/30.0 * PI + secondAngle/60.0;
hourAngle = (double)(t->tm_hour > 12 ? t->tm_hour-12 : t->tm_hour)/6.0 * PI+ minuteAngle/12.0;
glutPostRedisplay();
glutTimerFunc(1000 , redraw , 1);
}
/************************************************************************************************************/
//DRIVER FUNCTION
int main(int argc , char ** argv) {
glutInit(&argc , argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500 , 500);
glutInitWindowPosition(100 , 100);
glutCreateWindow("Clock");
init();
glutDisplayFunc(drawClock);
glutReshapeFunc(reshape);
glutTimerFunc(15 , redraw , 1);
glutMainLoop();
return 0;
}
/************************************************************************************************************/