-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
143 lines (120 loc) · 3.37 KB
/
Copy pathmain.cpp
File metadata and controls
143 lines (120 loc) · 3.37 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
#include <iostream>
#include <windows.h>
#include <cmath>
#ifdef __linux__
#define clearScreenCommand "clear"
#elif _WIN32
#define clearScreenCommand "cls"
#else
#define clearScreenCommand "clear"
#endif
#define width 104
#define height 54
#define PI 3.14159265
char map[width][height];
bool horizontalcorrect = true;
float FocalLen = 1.0f;
float vertices[] = {
// x y
-0.5f, -0.5f, 1.0f,
0.5f, -0.5f, 1.0f,
0.5f, -0.5f, 2.0f,
-0.5f, -0.5f, 2.0f,
-0.5f, 0.5f, 1.0f,
0.5f, 0.5f, 1.0f,
0.5f, 0.5f, 2.0f,
-0.5f, 0.5f, 2.0f
};
const int lines[] = {
1, 2,
2, 3,
3, 4,
4, 1,
1, 5,
2, 6,
3, 7,
4, 8,
5, 6,
6, 7,
7, 8,
8, 5,
};
float calculateCenterOfVertices(float varr[], int varrsize, int axis) {
float maxval = varr[axis], minval = varr[axis];
for (int i = axis; i < varrsize; i += 3) {
if (varr[i] > maxval) maxval = varr[i];
if (varr[i] < minval) minval = varr[i];
}
return (maxval + minval) / 2.0f;
}
void project(float varr[], int varrsize, const int larr[], int larrsize, float xtheta, float ytheta, float ztheta) {
float projectedVertices[varrsize] = {};
float midvalx = calculateCenterOfVertices(varr, varrsize, 0);
float midvaly = calculateCenterOfVertices(varr, varrsize, 1);
float midvalz = calculateCenterOfVertices(varr, varrsize, 2);
for (int i = 0; i < varrsize; i += 3) { //vertex rotation using a 2D Rotation Matrix for each axis
float x = varr[i] - midvalx, y = varr[i + 1] - midvaly, z = varr[i + 2] - midvalz;
float xt = x, yt = y, zt = z;
x = xt * cos(ztheta) - yt * sin(ztheta);
y = xt * sin(ztheta) + yt * cos(ztheta);
xt = x, yt = y, zt = z;
y = yt * cos(xtheta) - zt * sin(xtheta);
z = yt * sin(xtheta) + zt * cos(xtheta);
xt = x, yt = y, zt = z;
z = zt * cos(ytheta) - xt * sin(ytheta);
x = zt * sin(ytheta) + xt * cos(ytheta);
x += midvalx, y += midvaly, z += midvalz;
if (horizontalcorrect) {
x *= float(height) / float(width);
}
projectedVertices[i] = (FocalLen * x) / (z + FocalLen); //xproj
projectedVertices[i + 1] = (FocalLen * y) / (z + FocalLen); //yproj
}
for (int i = 0; i < larrsize; i += 2) {
int x0 = int(projectedVertices[(larr[i] - 1) * 3] * width) + width / 2;
int y0 = int(projectedVertices[(larr[i] - 1) * 3 + 1] * height) + height / 2;
int x1 = int(projectedVertices[(larr[i + 1] - 1) * 3] * width) + width / 2;
int y1 = int(projectedVertices[(larr[i + 1] - 1) * 3 + 1] * height) + height / 2;
int dx = abs(x1 - x0);
int sx = (x0 < x1) ? 1 : -1;
int dy = -abs(y1 - y0);
int sy = (y0 < y1) ? 1 : -1;
int error = dx + dy;
int e2;
while (true) { // Bresenham's Raster
if (x0 >= 0 && y0 >= 0 && x0 < width && y0 < height) map[x0][y0] = '#';
if (x0 == x1 && y0 == y1) { break; };
e2 = 2 * error;
if (e2 >= dy) {
if (x0 == x1) { break; }
error = error + dy;
x0 = x0 + sx;
}
if (e2 <= dx) {
if (y0 == y1) { break; }
error = error + dx;
y0 = y0 + sy;
}
}
}
}
void displayMap() {
system("cls");
for (int i = 0; i < height; i += 1) {
for (int j = 0; j < width; j += 1) {
putchar(map[j][i]), putchar(map[j][i]);
}
putchar('\n');
}
}
int main() {
float alpha = 0.0f;
while (true) {
memset(map, ' ', sizeof(map));//clear projections
project(vertices, sizeof(vertices) / sizeof(float), lines, sizeof(lines) / sizeof(int), 0, alpha, 0);
displayMap();
Sleep(100);
alpha += PI / 60.0f;
}
return 0;
}