-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
304 lines (243 loc) · 8.15 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
#include <iostream>
#include <string>
#include <Windows.h>
#include <vector>
#include <algorithm>
#include <cmath>
#include <thread>
#include <chrono>
const double PI = 3.14159265358979323846;
const double focal_length = 80;
template <typename T>
bool is_belongs(const std::vector<T> &vec, const T &val) {
for (const auto &el : vec) {
if (el == val) {
return true;
}
}
return false;
}
class Point2D {
private:
double x;
double y;
public:
Point2D() {
this->x = 0;
this->y = 0;
}
Point2D(double x, double y) {
this->x = x;
this->y = y;
}
double getX() const { return this->x; }
double getY() const { return this->y; }
friend bool operator== (const Point2D &lhs, const Point2D &rhs) {
return ( (lhs.x == rhs.x) && (lhs.y == rhs.y) );
}
};
class Point3D {
private:
double x;
double y;
double z;
public:
Point3D() {
this->x = 0.0;
this->y = 0.0;
this->z = 0.0;
}
Point3D(double x, double y, double z) {
this->x = x;
this->y = y;
this->z = z;
}
double getX() const { return this->x; }
double getY() const { return this->y; }
double getZ() const { return this->z; }
};
class Matrix {
private:
std::vector<std::vector<double>> matrix;
public:
Matrix() {
this->matrix = std::vector<std::vector<double>>();
}
Matrix(std::vector<std::vector<double>> matrix) {
this->matrix = matrix;
}
std::vector<std::vector<double>> getMatrix() const { return this->matrix; }
double get(int i, int j) const { return this->matrix[i][j]; }
Matrix operator*(const Matrix &rhs) {
std::vector<std::vector<double>> result;
for (int i = 0; i < this->matrix.size(); i++) {
std::vector<double> row;
for (int j = 0; j < rhs.matrix[0].size(); j++) {
double sum = 0;
for (int k = 0; k < this->matrix[0].size(); k++) {
sum += this->matrix[i][k] * rhs.matrix[k][j];
}
row.push_back(sum);
}
result.push_back(row);
}
return Matrix(result);
}
Matrix operator*(const std::vector<double> vec) {
// Check if the vector size is equal to the number of columns in the matrix
if (vec.size() != this->matrix[0].size()) {
std::cerr << "Vector size must be equal to the number of columns in the matrix!" << std::endl;
return Matrix();
}
std::vector<std::vector<double>> result;
for (int i = 0; i < this->matrix.size(); i++) {
std::vector<double> row;
for (int j = 0; j < vec.size(); j++) {
row.push_back(this->matrix[i][j] * vec[j]);
}
result.push_back(row);
}
return Matrix(result);
}
};
class Console {
private:
HANDLE handle;
CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
int width;
int height;
public:
Console() {
this->handle = GetStdHandle(STD_OUTPUT_HANDLE);
if (!GetConsoleScreenBufferInfo(this->handle, &(this->csbiInfo))) {
std::cerr << "Failed to get console buffer info!" << std::endl;
exit(1); // todo: throw error
}
this->width = csbiInfo.dwSize.X;
this->height = csbiInfo.dwSize.Y;
}
int getWidth() { return this->width; }
int getHeight() { return this->height; }
void draw_points(const std::vector<Point2D> &points) {
std::string to_draw;
for (int y = 0; y < this->height; y++) {
for (int x = 0; x < this->width; x++) {
to_draw += " ";
}
if (y < this->height - 1) {
to_draw += "\n";
}
}
for (const Point2D &point : points) {
int x = point.getX();
int y = point.getY();
if (x >= 0 && x < this->width && y >= 0 && y < this->height) {
to_draw[y * this->width + x] = '0';
}
}
std::cout << to_draw;
}
void clear() {
COORD topLeft = {0, 0};
DWORD length = this->width * this->height;
DWORD written;
FillConsoleOutputCharacter(this->handle, TEXT(' '), length, topLeft, &written);
SetConsoleCursorPosition(this->handle, topLeft);
}
std::vector<Point2D> project(const std::vector<Point3D> &points) {
std::vector<Point2D> result;
for(const Point3D &point : points) {
int x = this->width / 2 + point.getX() * (focal_length / point.getZ());
int y = this->height / 2 + point.getY() * (focal_length / point.getZ());
result.emplace_back(x, y);
}
return result;
}
};
class Torus {
private:
Point3D center;
double R;
double r;
std::vector<Point3D> points;
public:
Torus() {
this->R = 0.0;
this->r = 0.0;
}
Torus(Point3D center, double R, double r) {
this->center = center;
this->R = R;
this->r = r;
for(double i = 0; i < 2 * PI; i += PI / 100) {
for(double j = 0; j < 2 * PI; j += PI / 100) {
double x = ((R + r * std::cos(j)) * std::cos(i)) + center.getX();
double y = ((R + r * std::cos(j)) * std::sin(i)) + center.getY();
double z = (r * std::sin(j)) + center.getZ();
this->points.emplace_back(x, y, z);
}
}
}
std::vector<Point3D> getPoints() const { return this->points; }
void rotate(double angleX, double angleY, double angleZ) {
std::vector<std::vector<double>> rotationX = {
{1, 0, 0},
{0, std::cos(angleX), -std::sin(angleX)},
{0, std::sin(angleX), std::cos(angleX)}
};
std::vector<std::vector<double>> rotationY = {
{std::cos(angleY), 0, std::sin(angleY)},
{0, 1, 0},
{-std::sin(angleY), 0, std::cos(angleY)}
};
std::vector<std::vector<double>> rotationZ = {
{std::cos(angleZ), -std::sin(angleZ), 0},
{std::sin(angleZ), std::cos(angleZ), 0},
{0, 0, 1}
};
// Rotation matrices
Matrix rotationXMatrix(rotationX);
Matrix rotationYMatrix(rotationY);
Matrix rotationZMatrix(rotationZ);
// Apply rotations around the center of the torus
for (int i = 0; i < this->points.size(); i++) {
Point3D point = this->points[i];
Matrix pointMatrix({{point.getX() - this->center.getX(),
point.getY() - this->center.getY(),
point.getZ() - this->center.getZ()}});
Matrix rotatedPointMatrix = pointMatrix * rotationXMatrix * rotationYMatrix * rotationZMatrix;
this->points[i] = Point3D(rotatedPointMatrix.get(0, 0) + this->center.getX(),
rotatedPointMatrix.get(0, 1) + this->center.getY(),
rotatedPointMatrix.get(0, 2) + this->center.getZ());
}
}
};
int main() {
using namespace std::chrono;
const int fps = 60; // Desired FPS
milliseconds msPerFrame(1000 / fps);
steady_clock::time_point lastFrameTime = steady_clock::now();
Console myConsole;
Point3D torus_center(0, 0, -300);
Torus myTorus(torus_center, 30, 10);
std::vector<Point2D> points = myConsole.project(myTorus.getPoints());
myConsole.clear();
while (true) {
myTorus.rotate(0.1, 0.1, 0.1);
points = myConsole.project(myTorus.getPoints());
steady_clock::time_point start = steady_clock::now();
myConsole.clear();
myConsole.draw_points(points);
steady_clock::time_point end = steady_clock::now();
auto duration = duration_cast<milliseconds>(end - start);
auto sleepTime = msPerFrame - duration;
if (sleepTime.count() > 0) {
std::this_thread::sleep_for(sleepTime);
} else {
// Frame took longer than our frame time
std::cerr << "Warning: Frame processing took too long\n";
}
lastFrameTime = steady_clock::now();
}
return 0;
}