-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtriangle.cpp
More file actions
112 lines (80 loc) · 2.52 KB
/
Copy pathtriangle.cpp
File metadata and controls
112 lines (80 loc) · 2.52 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
#include <iostream>
#include <math.h>
#include <vector>
#include <unistd.h>
using namespace std;
void rotate(float& x, float& y, float theta);
void plot(float& x, float& y, vector<string>& area, const float& scale, const int& centerX, const int& centerY);
int main()
{
// area dimension
const int width = 80;
const int height = 24;
// center factor
const int centerX = width/2;
const int centerY = height/2;
// scale factor
const float scale = 7.5f;
// rotation properties
float theta = 0.0f;
const float rotationSpeed = 0.05f;
// create triangle
const float range = 0.02f;
// x and y cartesians points
float x, y;
while (1)
{
// area buffer
vector<string> area(height, string(width, '.'));
for (float i = 0.0f; i <= 1.0f; i+=range)
{
// bottom side
x = -1.0f + 2.0f * i;
y = -1.0f;
rotate(x, y, theta);
plot(x, y, area, scale, centerX, centerY);
if (i <= 0.5)
{
// diagonal left-side
x = -1.0f + 2.0f * i;
y = 2 * x + 1; // bend the line
rotate(x, y, theta);
plot(x, y, area, scale, centerX, centerY);
// diagonal right-side
x = 1.0f - 2.0f * i;
y = -2 * x + 1; // bend the line
rotate(x, y, theta);
plot(x, y, area, scale, centerX, centerY);
}
}
for (auto &row : area)
{
cout << row << endl;
}
theta += rotationSpeed;
// animation delay
usleep(15000); // 2ms
}
return 0;
}
void plot(float& x, float& y, vector<string>& area, const float& scale, const int& centerX, const int& centerY)
{
// convert (1, 1) ... (-1, -1) to (0, 0) ... (0, 0)
const int screenX = static_cast<int>(centerX + (x * scale));
const int screenY = static_cast<int>(centerY - (y * scale));
// insert to buffer area
if (screenX >= 0 && screenX < static_cast<int>(area[0].size()) && screenY >= 0 && screenY < static_cast<int>(area.size()))
{
area[screenY][screenX] = 'O';
}
}
void rotate(float& x, float& y, float theta)
{
float new_x, new_y;
// rotate using matrix 2d-rotation
new_x = x * cos(theta) - y * sin(theta);
new_y = x * sin(theta) + y * cos(theta);
// assign rotated value to x and y
x = new_x;
y = new_y;
}