-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
312 lines (246 loc) · 8.42 KB
/
main.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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
//
// main.cpp
// Homework 2: Curves
//
// Created by mahina kaholokula on 10/17/14.
// Copyright (c) 2014 mahina kaholokula. All rights reserved.
//
#pragma once
#include "Curve.cpp"
#include <time.h>
#include <ctime>
////////GLOBAL VARIABLES
double win_width = 700;
double win_height = 700;
std::vector<Freeform*> curves;
Freeform* selected;
int selectedIndex; // with respect to curves vector
char keyPressed = 'X'; // 'X' means no key pressed
time_t currentTime;
struct tm *localTime;
double dt;
int Hour, Min, Sec;
BezierDigit second = *new BezierDigit(0);
BezierDigit tenSec = *new BezierDigit(0);
BezierDigit minute = *new BezierDigit(0);
BezierDigit tenMin = *new BezierDigit(0);
BezierDigit hour = *new BezierDigit(0);
BezierDigit tenHour = *new BezierDigit(0);
std::vector<BezierDigit> digits = {*new BezierDigit(0), *new BezierDigit(1), *new BezierDigit(2), *new BezierDigit(3), *new BezierDigit(4), *new BezierDigit(5), *new BezierDigit(6), *new BezierDigit(7), *new BezierDigit(8), *new BezierDigit(9)};
/////////OTHER FUNCTIONS
void selectNext() {
selectedIndex = (selectedIndex + 1) % curves.size();
selected = curves[selectedIndex];
}
void drawCounter() {
int digit = curves.size() % 10;
BezierDigit num = *new BezierDigit(digit);
glPushMatrix();
glScaled(0.1, 0.1, 1);
glTranslated(9, 9, 1);
num.setColor();
num.draw();
glPopMatrix();
digit = curves.size() / 10;
BezierDigit tens = *new BezierDigit(digit);
glPushMatrix();
glScaled(0.1, 0.1, 1);
glTranslated(8, 9, 1);
tens.setColor();
tens.draw();
glPopMatrix();
}
void initClock() {
time( ¤tTime );// Get the current time
localTime = localtime( ¤tTime ); // Convert the current time to the local time
Hour = localTime->tm_hour;
Min = localTime->tm_min;
Sec = localTime->tm_sec;
second = *new BezierDigit(Sec % 10);
tenSec = *new BezierDigit(Sec / 10);
minute = *new BezierDigit(Min % 10);
tenMin = *new BezierDigit(Min / 10);
hour = *new BezierDigit(Hour % 10);
tenHour = *new BezierDigit(Hour / 10);
}
void drawClock(){
// int one = int(time (NULL)) % 10;
// int ten = int(time (NULL)) % 60 / 10;
// int hun = int(time(NULL)) / 60 % 10;
// int thou = int(time(NULL)) / 60 % 60 / 10;
// int tthou = int(time(NULL)) / 3600 % 24 % 10;
// int hthou = int(time(NULL)) / 3600 % 24 / 10;
for (int i = 0; i < 12; i++) {
second.controlPoints[i] = second.controlPoints[i] + (digits[Sec % 10].controlPoints[i] - second.controlPoints[i]) * dt/10.0;
}
if (Sec % 10 == 0) {
for (int i = 0; i < 12; i++) {
tenSec.controlPoints[i] = tenSec.controlPoints[i] + (digits[Sec / 10].controlPoints[i] - tenSec.controlPoints[i]) * dt/10.0;
}
}
if (Sec / 10 == 0) {
for (int i = 0; i < 12; i++) {
minute.controlPoints[i] = minute.controlPoints[i] + (digits[Min % 10].controlPoints[i] - minute.controlPoints[i]) * dt/10.0;
}
}
if (Min % 10 == 0) {
for (int i = 0; i < 12; i++) {
tenMin.controlPoints[i] = tenMin.controlPoints[i] + (digits[Min / 10].controlPoints[i] - tenMin.controlPoints[i]) * dt/10.0;
}
}
if (Min / 10 == 0) {
for (int i = 0; i < 12; i++) {
hour.controlPoints[i] = hour.controlPoints[i] + (digits[(Hour % 10)].controlPoints[i] - hour.controlPoints[i]) * dt/10.0;
}
}
if (Hour % 10 == 0) {
for (int i = 0; i < 12; i++) {
tenHour.controlPoints[i] = tenHour.controlPoints[i] + (digits[(Hour / 10)].controlPoints[i] - tenHour.controlPoints[i]) * dt/10.0;
}
}
glPushMatrix();
glScaled(0.05, 0.05, 1);
glTranslated(19, -19, 1);
second.setColor();
second.draw();
glPopMatrix();
glPushMatrix();
glScaled(0.05, 0.05, 1);
glTranslated(18, -19, 1);
tenSec.setColor();
tenSec.draw();
glPopMatrix();
glPushMatrix();
glScaled(0.05, 0.05, 1);
glTranslated(16, -19, 1);
minute.setColor();
minute.draw();
glPopMatrix();
glPushMatrix();
glScaled(0.05, 0.05, 1);
glTranslated(15, -19, 1);
tenMin.setColor();
tenMin.draw();
glPopMatrix();
glPushMatrix();
glScaled(0.05, 0.05, 1);
glTranslated(13, -19, 1);
hour.setColor();
hour.draw();
glPopMatrix();
glPushMatrix();
glScaled(0.05, 0.05, 1);
glTranslated(12, -19, 1);
tenHour.setColor();
tenHour.draw();
glPopMatrix();
}
////////IMAGE DISPLAY FUNCTIONS
void onDisplay(){
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
for (Freeform* curve: curves){
if (curve == selected) {
curve->drawControlPoints();
curve->drawSelected(); //sets color special!
}
else {
curve->setColor(); //to normal color and line width
curve->draw();
}
}
drawCounter();
drawClock();
glutSwapBuffers();
}
void onMouse(int button, int state, int x, int y){
double xcoor = x/(win_width/2) - 1;
double ycoor = 1 - y/(win_height/2);
// trying to draw a line
if (state == GLUT_DOWN) {
//new curve being made
if (keyPressed == 'B' or keyPressed == 'L' or keyPressed == 'P')
curves[curves.size() - 1]->addControlPoint(float2(xcoor, ycoor));
//add control point to selected curve
else if (keyPressed == 'A')
selected->addControlPoint(float2(xcoor, ycoor));
//delete control point of selected curve
else if (keyPressed == 'D')
selected->removeControlPoint(xcoor, ycoor);
//selecting a curve
else if (keyPressed == 'X') {
selected = nullptr; // if not near any curve, nothing is selected
for (int i = 0; i < curves.size(); i++) {
if (curves[i]->isNear(xcoor,ycoor)){
selected = curves[i];
selectedIndex = i;
}
}
}
}
glutPostRedisplay();
}
void onKeyboard(unsigned char key, int x, int y) {
if (key == 'B') {
keyPressed = 'B';
selected = new BezierCurve;
curves.push_back(selected);
selectedIndex = curves.size() - 1;
}
else if (key == 'L') {
keyPressed = 'L';
selected = new LagrangeCurve;
curves.push_back(selected);
selectedIndex = curves.size() - 1;
}
else if (key == 'P') {
keyPressed = 'P';
selected = new PolyLine;
curves.push_back(selected);
selectedIndex = curves.size() - 1;
}
else if (selected != nullptr) {
if (key == ' ') {
selectNext();
}
else if (key == 'A' or key == 'D') {
keyPressed = key;
}
}
glutPostRedisplay();
}
void onKeyboardUp(unsigned char key, int x, int y) {
keyPressed = 'X';
//if curve now has < 3 control points, remove it
if (curves[selectedIndex]->controlPoints.size() < 3) {
curves.erase(curves.begin() + selectedIndex);
selected = nullptr;
}
glutPostRedisplay();
}
void onIdle() {
double t = glutGet(GLUT_ELAPSED_TIME) * 0.001;
dt = double(int(floor(t)) % 10) + 1.0;
time( ¤tTime );// Get the current time
localTime = localtime( ¤tTime ); // Convert the current time to the local time
Hour = localTime->tm_hour;
Min = localTime->tm_min;
Sec = localTime->tm_sec;
glutPostRedisplay();
}
int main(int argc, char *argv[]) {
glutInit(&argc, argv); // GLUT initialization
glutInitWindowSize(win_width, win_height); // Initial resolution of the MsWindows Window is 600x600 pixels
glutInitWindowPosition(100, 100); // Initial location of the MsWindows window
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); // Image = 8 bit R,G,B + double buffer + depth buffer
glutCreateWindow("Curves"); // Window is born
initClock();
glutDisplayFunc(onDisplay); // Register event handlers
glutMouseFunc(onMouse);
glutKeyboardFunc(onKeyboard);
glutKeyboardUpFunc(onKeyboardUp);
glutIdleFunc(onIdle);
glutIgnoreKeyRepeat(1); //don't register auto repeat (keyboard func only registers once per char now)
glutMainLoop();
return 0;
}