-
Notifications
You must be signed in to change notification settings - Fork 12
/
Primitives2D.cs
536 lines (438 loc) · 22.5 KB
/
Primitives2D.cs
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace MonoGame
{
public static class Primitives2D
{
#region Private Members
private static readonly Dictionary<String, List<Vector2>> circleCache = new Dictionary<string, List<Vector2>>();
//private static readonly Dictionary<String, List<Vector2>> arcCache = new Dictionary<string, List<Vector2>>();
private static Texture2D pixel;
#endregion
#region Private Methods
private static void CreateThePixel(SpriteBatch spriteBatch)
{
pixel = new Texture2D(spriteBatch.GraphicsDevice, 1, 1, false, SurfaceFormat.Color);
pixel.SetData(new[] { Color.White });
}
/// <summary>
/// Draws a list of connecting points
/// </summary>
/// <param name="spriteBatch">The destination drawing surface</param>
/// /// <param name="position">Where to position the points</param>
/// <param name="points">The points to connect with lines</param>
/// <param name="color">The color to use</param>
/// <param name="thickness">The thickness of the lines</param>
private static void DrawPoints(SpriteBatch spriteBatch, Vector2 position, List<Vector2> points, Color color, float thickness)
{
if (points.Count < 2)
return;
for (int i = 1; i < points.Count; i++)
{
DrawLine(spriteBatch, points[i - 1] + position, points[i] + position, color, thickness);
}
}
/// <summary>
/// Creates a list of vectors that represents a circle
/// </summary>
/// <param name="radius">The radius of the circle</param>
/// <param name="sides">The number of sides to generate</param>
/// <returns>A list of vectors that, if connected, will create a circle</returns>
private static List<Vector2> CreateCircle(double radius, int sides)
{
// Look for a cached version of this circle
String circleKey = radius + "x" + sides;
if (circleCache.ContainsKey(circleKey))
{
return circleCache[circleKey];
}
List<Vector2> vectors = new List<Vector2>();
const double max = 2.0 * Math.PI;
double step = max / sides;
for (double theta = 0.0; theta < max; theta += step)
{
vectors.Add(new Vector2((float)(radius * Math.Cos(theta)), (float)(radius * Math.Sin(theta))));
}
// then add the first vector again so it's a complete loop
vectors.Add(new Vector2((float)(radius * Math.Cos(0)), (float)(radius * Math.Sin(0))));
// Cache this circle so that it can be quickly drawn next time
circleCache.Add(circleKey, vectors);
return vectors;
}
/// <summary>
/// Creates a list of vectors that represents an arc
/// </summary>
/// <param name="radius">The radius of the arc</param>
/// <param name="sides">The number of sides to generate in the circle that this will cut out from</param>
/// <param name="startingAngle">The starting angle of arc, 0 being to the east, increasing as you go clockwise</param>
/// <param name="radians">The radians to draw, clockwise from the starting angle</param>
/// <returns>A list of vectors that, if connected, will create an arc</returns>
private static List<Vector2> CreateArc(float radius, int sides, float startingAngle, float radians)
{
List<Vector2> points = new List<Vector2>();
points.AddRange(CreateCircle(radius, sides));
points.RemoveAt(points.Count - 1); // remove the last point because it's a duplicate of the first
// The circle starts at (radius, 0)
double curAngle = 0.0;
double anglePerSide = MathHelper.TwoPi / sides;
// "Rotate" to the starting point
while ((curAngle + (anglePerSide / 2.0)) < startingAngle)
{
curAngle += anglePerSide;
// move the first point to the end
points.Add(points[0]);
points.RemoveAt(0);
}
// Add the first point, just in case we make a full circle
points.Add(points[0]);
// Now remove the points at the end of the circle to create the arc
int sidesInArc = (int)((radians / anglePerSide) + 0.5);
points.RemoveRange(sidesInArc + 1, points.Count - sidesInArc - 1);
return points;
}
#endregion
#region FillRectangle
/// <summary>
/// Draws a filled rectangle
/// </summary>
/// <param name="spriteBatch">The destination drawing surface</param>
/// <param name="rect">The rectangle to draw</param>
/// <param name="color">The color to draw the rectangle in</param>
public static void FillRectangle(this SpriteBatch spriteBatch, Rectangle rect, Color color)
{
if (pixel == null)
{
CreateThePixel(spriteBatch);
}
// Simply use the function already there
spriteBatch.Draw(pixel, rect, color);
}
/// <summary>
/// Draws a filled rectangle
/// </summary>
/// <param name="spriteBatch">The destination drawing surface</param>
/// <param name="rect">The rectangle to draw</param>
/// <param name="color">The color to draw the rectangle in</param>
/// <param name="angle">The angle in radians to draw the rectangle at</param>
public static void FillRectangle(this SpriteBatch spriteBatch, Rectangle rect, Color color, float angle)
{
if (pixel == null)
{
CreateThePixel(spriteBatch);
}
spriteBatch.Draw(pixel, rect, null, color, angle, Vector2.Zero, SpriteEffects.None, 0);
}
/// <summary>
/// Draws a filled rectangle
/// </summary>
/// <param name="spriteBatch">The destination drawing surface</param>
/// <param name="location">Where to draw</param>
/// <param name="size">The size of the rectangle</param>
/// <param name="color">The color to draw the rectangle in</param>
public static void FillRectangle(this SpriteBatch spriteBatch, Vector2 location, Vector2 size, Color color)
{
FillRectangle(spriteBatch, location, size, color, 0.0f);
}
/// <summary>
/// Draws a filled rectangle
/// </summary>
/// <param name="spriteBatch">The destination drawing surface</param>
/// <param name="location">Where to draw</param>
/// <param name="size">The size of the rectangle</param>
/// <param name="angle">The angle in radians to draw the rectangle at</param>
/// <param name="color">The color to draw the rectangle in</param>
public static void FillRectangle(this SpriteBatch spriteBatch, Vector2 location, Vector2 size, Color color, float angle)
{
if (pixel == null)
{
CreateThePixel(spriteBatch);
}
// stretch the pixel between the two vectors
spriteBatch.Draw(pixel,
location,
null,
color,
angle,
Vector2.Zero,
size,
SpriteEffects.None,
0);
}
/// <summary>
/// Draws a filled rectangle
/// </summary>
/// <param name="spriteBatch">The destination drawing surface</param>
/// <param name="x">The X coord of the left side</param>
/// <param name="y">The Y coord of the upper side</param>
/// <param name="w">Width</param>
/// <param name="h">Height</param>
/// <param name="color">The color to draw the rectangle in</param>
public static void FillRectangle(this SpriteBatch spriteBatch, float x, float y, float w, float h, Color color)
{
FillRectangle(spriteBatch, new Vector2(x, y), new Vector2(w, h), color, 0.0f);
}
/// <summary>
/// Draws a filled rectangle
/// </summary>
/// <param name="spriteBatch">The destination drawing surface</param>
/// <param name="x">The X coord of the left side</param>
/// <param name="y">The Y coord of the upper side</param>
/// <param name="w">Width</param>
/// <param name="h">Height</param>
/// <param name="color">The color to draw the rectangle in</param>
/// <param name="angle">The angle of the rectangle in radians</param>
public static void FillRectangle(this SpriteBatch spriteBatch, float x, float y, float w, float h, Color color, float angle)
{
FillRectangle(spriteBatch, new Vector2(x, y), new Vector2(w, h), color, angle);
}
#endregion
#region DrawRectangle
/// <summary>
/// Draws a rectangle with the thickness provided
/// </summary>
/// <param name="spriteBatch">The destination drawing surface</param>
/// <param name="rect">The rectangle to draw</param>
/// <param name="color">The color to draw the rectangle in</param>
public static void DrawRectangle(this SpriteBatch spriteBatch, Rectangle rect, Color color)
{
DrawRectangle(spriteBatch, rect, color, 1.0f);
}
/// <summary>
/// Draws a rectangle with the thickness provided
/// </summary>
/// <param name="spriteBatch">The destination drawing surface</param>
/// <param name="rect">The rectangle to draw</param>
/// <param name="color">The color to draw the rectangle in</param>
/// <param name="thickness">The thickness of the lines</param>
public static void DrawRectangle(this SpriteBatch spriteBatch, Rectangle rect, Color color, float thickness)
{
// TODO: Handle rotations
// TODO: Figure out the pattern for the offsets required and then handle it in the line instead of here
DrawLine(spriteBatch, new Vector2(rect.X, rect.Y), new Vector2(rect.Right, rect.Y), color, thickness); // top
DrawLine(spriteBatch, new Vector2(rect.X + 1f, rect.Y), new Vector2(rect.X + 1f, rect.Bottom + thickness), color, thickness); // left
DrawLine(spriteBatch, new Vector2(rect.X, rect.Bottom), new Vector2(rect.Right, rect.Bottom), color, thickness); // bottom
DrawLine(spriteBatch, new Vector2(rect.Right + 1f, rect.Y), new Vector2(rect.Right + 1f, rect.Bottom + thickness), color, thickness); // right
}
/// <summary>
/// Draws a rectangle with the thickness provided
/// </summary>
/// <param name="spriteBatch">The destination drawing surface</param>
/// <param name="location">Where to draw</param>
/// <param name="size">The size of the rectangle</param>
/// <param name="color">The color to draw the rectangle in</param>
public static void DrawRectangle(this SpriteBatch spriteBatch, Vector2 location, Vector2 size, Color color)
{
DrawRectangle(spriteBatch, new Rectangle((int)location.X, (int)location.Y, (int)size.X, (int)size.Y), color, 1.0f);
}
/// <summary>
/// Draws a rectangle with the thickness provided
/// </summary>
/// <param name="spriteBatch">The destination drawing surface</param>
/// <param name="location">Where to draw</param>
/// <param name="size">The size of the rectangle</param>
/// <param name="color">The color to draw the rectangle in</param>
/// <param name="thickness">The thickness of the line</param>
public static void DrawRectangle(this SpriteBatch spriteBatch, Vector2 location, Vector2 size, Color color, float thickness)
{
DrawRectangle(spriteBatch, new Rectangle((int)location.X, (int)location.Y, (int)size.X, (int)size.Y), color, thickness);
}
#endregion
#region DrawLine
/// <summary>
/// Draws a line from point1 to point2 with an offset
/// </summary>
/// <param name="spriteBatch">The destination drawing surface</param>
/// <param name="x1">The X coord of the first point</param>
/// <param name="y1">The Y coord of the first point</param>
/// <param name="x2">The X coord of the second point</param>
/// <param name="y2">The Y coord of the second point</param>
/// <param name="color">The color to use</param>
public static void DrawLine(this SpriteBatch spriteBatch, float x1, float y1, float x2, float y2, Color color)
{
DrawLine(spriteBatch, new Vector2(x1, y1), new Vector2(x2, y2), color, 1.0f);
}
/// <summary>
/// Draws a line from point1 to point2 with an offset
/// </summary>
/// <param name="spriteBatch">The destination drawing surface</param>
/// <param name="x1">The X coord of the first point</param>
/// <param name="y1">The Y coord of the first point</param>
/// <param name="x2">The X coord of the second point</param>
/// <param name="y2">The Y coord of the second point</param>
/// <param name="color">The color to use</param>
/// <param name="thickness">The thickness of the line</param>
public static void DrawLine(this SpriteBatch spriteBatch, float x1, float y1, float x2, float y2, Color color, float thickness)
{
DrawLine(spriteBatch, new Vector2(x1, y1), new Vector2(x2, y2), color, thickness);
}
/// <summary>
/// Draws a line from point1 to point2 with an offset
/// </summary>
/// <param name="spriteBatch">The destination drawing surface</param>
/// <param name="point1">The first point</param>
/// <param name="point2">The second point</param>
/// <param name="color">The color to use</param>
public static void DrawLine(this SpriteBatch spriteBatch, Vector2 point1, Vector2 point2, Color color)
{
DrawLine(spriteBatch, point1, point2, color, 1.0f);
}
/// <summary>
/// Draws a line from point1 to point2 with an offset
/// </summary>
/// <param name="spriteBatch">The destination drawing surface</param>
/// <param name="point1">The first point</param>
/// <param name="point2">The second point</param>
/// <param name="color">The color to use</param>
/// <param name="thickness">The thickness of the line</param>
public static void DrawLine(this SpriteBatch spriteBatch, Vector2 point1, Vector2 point2, Color color, float thickness)
{
// calculate the distance between the two vectors
float distance = Vector2.Distance(point1, point2);
// calculate the angle between the two vectors
float angle = (float)Math.Atan2(point2.Y - point1.Y, point2.X - point1.X);
DrawLine(spriteBatch, point1, distance, angle, color, thickness);
}
/// <summary>
/// Draws a line from point1 to point2 with an offset
/// </summary>
/// <param name="spriteBatch">The destination drawing surface</param>
/// <param name="point">The starting point</param>
/// <param name="length">The length of the line</param>
/// <param name="angle">The angle of this line from the starting point in radians</param>
/// <param name="color">The color to use</param>
public static void DrawLine(this SpriteBatch spriteBatch, Vector2 point, float length, float angle, Color color)
{
DrawLine(spriteBatch, point, length, angle, color, 1.0f);
}
/// <summary>
/// Draws a line from point1 to point2 with an offset
/// </summary>
/// <param name="spriteBatch">The destination drawing surface</param>
/// <param name="point">The starting point</param>
/// <param name="length">The length of the line</param>
/// <param name="angle">The angle of this line from the starting point</param>
/// <param name="color">The color to use</param>
/// <param name="thickness">The thickness of the line</param>
public static void DrawLine(this SpriteBatch spriteBatch, Vector2 point, float length, float angle, Color color, float thickness)
{
if (pixel == null)
{
CreateThePixel(spriteBatch);
}
// stretch the pixel between the two vectors
spriteBatch.Draw(pixel,
point,
null,
color,
angle,
Vector2.Zero,
new Vector2(length, thickness),
SpriteEffects.None,
0);
}
#endregion
#region PutPixel
public static void PutPixel(this SpriteBatch spriteBatch, float x, float y, Color color)
{
PutPixel(spriteBatch, new Vector2(x, y), color);
}
public static void PutPixel(this SpriteBatch spriteBatch, Vector2 position, Color color)
{
if (pixel == null)
{
CreateThePixel(spriteBatch);
}
spriteBatch.Draw(pixel, position, color);
}
#endregion
#region DrawCircle
/// <summary>
/// Draw a circle
/// </summary>
/// <param name="spriteBatch">The destination drawing surface</param>
/// <param name="center">The center of the circle</param>
/// <param name="radius">The radius of the circle</param>
/// <param name="sides">The number of sides to generate</param>
/// <param name="color">The color of the circle</param>
public static void DrawCircle(this SpriteBatch spriteBatch, Vector2 center, float radius, int sides, Color color)
{
DrawPoints(spriteBatch, center, CreateCircle(radius, sides), color, 1.0f);
}
/// <summary>
/// Draw a circle
/// </summary>
/// <param name="spriteBatch">The destination drawing surface</param>
/// <param name="center">The center of the circle</param>
/// <param name="radius">The radius of the circle</param>
/// <param name="sides">The number of sides to generate</param>
/// <param name="color">The color of the circle</param>
/// <param name="thickness">The thickness of the lines used</param>
public static void DrawCircle(this SpriteBatch spriteBatch, Vector2 center, float radius, int sides, Color color, float thickness)
{
DrawPoints(spriteBatch, center, CreateCircle(radius, sides), color, thickness);
}
/// <summary>
/// Draw a circle
/// </summary>
/// <param name="spriteBatch">The destination drawing surface</param>
/// <param name="x">The center X of the circle</param>
/// <param name="y">The center Y of the circle</param>
/// <param name="radius">The radius of the circle</param>
/// <param name="sides">The number of sides to generate</param>
/// <param name="color">The color of the circle</param>
public static void DrawCircle(this SpriteBatch spriteBatch, float x, float y, float radius, int sides, Color color)
{
DrawPoints(spriteBatch, new Vector2(x, y), CreateCircle(radius, sides), color, 1.0f);
}
/// <summary>
/// Draw a circle
/// </summary>
/// <param name="spriteBatch">The destination drawing surface</param>
/// <param name="x">The center X of the circle</param>
/// <param name="y">The center Y of the circle</param>
/// <param name="radius">The radius of the circle</param>
/// <param name="sides">The number of sides to generate</param>
/// <param name="color">The color of the circle</param>
/// <param name="thickness">The thickness of the lines used</param>
public static void DrawCircle(this SpriteBatch spriteBatch, float x, float y, float radius, int sides, Color color, float thickness)
{
DrawPoints(spriteBatch, new Vector2(x, y), CreateCircle(radius, sides), color, thickness);
}
#endregion
#region DrawArc
/// <summary>
/// Draw a arc
/// </summary>
/// <param name="spriteBatch">The destination drawing surface</param>
/// <param name="center">The center of the arc</param>
/// <param name="radius">The radius of the arc</param>
/// <param name="sides">The number of sides to generate</param>
/// <param name="startingAngle">The starting angle of arc, 0 being to the east, increasing as you go clockwise</param>
/// <param name="radians">The number of radians to draw, clockwise from the starting angle</param>
/// <param name="color">The color of the arc</param>
public static void DrawArc(this SpriteBatch spriteBatch, Vector2 center, float radius, int sides, float startingAngle, float radians, Color color)
{
DrawArc(spriteBatch, center, radius, sides, startingAngle, radians, color, 1.0f);
}
/// <summary>
/// Draw a arc
/// </summary>
/// <param name="spriteBatch">The destination drawing surface</param>
/// <param name="center">The center of the arc</param>
/// <param name="radius">The radius of the arc</param>
/// <param name="sides">The number of sides to generate</param>
/// <param name="startingAngle">The starting angle of arc, 0 being to the east, increasing as you go clockwise</param>
/// <param name="radians">The number of radians to draw, clockwise from the starting angle</param>
/// <param name="color">The color of the arc</param>
/// <param name="thickness">The thickness of the arc</param>
public static void DrawArc(this SpriteBatch spriteBatch, Vector2 center, float radius, int sides, float startingAngle, float radians, Color color, float thickness)
{
List<Vector2> arc = CreateArc(radius, sides, startingAngle, radians);
//List<Vector2> arc = CreateArc2(radius, sides, startingAngle, degrees);
DrawPoints(spriteBatch, center, arc, color, thickness);
}
#endregion
}
}