-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathline.cpp
245 lines (212 loc) · 8.57 KB
/
line.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
#include "line.h"
#include "constants.h"
Line::Line(line_type type, sf::Vector2f start, sf::Vector2f finish):
m_type(type),
m_start(start),
m_finish(finish),
m_color(sf::Color::Black),
m_body(sf::Lines, 0)
{
ConstructBody();
}
Line::~Line() {
}
double Line::Length() const {
sf::Vector2f dist = m_finish - m_start;
return sqrt(dist.x*dist.x + dist.y*dist.y);
}
sf::Vector2f Line::GetStart() const {
return m_start;
};
sf::Vector2f Line::GetFinish() const {
return m_finish;
};
sf::Vector2f Line::FromOrigin() const {
return m_finish - m_start;
};
void Line::Draw(sf::RenderTarget& target, draw_type style) const {
if(style == dt_default) {
target.draw(m_body);
} else if (style == dt_simple) {
if(m_type == lt_hidden || m_type == lt_base) {
return; // Don't draw
}
sf::Vertex simpleLine[] = {sf::Vertex(m_start, m_color),
sf::Vertex(m_finish, m_color)};
target.draw(simpleLine, 2, sf::Lines);
} else if (style == dt_overlay) {
if(m_type == lt_hidden || m_type == lt_base) {
return; // Don't draw
}
// TODO: If drawing becomes slow, we can cache these for better performance
sf::Color color = sf::Color::Magenta;;
sf::Vector2f dist = m_finish - m_start;
double length = sqrt(dist.x * dist.x + dist.y * dist.y);
dist /= (float)length;
sf::VertexArray dotted_body;
dotted_body.setPrimitiveType(sf::Lines);
sf::Vector2f dot = dist * (float)LINE_DOT_SIZE;
sf::Vector2f loc = m_start;
loc = m_start;
int numDots = length / LINE_DOT_SIZE / 2;
for(int iii = 0; iii < numDots; iii++) { // Draw the dots along the line
dotted_body.append(sf::Vertex(loc, color));
dotted_body.append(sf::Vertex(loc + dot, color));
loc += dot * (float)2.0;
}
dotted_body.append(sf::Vertex(loc, color));
dotted_body.append(sf::Vertex(m_finish, color));
target.draw(dotted_body);
}
}
void Line::ConstructBody() {
m_body.clear();
m_body.setPrimitiveType(sf::Lines);
sf::Color color = m_color;
if(m_start == m_finish) {
m_body.append(sf::Vertex(m_start, color));
m_body.append(sf::Vertex(m_start, color));
return;
}
sf::Vector2f dist = m_finish - m_start;
double length = sqrt(dist.x * dist.x + dist.y * dist.y);
dist /= (float)length;
sf::Vector2f arrow, loc, dot; // For hidden and base lines
int numDots = -1;
if(m_type != lt_base && m_type != lt_hidden) { // No dots
m_body.append(sf::Vertex(m_start, color));
m_body.append(sf::Vertex(m_finish, color));
}
switch(m_type) {
case lt_topRight:
m_body.append(sf::Vertex(m_finish, color));
m_body.append(sf::Vertex(m_finish - sf::Vector2f(dist.x, dist.y) * (float)LINE_ARROW_LENGTH
+ sf::Vector2f(dist.y*LINE_ARROW_LENGTH, dist.x*-LINE_ARROW_HEIGHT), color));
return;
case lt_botRight:
m_body.append(sf::Vertex(m_finish, color));
m_body.append(sf::Vertex(m_finish - sf::Vector2f(dist.x, dist.y) * (float)LINE_ARROW_LENGTH
+ sf::Vector2f(dist.y*-LINE_ARROW_LENGTH, dist.x*LINE_ARROW_HEIGHT), color));
return;
case lt_topLeft:
m_body.append(sf::Vertex(m_start, color));
m_body.append(sf::Vertex(m_start + sf::Vector2f(dist.x, dist.y) * (float)LINE_ARROW_LENGTH
+ sf::Vector2f(dist.y*LINE_ARROW_LENGTH, dist.x*-LINE_ARROW_HEIGHT), color));
return;
case lt_botLeft:
m_body.append(sf::Vertex(m_start, color));
m_body.append(sf::Vertex(m_start + sf::Vector2f(dist.x, dist.y) * (float)LINE_ARROW_LENGTH
+ sf::Vector2f(dist.y*-LINE_ARROW_LENGTH, dist.x*LINE_ARROW_HEIGHT), color));
return;
case lt_base:
arrow = m_finish - sf::Vector2f(dist.x, dist.y) * (float)LINE_ARROW_LENGTH
+ sf::Vector2f(dist.y*LINE_ARROW_LENGTH, dist.x*-LINE_ARROW_HEIGHT);
loc = m_start;
dot = dist * (float)LINE_DOT_SIZE;
numDots = length / LINE_DOT_SIZE / 2;
for(int iii = 0; iii < numDots; iii++) { // Draw the dots along the line
m_body.append(sf::Vertex(loc, color));
m_body.append(sf::Vertex(loc + dot, color));
loc += dot * (float)2.0;
}
m_body.append(sf::Vertex(loc, color));
m_body.append(sf::Vertex(m_finish, color));
loc = m_finish;
dot = (arrow - m_finish);
dot *= (float)(LINE_DOT_SIZE / sqrt(dot.x*dot.x + dot.y*dot.y));
numDots = sqrt(LINE_ARROW_HEIGHT*LINE_ARROW_HEIGHT + LINE_ARROW_LENGTH*LINE_ARROW_LENGTH) / LINE_DOT_SIZE / 2; // TODO: Don't recalculate this every time
for(int iii = 0; iii < numDots; iii++) { // Draw the dots along the arrow
m_body.append(sf::Vertex(loc, color));
m_body.append(sf::Vertex(loc + dot, color));
loc += dot * (float)2.0;
}
m_body.append(sf::Vertex(loc, color));
m_body.append(sf::Vertex(arrow, color));
break;
case lt_hidden:
m_body.setPrimitiveType(sf::Points);
loc = m_start;
dot = sf::Vector2f(dist.y, -dist.x) * (float)LINE_DOT_DIST;
numDots = length / LINE_DOT_DIST / 2;
for(int iii = 0; iii < numDots; iii++) {
m_body.append(sf::Vertex(loc - dot, color));
loc += dist * (float)LINE_DOT_DIST;
m_body.append(sf::Vertex(loc, color));
loc += dist * (float)LINE_DOT_DIST;
m_body.append(sf::Vertex(loc + dot, color));
}
break;
default: // Solid line, no arrow
return;
}
}
Line::line_type Line::GetType() const {
return m_type;
}
void Line::SetType(line_type newType) {
m_type = newType;
ConstructBody();
}
void Line::SetPosition(sf::Vector2f start, sf::Vector2f finish) {
m_start = start;
m_finish = finish;
ConstructBody();
}
void Line::SetColor(sf::Color color) {
m_color = color;
ConstructBody();
}
Transform Line::Match(const Line& base) const {
sf::Vector2f baseStart = base.GetStart();
sf::Vector2f baseFinish = base.GetFinish();
if(base.GetType() == Line::lt_botLeft ||
base.GetType() == Line::lt_topLeft) {
baseStart = base.GetFinish();
baseFinish = base.GetStart();
}
sf::Vector2f myDiff = m_finish - m_start,
baseDiff = baseFinish - baseStart;
double scale = base.Length() / Length();
double theta = atan2(baseDiff.y*myDiff.x - baseDiff.x*myDiff.y, baseDiff.x*myDiff.x + baseDiff.y*myDiff.y);
sf::Vector2f translate = baseStart - m_start;
sf::Vector2f reflect_start(0,0),
reflect_finish(0,0);
if(base.GetType() == Line::lt_botRight ||
base.GetType() == Line::lt_topLeft) {
reflect_start = baseStart;
reflect_finish = baseFinish;
}
return Transform(scale, theta, m_start, translate, reflect_start, reflect_finish);
}
Line Line::ApplyTransform(Transform t) const {
sf::Vector2f newStart = m_start - t.origin,
newFinish = m_finish - t.origin;
// Rotations
newStart = sf::Vector2f(newStart.x*cos(t.theta) - newStart.y*sin(t.theta),
newStart.x*sin(t.theta) + newStart.y*cos(t.theta));
newFinish = sf::Vector2f(newFinish.x*cos(t.theta) - newFinish.y*sin(t.theta),
newFinish.x*sin(t.theta) + newFinish.y*cos(t.theta));
// Dilations
newStart *= (float)t.scale;
newFinish *= (float)t.scale;
// Translations
newStart += t.origin + t.translate;
newFinish += t.origin + t.translate;
Line::line_type newType = m_type;
if(newType == lt_base)
newType = lt_topLeft;
// Reflections, if necessary
if(t.reflect_start != t.reflect_finish) {
sf::Vector2f axis = t.reflect_finish - t.reflect_start;
double axisSquared = axis.x*axis.x + axis.y*axis.y;
sf::Vector2f P = newStart - t.reflect_start;
newStart = (float)(2*(P.x*axis.x + P.y*axis.y)/axisSquared) * axis - P + t.reflect_start;
P = newFinish - t.reflect_start;
newFinish = (float)(2*(P.x*axis.x + P.y*axis.y)/axisSquared) * axis - P + t.reflect_start;
if(newType == lt_topRight || newType == lt_topLeft)
newType = (Line::line_type)(newType + 1);
else if(newType == lt_botRight || newType == lt_botLeft)
newType = (Line::line_type)(newType - 1);
}
return Line(newType, newStart, newFinish);
}