-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcube.c
More file actions
286 lines (249 loc) · 6.32 KB
/
cube.c
File metadata and controls
286 lines (249 loc) · 6.32 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
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
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#define ESC "\x1B"
#define FPS 30.0f
#define TERM_COLS 150
#define TERM_ROWS 80
#define VERTICAL_BORDER "║"
#define HORIZONTAL_BORDER "═"
#define TOP_LEFT_BORDER "╔"
#define TOP_RIGHT_BORDER "╗"
#define BOT_LEFT_BORDER "╚"
#define BOT_RIGHT_BORDER "╝"
#define CIRCLE "⚪"
#define SQUARE "█"
#define LEFT -100.0f
#define RIGHT 100.0f
#define TOP -100.0f
#define BOT 100.0f
static bool RUN = true;
typedef struct {
float x, y, z;
} Point3;
typedef struct {
float x, y;
} Point2;
typedef Point3 Cube[8];
#define CUBE_SIZE 50.0f
#define CUBE_LEFT -CUBE_SIZE/2.0f
#define CUBE_TOP -CUBE_SIZE/2.0f
#define CUBE_FRONT -CUBE_SIZE/2.0f
#define CUBE_RIGHT CUBE_LEFT + CUBE_SIZE
#define CUBE_BOTTOM CUBE_TOP + CUBE_SIZE
#define CUBE_BACK CUBE_FRONT + CUBE_SIZE
static Cube THE_CUBE = {
//TOP
{CUBE_LEFT, CUBE_TOP, CUBE_FRONT}, {CUBE_RIGHT, CUBE_TOP, CUBE_FRONT },
{CUBE_RIGHT, CUBE_TOP, CUBE_BACK}, {CUBE_LEFT, CUBE_TOP, CUBE_BACK },
//BOTTOM
{CUBE_LEFT, CUBE_BOTTOM, CUBE_FRONT}, {CUBE_RIGHT, CUBE_BOTTOM, CUBE_FRONT },
{CUBE_RIGHT, CUBE_BOTTOM, CUBE_BACK}, {CUBE_LEFT, CUBE_BOTTOM, CUBE_BACK },
};
Point2 project(Point3 p)
{
Point2 pp = (Point2) {
.x = p.x,
.y = p.y,
};
return pp;
}
void handle_sigint(int _)
{
(void)_;
RUN = false;
}
Point2 point_from_ints(int x, int y)
{
return (Point2) {
.x = (float)x,
.y = (float)y,
};
}
void term_clear()
{
printf(ESC"[2J"); // erase entire screen
printf(ESC"[H"); // move cursor to Home = (0,0)
}
void term_init()
{
signal(SIGINT, handle_sigint);
printf(ESC"[?47h"); // save screen
printf(ESC"[?25l"); // make cursor invisible
printf(ESC"[?1049h"); // enables the alternative buffer
}
void term_cleanup()
{
printf(ESC"[?25h"); // make cursor visible
printf(ESC"[?1049l"); // disables the alternative buffer
printf(ESC"[?47l"); // restore screen
}
void term_move(int col, int row)
{
printf(ESC "[%d;%df", row, col); // move cursor to (row, col)
}
void term_moveto(Point2 p)
{
// map LEFT<->RIGHT to 0<->TERM_COLS
int col = ((p.x + (LEFT - RIGHT) / 2.0f) / (LEFT - RIGHT)) * TERM_COLS;
// map TOP<->BOT to 0<->TERM_ROWS
int row = ((p.y + (TOP - BOT) / 2.0f) / (TOP - BOT)) * TERM_ROWS;
term_move(col, row);
}
void term_borders()
{
// Top and Bottom Borders
for (int i = 1; i < TERM_COLS - 1; ++i) {
term_move(i, 0);
printf(HORIZONTAL_BORDER);
term_move(i, TERM_ROWS - 1);
printf(HORIZONTAL_BORDER);
}
// Left and Right Borders
for (int j = 1; j < TERM_ROWS - 1; ++j) {
term_move(0, j);
printf(VERTICAL_BORDER);
term_move(TERM_COLS - 1, j);
printf(VERTICAL_BORDER);
}
// Corners
term_move(0, 0);
printf(TOP_LEFT_BORDER);
term_move(TERM_COLS-1, 0);
printf(TOP_RIGHT_BORDER);
term_move(0, TERM_ROWS-1);
printf(BOT_LEFT_BORDER);
term_move(TERM_COLS-1, TERM_ROWS-1);
printf(BOT_RIGHT_BORDER);
}
void point_print(Point2 p)
{
term_moveto(p);
printf(SQUARE);
}
void line_print(Point3 a, Point3 b)
{
Point2 pa = project(a);
Point2 pb = project(b);
if ((int)pa.x != (int)pb.x) {
Point2 start = pa.x < pb.x ? pa : pb;
Point2 end = pa.x < pb.x ? pb : pa;
float dy = end.y - start.y;
float dx = end.x - start.x;
float m = dy / dx;
for (int i = 0; start.x + i <= end.x; ++i) {
Point2 p = (Point2) {
.x = start.x + i,
.y = start.y + m * i,
};
point_print(p);
}
} else {
float start = pa.y < pb.y ? pa.y : pb.y;
float end = pa.y < pb.y ? pb.y : pa.y;
for (int j = 0; start + j <= end; ++j) {
Point2 p = (Point2) {
.x = pa.x,
.y = start + j,
};
point_print(p);
}
}
}
void cube_print(Cube c)
{
for (int i = 0; i < 4; ++i) {
line_print(c[i] , c[(i + 1) % 4]);
line_print(c[i + 4], c[(i + 1) % 4 + 4]);
line_print(c[i] , c[i + 4]);
}
}
void cube_rotate_x(Cube cube, float angle)
{
float s = sinf(angle);
float c = cosf(angle);
for (int i = 0; i < 8; ++i) {
float old_x = cube[i].x;
float old_y = cube[i].y;
float old_z = cube[i].z;
cube[i] = (Point3) {
.x = old_x,
.y = c * old_y + -s * old_z,
.z = s * old_y + c * old_z,
};
}
}
void cube_rotate_y(Cube cube, float angle)
{
float s = sinf(angle);
float c = cosf(angle);
for (int i = 0; i < 8; ++i) {
float old_x = cube[i].x;
float old_y = cube[i].y;
float old_z = cube[i].z;
cube[i] = (Point3) {
.x = c * old_x + s * old_z,
.y = old_y,
.z = -s * old_x + c * old_z,
};
}
}
void cube_rotate_z(Cube cube, float angle)
{
float s = sinf(angle);
float c = cosf(angle);
for (int i = 0; i < 8; ++i) {
float old_x = cube[i].x;
float old_y = cube[i].y;
float old_z = cube[i].z;
cube[i] = (Point3) {
.x = c * old_x + -s * old_y,
.y = s * old_x + c * old_y,
.z = old_z,
};
}
}
void axis()
{
Point3 y_axis_top = (Point3){
.x = 0,
.y = TOP,
.z = 0,
};
Point3 y_axis_bot = (Point3){
.x = 0,
.y = BOT,
.z = 0,
};
line_print(y_axis_top, y_axis_bot);
Point3 x_axis_top = (Point3){
.x = LEFT,
.y = 0,
.z = 10,
};
Point3 x_axis_bot = (Point3){
.x = RIGHT,
.y = 0,
.z = 10,
};
line_print(x_axis_top, x_axis_bot);
}
int main(void)
{
term_init();
float theta = M_PI / (float)100;
while (RUN) {
term_clear();
term_borders();
cube_rotate_x(THE_CUBE, theta);
cube_rotate_y(THE_CUBE, theta);
cube_rotate_z(THE_CUBE, theta);
cube_print(THE_CUBE);
fflush(stdout);
usleep(1000000 / FPS);
}
term_cleanup();
return 0;
}