-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpyth-tree.cpp
More file actions
178 lines (141 loc) · 5.38 KB
/
Copy pathpyth-tree.cpp
File metadata and controls
178 lines (141 loc) · 5.38 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
#include <iostream>
#include <fstream>
#include <algorithm>
#include <string>
#include <cmath>
#include <limits>
#define PI 3.14159265358979323846
#define ALPHA std::asin(0.6)
// Global variables
double c = 5;
double minX = std::numeric_limits<double>::max();
double minY = std::numeric_limits<double>::max();
double maxX = std::numeric_limits<double>::min();
double maxY = std::numeric_limits<double>::min();
std::ofstream* resultFile;
class Vertex3 {
public:
Vertex3() : components{0} { }
Vertex3(double x, double y, double z) {
(*this)[0] = x; (*this)[1] = y; (*this)[2] = z;
}
double operator[](int idx) const {
return this->components[idx];
}
double& operator[](int idx) {
return this->components[idx];
}
private:
double components[3];
};
class Matrix3 {
public:
Matrix3() : components{0} {}
const double* operator[](int idx) const {
return this->components[idx];
}
double* operator[](int idx) {
return this->components[idx];
}
Matrix3 operator*(const Matrix3& other) {
Matrix3 result;
for(int i = 0; i < 3; ++i) {
for(int j = 0; j < 3; ++j) {
result[i][j] = (*this)[i][0] * other[0][j] + (*this)[i][1] * other[1][j] + (*this)[i][2] * other[2][j];
}
}
return result;
}
Vertex3 operator*(const Vertex3& other) {
Vertex3 result;
for(int i = 0; i < 3; ++i) {
result[i] = (*this)[i][0] * other[0] + (*this)[i][1] * other[1] + (*this)[i][2] * other[2];
}
return result;
}
static Matrix3 identity() {
Matrix3 result;
result[0][0] = 1; result[1][1] = 1; result[2][2] = 1;
return result;
}
static Matrix3 translate(double x, double y) {
Matrix3 result = Matrix3::identity();
result[0][2] = x; result[1][2] = y;
return result;
}
static Matrix3 scale(double x, double y) {
Matrix3 result = Matrix3::identity();
result[0][0] = x; result[1][1] = y;
return result;
}
static Matrix3 rotate(double theta) {
Matrix3 result = Matrix3::identity();
result[0][0] = std::cos(theta); result[0][1] = -std::sin(theta);
result[1][0] = std::sin(theta); result[1][1] = std::cos(theta);
return result;
}
private:
double components[3][3];
};
void drawStruct(Matrix3 m, std::ofstream& file) {
// The points in the orthonormal system that define the basic structure
static const Vertex3 A{0, 0, 1};
static const Vertex3 B{5, 0, 1};
static const Vertex3 C{5, 5, 1};
static const Vertex3 D{0, 5, 1};
static const Vertex3 E{16.0/5, 37.0/5, 1};
static const Vertex3 F{4.0/5, 53.0/5, 1};
static const Vertex3 G{-12.0/5, 41.0/5, 1};
static const Vertex3 H{37.0/5, 34.0/5, 1};
static const Vertex3 I{28.0/5, 46.0/5, 1};
Vertex3 mA = m * A;
Vertex3 mB = m * B;
Vertex3 mC = m * C;
Vertex3 mD = m * D;
Vertex3 mE = m * E;
Vertex3 mF = m * F;
Vertex3 mG = m * G;
Vertex3 mH = m * H;
Vertex3 mI = m * I;
// Draw the structure
file << mA[0] << " " << mA[1] << " moveto\n";
file << mB[0] << " " << mB[1] << " lineto\n";
file << mC[0] << " " << mC[1] << " lineto\n";
file << mD[0] << " " << mD[1] << " lineto\n";
file << mA[0] << " " << mA[1] << " lineto\n";
file << mD[0] << " " << mD[1] << " moveto\n";
file << mE[0] << " " << mE[1] << " lineto\n";
file << mF[0] << " " << mF[1] << " lineto\n";
file << mG[0] << " " << mG[1] << " lineto\n";
file << mD[0] << " " << mD[1] << " lineto\n";
file << mC[0] << " " << mC[1] << " moveto\n";
file << mH[0] << " " << mH[1] << " lineto\n";
file << mI[0] << " " << mI[1] << " lineto\n";
file << mE[0] << " " << mE[1] << " lineto\n";
file << mC[0] << " " << mC[1] << " lineto\n";
// Update the bounding box
minX = std::min({minX, mA[0], mB[0], mC[0], mD[0], mE[0], mF[0], mG[0], mH[0], mI[0]}); maxX = std::max({maxX, mA[0], mB[0], mC[0], mD[0], mE[0], mF[0], mG[0], mH[0], mI[0]});
minY = std::min({minY, mA[1], mB[1], mC[1], mD[1], mE[1], mF[1], mG[1], mH[1], mI[1]}); maxY = std::max({maxY, mA[1], mB[1], mC[1], mD[1], mE[1], mF[1], mG[1], mH[1], mI[1]});
}
void pythagoreanTree(Matrix3 mvt, int depth, std::ofstream& file) {
if(depth == 0) return;
Matrix3 left = mvt * Matrix3::translate(0, 5) * Matrix3::rotate(ALPHA) * Matrix3::scale(0.8, 0.8);
Matrix3 right = mvt * Matrix3::translate(5, 5) * Matrix3::rotate(ALPHA - PI/2) * Matrix3::scale(0.6, 0.6) * Matrix3::translate(-5, 0);
drawStruct(mvt, file);
pythagoreanTree(left, depth - 1, file);
pythagoreanTree(right, depth - 1, file);
}
int main(int argc, char** args) {
int n = std::stoi(std::string(args[1]));
std::ofstream file("result" + std::to_string(n) + ".eps");
if(file.good()) {
file << "%!PS-Adobe-3.0 EPSF-3.0 \n" << "%%%%Title: Pythagorean-tree " << n << " iterations\n" << "%%Creator: (Josué Mongan) \n"
<< "%%BoundingBox: (atend)\n" << "%%EndComments\n" << "1.0 setlinewidth\n";
pythagoreanTree(Matrix3::scale(15, 15) * Matrix3::translate(18, 22), n, file);
file << "stroke\n" << "%%Trailer\n" << "%%%%BoundingBox: " << std::floor(minX) - 100 << " " << std::floor(minY) - 100 << " " << std::ceil(maxX) + 100 << " " << std::ceil(maxY) + 100 << "\n"
<< "%%EOF";
} else {
return 1;
}
return 0;
}