-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclock.cpp
167 lines (130 loc) · 3.64 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
165
166
167
//Program that renders an analog clock in the linux terminal. (not windows compatible)
#include <iostream>
#include "termg.h"// terminal graphics functions
#include "timeutils.h"// time functions
#include <algorithm>// min()
#include <math.h>// sqrt() sine cosine
#include <unistd.h>// usleep
#define PI 3.141592653589793
int width,height;
int bufferWidth,bufferHeight;
char** drawBuffer; //buffer containing next frame
char** oldBuffer; //buffer containing previous frame
void updateDimensions(){
int size[4];
getScreenDim(size);
width = size[0]; height = size[1];
}
void initBuffers() {
drawBuffer = new char*[width];
oldBuffer = new char*[width];
for(int i = 0; i < width; i++) {
drawBuffer[i] = new char[height];
oldBuffer[i] = new char[height];
for(int y = 0; y < height; y++) {
drawBuffer[i][y] = ' ';
oldBuffer[i][y] = ' ';
}
}
bufferWidth = width;
bufferHeight = height;
}
void resizeBuffers(){
char** newDrawBuffer = new char*[width];
char** newOldBuffer = new char*[width];
for(int x = 0; x < width; x++){
newDrawBuffer[x] = new char[height];
newOldBuffer[x] = new char[height];
for(int y = 0; y < height; y++) {
newDrawBuffer[x][y] = ' ';
newOldBuffer[x][y] = ' ';
}
}
for(int x = 0; x < bufferWidth; x++) {
delete []drawBuffer[x];
delete []oldBuffer[x];
}
delete []drawBuffer;
delete []oldBuffer;
drawBuffer = newDrawBuffer;
oldBuffer = newOldBuffer;
clearScreen();
bufferWidth = width;
bufferHeight = height;
}
void updateTerminal() {
//write differences between old and draw to terminal.
for(int y = 0; y < bufferHeight; y++) {
for(int x = 0; x < bufferWidth; x++) {
if(oldBuffer[x][y]!=drawBuffer[x][y]) {
setCharAtLocation(x, y, drawBuffer[x][y]);
}
}
}
//old buffer = draw. draw buffer = old
char** swap = oldBuffer;
oldBuffer = drawBuffer;
drawBuffer = swap;
//oldbuffer which is now draw buffer is already clear.
//So oldBuffer contains current frame and drawBuffer is clear for drawing.
for(int y = 0; y < bufferHeight; y++) {
for(int x = 0; x < bufferWidth; x++) {
drawBuffer[x][y] = ' ';
}
}
//make sure it displays.
std::cout << std::flush;
}
int radius(){
int radius = height/2;
if(radius*2*yoverx > width) {
radius = width/2;
}
return radius;
}
void drawCircle() {
int r = radius();
for(int y = 1; y < height; y++){
//r^2 = (x-width/2)^2 + (y-height/2)^2useep
//(x-width/2)^2 = r^2 - (y-height/2)^2
//x-width/2 = sqrt(r^2 - (y-height/2)^2) or -
//x = width/2 -+ sqrt(r^2 - (y-height/2)^2)
y--;
int x1 = width/2 - yoverx*sqrt(r*r - (y-height/2)*(y-height/2));
int x2 = width/2 + yoverx*sqrt(r*r - (y-height/2)*(y-height/2));
y++;
int x3 = width/2 - yoverx*sqrt(r*r - (y-height/2)*(y-height/2));
int x4 = width/2 + yoverx*sqrt(r*r - (y-height/2)*(y-height/2));
drawLine(x1, y-1, x3, y, drawBuffer);
drawLine(x2, y-1, x4, y, drawBuffer);
}
}
void drawHandle(double progress, double relLength) {
int cx = width/2;
int cy = height/2;
int r = radius();
int dx = relLength*yoverx*r*sin(progress*PI*2);
int dy = relLength*r*cos(progress*PI*2);
drawLine(cx, cy, cx + dx, cy - dy, drawBuffer);
}
int main() {
updateDimensions();
initBuffers();
clearScreen();
setCursorInvis();
while(true) {
updateDimensions();
if(bufferWidth != width || bufferHeight != height) {
resizeBuffers();
}
drawCircle();
double secondProg = timeSecond()/(double)60;
double minProg = timeMinute()/(double)60 + secondProg/(double)60;
double hourProg = timeHour()/(double)12 + minProg/(double)60;
drawHandle(minProg,1);
drawHandle(hourProg,0.5);
drawHandle(secondProg, 0.25);
updateTerminal();
usleep(1000000);
}
}