-
Notifications
You must be signed in to change notification settings - Fork 0
/
Shape.cpp
executable file
·275 lines (232 loc) · 5.77 KB
/
Shape.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
#include <math.h>
#ifdef __MACOSX_CORE__
#include <GLUT/glut.h>
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#else
#ifdef _WIN32
#include <windows.h>
#endif
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#endif
#include <iostream>
#include <time.h>
#include <stdlib.h>
#include "Shape.h"
// ==================
// Arc implementation
// ==================
Spiral::Spiral(Point2D center, float startAngle, float startRadius,
float endAngle, float endRadius) :
m_center(center),
m_startRadius(startRadius),
m_endRadius(endRadius),
m_startAngle(startAngle),
m_endAngle(endAngle),
m_filled(false)
{
m_fDotted = false;
m_fDirected = false;
m_lineWidth = 1;
m_color = Color(0, 0, 0);
}
void Spiral::setFilled(bool filled)
{
m_filled = filled;
}
void Spiral::setColor(const Color& color)
{
m_color = color;
}
void Spiral::draw()
{
float end = m_endAngle;
if (m_endAngle > m_startAngle)
end -= 360;
glColor4f(m_color.r, m_color.g, m_color.b, m_color.a);
/*
if (m_fDotted) {
glEnable(GL_LINE_STIPPLE);
glLineStipple(1, 0xFF00);
}
*/
float angle, x, y, interpRadius;
float maxRadius = std::max(m_startRadius, m_endRadius);
float radiusDiff = m_startRadius - m_endRadius;
float newAngleDiff, baseAngleDiff = m_startAngle - end;
if (!m_filled) {
glLineWidth(m_lineWidth);
glBegin(GL_LINE_STRIP);
} else
glBegin(GL_POLYGON);
{
// Draw counter-clockwise
x = m_center.x + m_endRadius * cos((double) m_endAngle * MY_PI / 180);
y = m_center.y - m_endRadius * sin((double) m_endAngle * MY_PI / 180);
glVertex2f(x, y);
for (angle = end + 180 / maxRadius; angle < m_startAngle; angle += 180 / maxRadius) {
newAngleDiff = Spiral::clampAngle(angle) - m_endAngle;
if (Spiral::clampAngle(angle) < m_endAngle)
newAngleDiff += 360;
interpRadius = m_endRadius + radiusDiff * newAngleDiff / baseAngleDiff;
x = m_center.x + interpRadius * cos((double) angle * MY_PI / 180);
y = m_center.y - interpRadius * sin((double) angle * MY_PI / 180);
glVertex2f(x, y);
}
x = m_center.x + m_startRadius * cos((double) m_startAngle * MY_PI / 180);
y = m_center.y - m_startRadius * sin((double) m_startAngle * MY_PI / 180);
glVertex2f(x, y);
}
glEnd();
/*
if (m_fDotted)
glDisable(GL_LINE_STIPPLE);
*/
}
// Utility function that returns the angle of a point around the center.
// Positive x-axis is 0 degree
float Spiral::getAngle(Point2D center, Point2D p)
{
// Flip the y because the direction of y is reversed
float dx = p.x - center.x,
dy = center.y - p.y;
float adx = abs(dx),
ady = abs(dy);
float baseAngle = 0.0f;
// The same point
if (adx == 0 && ady == 0)
return 0;
// Perpendicular
if (adx == 0) {
if (dy > 0)
return 90;
else
return 270;
} else if (ady == 0) {
if (dx > 0)
return 0;
else
return 180;
}
baseAngle = atan(ady / adx) * 180 / MY_PI;
if (dx < 0)
baseAngle = 180 - baseAngle;
if (dy < 0)
baseAngle = 360 - baseAngle;
return baseAngle;
}
Point2D Spiral::getPointFromRadius(Point2D center, float radius, float angle)
{
float x = center.x + radius * cos((double) angle * MY_PI / 180),
y = center.y - radius * sin((double) angle * MY_PI / 180);
return Point2D(x, y);
}
float Spiral::clampAngle(float angle) {
for (; angle < 0; angle += 360);
for (; angle > 360; angle -= 360);
return angle;
}
// ===================
// Line implementation
// ===================
Line::Line(Point2D p1, Point2D p2) : m_p1(p1), m_p2(p2)
{
m_fDotted = false;
m_fDirected = false;
m_lineWidth = 1;
m_color = Color(0, 0, 0);
}
void Line::draw()
{
glColor3f(m_color.r, m_color.g, m_color.b);
if (m_fDotted) {
glEnable(GL_LINE_STIPPLE);
glLineStipple(1, 0xFF00);
}
glLineWidth(m_lineWidth);
glBegin(GL_LINES);
{
glVertex2f(m_p1.x, m_p1.y);
glVertex2f(m_p2.x, m_p2.y);
}
glEnd();
if (m_fDotted)
glDisable(GL_LINE_STIPPLE);
// Draw the indicator at the start and end part of the line
if (m_fDirected) {
glBegin(GL_POINTS);
{
glPointSize(10);
glVertex2f(m_p1.x, m_p1.y);
glPointSize(5);
glVertex2f(m_p2.x, m_p2.y);
}
glEnd();
}
}
float Line::getDistance(Point2D pos)
{
// pos - start
Point2D a;
a.x = pos.x - m_p1.x;
a.y = pos.y - m_p1.y;
// end - start
Point2D b;
b.x = -(m_p2.y - m_p1.y);
b.y = m_p2.x - m_p1.x;
// length of end - start
float amp = sqrt(b.x * b.x + b.y * b.y);
// distance from line to point
// a dot b / amp
return (a.x * b.x + a.y * b.y) / amp;
}
float Line::getParallelPosition(Point2D pos)
{
// pos - start
Point2D a;
a.x = pos.x - m_p1.x;
a.y = pos.y - m_p1.y;
// end - start
Point2D b;
b.x = m_p2.x - m_p1.x;
b.y = m_p2.y - m_p1.y;
// length of end - start
float amp = sqrt(b.x * b.x + b.y * b.y);
// position between start & end, from 0 -to-> 1
// a dot b / length^2
// < 0 or > 1 if beyond endpoints
return (a.x * b.x + a.y * b.y) / (amp * amp);
}
Text::Text(Point2D pos, std::string str) : m_pos(pos), m_str(str)
{
m_color = Color(0, 0, 0);
}
void Text::draw()
{
glColor3f(m_color.r, m_color.g, m_color.b);
glRasterPos2i(m_pos.x, m_pos.y);
int lines = 0;
for(const char *p = m_str.c_str(); *p; p++) {
while (*p == '\n') {
lines++;
p++;
glRasterPos2i(m_pos.x, m_pos.y + (lines * 18));
}
glutBitmapCharacter(GLUT_BITMAP_9_BY_15, *p);
}
}
int Text::getWidth()
{
int maxLineWidth = 0, width = 0;
for(const char *p = m_str.c_str(); *p; p++) {
if (*p == '\n') {
maxLineWidth = std::max(width, maxLineWidth);
width = 0;
}
width += glutBitmapWidth(GLUT_BITMAP_9_BY_15, *p);
}
maxLineWidth = std::max(width, maxLineWidth);
return maxLineWidth;
}