-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathCGShape.cpp
311 lines (220 loc) · 8.48 KB
/
CGShape.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
// CGShape.cpp
//
// CGShape Class
// Copyright (c) 2015 by Kronosaur Productions, LLC. All Rights Reserved.
#include "PreComp.h"
bool CGShape::Arc (const CVector &vCenter, Metric rRadius, Metric rStartAngle, Metric rEndAngle, Metric rArcWidth, CGPath *retPath, Metric rSpacing, DWORD dwFlags)
// Arc
//
// Generates an arc of the given (radial) width. Spacing truncates the ends of
// the arc by the given number of pixels.
//
// ARC_INNER_RADIUS: If this flag is set, rRadius is the inner radius of the
// arc. Otherwise, it is the center.
{
retPath->Init();
// Short-circuit
if (rRadius <= 0.0 || rArcWidth <= 0.0)
return false;
// Options
bool bCenterRadius = ((dwFlags & ARC_INNER_RADIUS ? false : true));
// We will create a path
Metric rHalfWidth = 0.5 * rArcWidth;
Metric rInnerRadius = (bCenterRadius ? Max(0.0, (Metric)rRadius - rHalfWidth) : rRadius);
Metric rOuterRadius = (bCenterRadius ? (Metric)rRadius + rHalfWidth : rRadius + rArcWidth);
// If the start angle equals the end angle, then we create a full ring.
if (rStartAngle == rEndAngle)
{
// Add the points for the outer circle
TArray<CVector> Points;
CGeometry::AddArcPoints(vCenter, rOuterRadius, 0.0, 0.0, &Points);
retPath->AddPolygonHandoff(Points);
// If we've got an inner radius, add that
if (rInnerRadius > 0.0)
{
CGeometry::AddArcPoints(vCenter, rInnerRadius, 0.0, 0.0, &Points);
retPath->AddPolygonHandoff(Points);
}
}
// Otherwise, an arc
else
{
TArray<CVector> Points;
// Figure out how many radians the spacing is at the outer radius and the
// inner radius.
Metric rOuterSpacing = rSpacing / rOuterRadius;
Metric rInnerSpacing = (rInnerRadius > 0.0 ? rSpacing / rInnerRadius : 0.0);
// The spacing adjustments better not be larger than the arc, otherwise
// we paint nothing.
Metric rFullArc = ::mathAngleDiff(rStartAngle, rEndAngle);
if (2.0 * rOuterSpacing >= rFullArc || 2.0 * rInnerSpacing >= rFullArc)
return false;
// Compute angles for outer curve
Metric rStartOuter = ::mathAngleMod(rStartAngle + rOuterSpacing);
Metric rEndOuter = ::mathAngleMod(rEndAngle - rOuterSpacing);
// Start at the outer curve
CGeometry::AddArcPoints(vCenter, rOuterRadius, rStartOuter, rEndOuter, &Points, CGeometry::FLAG_SCREEN_COORDS);
// If the inner radius is 0 then we just need a single point at the center.
if (rInnerRadius == 0.0)
Points.Insert(vCenter);
// Otherwise, continue with the inner curve
else
{
Metric rStartInner = ::mathAngleMod(rStartAngle + rInnerSpacing);
Metric rEndInner = ::mathAngleMod(rEndAngle - rInnerSpacing);
CGeometry::AddArcPoints(vCenter, rInnerRadius, rStartInner, rEndInner, &Points, CGeometry::FLAG_SCREEN_COORDS | CGeometry::FLAG_CLOCKWISE);
}
// Create a path and rasterize it.
retPath->InitTakeHandoff(Points);
}
return true;
}
bool CGShape::ArcSegment (const CVector &vCenter, Metric rRadius, Metric rAngle, Metric rWidth, CGPath *retPath)
// ArcSegment
//
// Generates a circle segment (the area defined by a chord).
{
retPath->Init();
// Make sure we're in bounds
if (rRadius <= 0.0 || rWidth <= 0.0)
return false;
// If width is greater than 2 radii, then we've got a full circle
if (rWidth >= 2.0 * rRadius)
{
TArray<CVector> Points;
CGeometry::AddArcPoints(vCenter, rRadius, 0.0, 0.0, &Points);
retPath->InitTakeHandoff(Points);
}
// Otherwise, a circle segment
else
{
// Let C be the center of the circle. For now we compute everything as if
// C were at the origin (and translate later).
//
// Let P1 be the point at which the chord touches the circle. Let P2 be the
// second point such that P1 to P2 is the arc of the segment.
//
// Let M be the mid point on the chord between P1 and P2.
//
// Start by computing the distance between C and M along the rAngle. This
// will be negative if rWidth > rRadius.
Metric rChordD = rRadius - rWidth;
// Next, compute the angle M-C-P2. If we were to add this angle to rAngle,
// we would end up with the angle of P2.
Metric rHalfAngle = acos(rChordD / rRadius);
// Now compute the two angles for P1 and P2.
Metric rP1Angle = mathAngleMod(rAngle - rHalfAngle);
Metric rP2Angle = mathAngleMod(rAngle + rHalfAngle);
// Add the points along the arc
TArray<CVector> Points;
CGeometry::AddArcPoints(vCenter, rRadius, rP1Angle, rP2Angle, &Points, CGeometry::FLAG_SCREEN_COORDS);
// Create a path and rasterize it.
retPath->InitTakeHandoff(Points);
}
return true;
}
bool CGShape::ArcQuadrilateral (const CVector &vCenter, const CVector &vInnerPos, const CVector &vOuterPos, Metric rWidth, CGPath *retPath)
// ArcQuadrilateral
//
// Generates a shape in which two sides are straight and two sides are curved
// with respect to a center point.
{
retPath->Init();
// Decompose the inner and outer positions
Metric rInnerRadius;
Metric rInnerAngle = vInnerPos.Polar(&rInnerRadius);
Metric rOuterRadius;
Metric rOuterAngle = vOuterPos.Polar(&rOuterRadius);
// Make sure we're in bounds
if (rInnerRadius >= rOuterRadius)
return false;
// Generate some metrics for the quadrilateral
CVector vLength = vOuterPos - vInnerPos;
CVector vHalfWidth = 0.5 * rWidth * vLength.Perpendicular().Normal();
// Generate the intersection between the inner circle and the top line.
bool bNoInnerIntersect = false;
CVector vP1;
CVector vP2;
CVector vInnerCornerUp;
CGeometry::EIntersectResults iResult = CGeometry::IntersectLineCircle(vInnerPos + vHalfWidth, vOuterPos + vHalfWidth, CVector(), rInnerRadius, &vP1, &vP2);
if (iResult == CGeometry::intersectNone)
bNoInnerIntersect = true;
else if (iResult == CGeometry::intersectPoint)
vInnerCornerUp = vP1;
else
vInnerCornerUp = ((vP1 - vInnerPos).Length2() < (vP2 - vInnerPos).Length2() ? vP1 : vP2);
// Now the bottom line
CVector vInnerCornerDown;
iResult = CGeometry::IntersectLineCircle(vInnerPos - vHalfWidth, vOuterPos - vHalfWidth, CVector(), rInnerRadius, &vP1, &vP2);
if (iResult == CGeometry::intersectNone)
bNoInnerIntersect = true;
else if (iResult == CGeometry::intersectPoint)
vInnerCornerDown = vP1;
else
vInnerCornerDown = ((vP1 - vInnerPos).Length2() < (vP2 - vInnerPos).Length2() ? vP1 : vP2);
// If the inner side did not intersect, then we just use a straight line.
if (bNoInnerIntersect)
{
vInnerCornerUp = vInnerPos + vHalfWidth;
vInnerCornerDown = vInnerPos - vHalfWidth;
}
// Generate the intersection between the outer circle and the top line.
bool bNoOuterIntersect = false;
CVector vOuterCornerUp;
iResult = CGeometry::IntersectLineCircle(vInnerPos + vHalfWidth, vOuterPos + vHalfWidth, CVector(), rOuterRadius, &vP1, &vP2);
if (iResult == CGeometry::intersectNone)
bNoOuterIntersect = true;
else if (iResult == CGeometry::intersectPoint)
vOuterCornerUp = vP1;
else
vOuterCornerUp = ((vP1 - vOuterPos).Length2() < (vP2 - vOuterPos).Length2() ? vP1 : vP2);
// Now the bottom line
CVector vOuterCornerDown;
iResult = CGeometry::IntersectLineCircle(vInnerPos - vHalfWidth, vOuterPos - vHalfWidth, CVector(), rOuterRadius, &vP1, &vP2);
if (iResult == CGeometry::intersectNone)
bNoOuterIntersect = true;
else if (iResult == CGeometry::intersectPoint)
vOuterCornerDown = vP1;
else
vOuterCornerDown = ((vP1 - vOuterPos).Length2() < (vP2 - vOuterPos).Length2() ? vP1 : vP2);
// If no intersection, just use the two points
if (bNoOuterIntersect)
{
vOuterCornerUp = vOuterPos + vHalfWidth;
vOuterCornerDown = vOuterPos - vHalfWidth;
}
// We will create a path
TArray<CVector> Points;
// If no outer intersection, then just use the two points
if (bNoOuterIntersect)
{
Points.Insert(vCenter + vOuterCornerDown);
Points.Insert(vCenter + vOuterCornerUp);
}
// Otherwise, start at the outer curve
else
{
Metric rStartOuter = vOuterCornerDown.Polar();
Metric rEndOuter = vOuterCornerUp.Polar();
CGeometry::AddArcPoints(vCenter, rOuterRadius, rStartOuter, rEndOuter, &Points);
}
// If the inner radius is 0 then we just need a single point at the center.
if (rInnerRadius == 0.0)
Points.Insert(vCenter);
// If no intersection, then just use the two points
else if (bNoInnerIntersect)
{
Points.Insert(vCenter + vInnerCornerUp);
Points.Insert(vCenter + vInnerCornerDown);
}
// Otherwise, continue with the inner curve
else
{
Metric rStartInner = vInnerCornerDown.Polar();
Metric rEndInner = vInnerCornerUp.Polar();
CGeometry::AddArcPoints(vCenter, rInnerRadius, rStartInner, rEndInner, &Points, CGeometry::FLAG_CLOCKWISE);
}
// Done
retPath->InitTakeHandoff(Points);
return true;
}