-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShape.cpp
More file actions
252 lines (241 loc) · 5.3 KB
/
Shape.cpp
File metadata and controls
252 lines (241 loc) · 5.3 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
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
/* Author: Brendon Kofink
* Johann Rajadurai
* Aaron Sierra
* David Day
* Lucy Ray
* Assignment Title: Ball Game
* Assignment Description: user can launch balls to hit objects.
* Due Date: 12/08/2021
* Date Created: 11/08/2021
* Date Last Modified: 12/04/2021
*/
#include "Shape.h"
#include "vec2.h"
Shape::Shape(int sides, double radius, Color color)
{
this->sides = sides;
this->radius = radius;
this->color = color;
this->angle = 0;
generateVertices();
}
Shape::Shape()
{
this->sides = 10;
this->radius = 10;
this->angle = 0;
this->color = RED;
generateVertices();
}
void Shape::VerticalLine(SDL_Plotter &g, int x, int y1, int y2) const
{
if(y1>y2) {
swap(y1,y2);
}
for (int y = y1; y <= y2; y++)
{
if (x < g.getCol() && y < g.getRow() && x >= 0 && y >= 0)
{
g.plotPixel(x, y, color.R, color.G, color.B);
}
}
}
// Differs based on shape type, so each will have an if statement.
void Shape::draw(SDL_Plotter &g, vec2 pos) const
{
int count = 0;
Edge e, e1, e2;
// Circle
if (sides >= 10)
{
double i, angle, x1, y1;
// Only calculate half the circle
// and in the Draw Rect call, mirror it across the x axis.
for (i = 0; i < 180; i += 0.1)
{
// Basically, using trig to avoid using distance functions whenever possible
angle = i;
x1 = radius * cos(angle * PI / 180);
y1 = radius * sin(angle * PI / 180);
int x = center->x + x1;
VerticalLine(g, pos.x + x1, pos.y + y1, pos.y - y1);
}
}
else
{
int x = -radius;
// Using relativeVertices, create a filled in polygon
while(x <= radius)
{
count = 0;
// Find vertices that cross this x coordinate
for (int i = 0; i < sides; i++)
{
e = Edge(relativeVertices[i], relativeVertices[(i + 1) % sides]);
if (e.ContainsX(x))
{
if (count == 0)
{
e1 = e;
}
else if (count == 1)
{
e2 = e;
}
count++;
}
}
// Make better later
if (count > 0)
{
// Draw the line between the two vertices
if (count == 1)
{
VerticalLine(g, x, e1.p1.y, e1.p2.y);
}
else if (count == 2)
{
VerticalLine(g, x+pos.x, e1.Evaluate(x)+pos.y, e2.Evaluate(x)+pos.y);
}
}
x++;
}
}
}
void Shape::draw(SDL_Plotter &g) const
{
draw(g, *center);
}
// isColliding will be called by the GO.
bool Shape::isColliding(const Shape &other) const
{
bool isColliding = true;
vec2 axis, otherAxis;
double min, max, otherMin, otherMax;
// Get the axes
axis = getCollisionAxis(other).normalized();
// Get the projections
getProjection(axis, min, max);
other.getProjection(axis, otherMin, otherMax);
// Check if min and max overlap with otherMin and otherMax
if (min <= otherMax && max <= otherMin)
{
isColliding = false;
}
else
{
otherAxis = other.getCollisionAxis(*this).normalized();
// Get the projections
getProjection(otherAxis, min, max);
other.getProjection(otherAxis, otherMin, otherMax);
// Check if min and max overlap with otherMin and otherMax
if (min <= otherMax && max <= otherMin)
{
isColliding = false;
}
}
return isColliding;
}
vec2 Shape::getCollisionAxis(const Shape &other) const
{
int max;
Edge currEdge, bestEdge;
vec2 axis;
if (sides >= 10)
{
axis = *other.center - *center;
}
else
{
int max = -100000;
for (int i = 0; i < sides; i++)
{
currEdge = Edge(relativeVertices[i], relativeVertices[(i + 1) % sides]);
if (currEdge.GetNormal().dot(*other.center - *center) > max)
{
max = currEdge.GetNormal().dot(*other.center - *center);
bestEdge = currEdge;
}
}
axis = bestEdge.GetNormal();
}
return axis;
}
void Shape::getProjection(vec2 axis, double &min, double &max) const
{
// If the shape is a circle, just use the center and radius.
if (sides >= 10)
{
min = (*center-axis*radius).dot(axis);
max = (*center+axis*radius).dot(axis);
}
else
{
// Otherwise, go through each vertex and find the min and max.
min = center->dot(axis);
max = center->dot(axis);
for (int i = 0; i < sides; i++)
{
int dot = (relativeVertices[i]+*center).dot(axis);
if (dot < min)
min = dot;
if (dot > max)
max = dot;
}
}
}
void Shape::generateVertices()
{
if (sides < 10)
{
relativeVertices.clear();
double vertAngles = PI / sides;
vertAngles += angle;
for (size_t i = 0; i < sides; i++)
{
relativeVertices.push_back(vec2((cos(vertAngles) * radius), (sin(vertAngles) * radius)));
vertAngles += 2 * PI / sides;
}
}
}
// Getters
Color Shape::getColor()
{
return color;
}
double Shape::getRadius()
{
return radius;
}
int Shape::getSides()
{
return sides;
}
double Shape::getAngle()
{
return angle;
}
// Setters
void Shape::setColor(Color color)
{
this->color = color;
}
void Shape::setRadius(double radius)
{
this->radius = radius;
generateVertices();
}
void Shape::setAngle(double angle)
{
this->angle = angle;
generateVertices();
}
void Shape::setCenter(vec2 *center)
{
this->center = center;
}
void Shape::rotate(double angle)
{
this->angle += angle;
generateVertices();
}