-
Notifications
You must be signed in to change notification settings - Fork 0
/
DrawGeometry.cs
72 lines (60 loc) · 2.77 KB
/
DrawGeometry.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Limestone.Utility;
namespace Limestone
{
public class DrawGeometry
{
public static void DrawRectangle(SpriteBatch batch, Rectangle area, Color color)
{
Texture2D whitePixel = Assets.GetTexture("whitePixel");
batch.Draw(whitePixel, area, color);
}
public static void DrawHollowRectangle(SpriteBatch batch, Rectangle area, int width, Color color)
{
Texture2D whitePixel = Assets.GetTexture("whitePixel");
batch.Draw(whitePixel, new Rectangle(area.X, area.Y, area.Width, width), color);
batch.Draw(whitePixel, new Rectangle(area.X, area.Y, width, area.Height), color);
batch.Draw(whitePixel, new Rectangle(area.X + area.Width - width, area.Y, width, area.Height), color);
batch.Draw(whitePixel, new Rectangle(area.X, area.Y + area.Height - width, area.Width, width), color);
}
public static void DrawCircle(SpriteBatch batch, Vector2 center, float radius, Color color, int lineWidth = 2, int segments = 16)
{
Vector2[] vertex = new Vector2[segments];
double increment = Math.PI * 2.0 / segments;
double theta = 0.0;
for (int i = 0; i < segments; i++)
{
vertex[i] = center + radius * new Vector2((float)Math.Cos(theta), (float)Math.Sin(theta));
theta += increment;
}
DrawPolygon(batch, vertex, segments, color, lineWidth);
}
public static void DrawPolygon(SpriteBatch batch, Vector2[] vertex, int count, Color color, int lineWidth)
{
Texture2D whitePixel = Assets.GetTexture("whitePixel");
if (count > 0)
{
for (int i = 0; i < count - 1; i++)
{
DrawLine(batch, vertex[i], vertex[i + 1], color, lineWidth);
}
DrawLine(batch, vertex[count - 1], vertex[0], color, lineWidth);
}
}
public static void DrawLine(SpriteBatch batch, Vector2 begin, Vector2 end, Color color, int width = 1)
{
Texture2D whitePixel = Assets.GetTexture("whitePixel");
Rectangle r = new Rectangle((int)begin.X, (int)begin.Y, (int)(end - begin).Length() + width, width);
Vector2 v = Vector2.Normalize(begin - end);
float angle = (float)Math.Acos(Vector2.Dot(v, -Vector2.UnitX));
if (begin.Y > end.Y) angle = MathHelper.TwoPi - angle;
batch.Draw(whitePixel, r, null, color, angle, Vector2.Zero, SpriteEffects.None, 0);
}
}
}