-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFlatPolygon.cs
151 lines (120 loc) · 4.4 KB
/
FlatPolygon.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
using System;
using System.Diagnostics.CodeAnalysis;
using System.Collections.Generic;
using FlatPhysics;
namespace Flat
{
public static class FlatPolygon
{
public static float Area(FlatVector[] vertices)
{
float area = 0f;
for (int i = 0; i < vertices.Length; i++)
{
FlatVector a = vertices[i];
FlatVector b = vertices[(i + 1) % vertices.Length];
float width = b.X - a.X;
float height = (b.Y + b.X) * 0.5f;
area += width * height;
}
return area;
}
public static bool PointInTriangle(FlatVector p, FlatVector a, FlatVector b, FlatVector c)
{
FlatVector ab = b - a;
FlatVector bc = c - b;
FlatVector ca = a - c;
FlatVector ap = p - a;
FlatVector bp = p - b;
FlatVector cp = p - c;
float c1 = FlatMath.Cross(ap, ab);
float c2 = FlatMath.Cross(bp, bc);
float c3 = FlatMath.Cross(cp, ca);
if (c1 <= 0f || c2 <= 0f || c3 <= 0f)
{
return false;
}
return true;
}
private static bool AnyVerticesInTriangle(FlatVector[] vertices, FlatVector a, FlatVector b, FlatVector c)
{
for (int j = 0; j < vertices.Length; j++)
{
FlatVector p = vertices[j];
if (FlatPolygon.PointInTriangle(p, a, b, c))
{
return true;
}
}
return false;
}
private static T GetItem<T>(List<T> list, int index)
{
int count = list.Count;
if(index >= count)
{
return list[index % count];
}
else if(index < 0)
{
return list[(index % count) + count];
}
return list[index];
}
public static bool Triangulate(FlatVector[] vertices, [NotNullWhen(true)] out int[]? triangleIndices, out string errorMessage)
{
triangleIndices = null;
errorMessage = string.Empty;
if (vertices is null)
{
errorMessage = "Vertices array is null.";
return false;
}
if (vertices.Length < 3)
{
errorMessage = "Vertices array must contain at least 3 items.";
return false;
}
int triangleCount = vertices.Length - 2;
int triangleIndicesCount = triangleCount * 3;
triangleIndices = new int[triangleIndicesCount];
int indexCount = 0;
List<int> indices = new List<int>(vertices.Length);
for (int i = 0; i < vertices.Length; i++)
{
indices.Add(i);
}
while (indices.Count > 3)
{
for (int i = 0; i < indices.Count; i++)
{
int a = FlatPolygon.GetItem(indices, i - 1);
int b = FlatPolygon.GetItem(indices, i);
int c = FlatPolygon.GetItem(indices, i + 1);
FlatVector va = vertices[a];
FlatVector vb = vertices[b];
FlatVector vc = vertices[c];
// Test for convexity. If not convex move to next angle.
if (FlatMath.Cross(va - vb, vc - vb) <= 0f)
{
continue;
}
// Test for any points "inside" this triangle.
if (FlatPolygon.AnyVerticesInTriangle(vertices, va, vb, vc))
{
continue;
}
triangleIndices[indexCount++] = a;
triangleIndices[indexCount++] = b;
triangleIndices[indexCount++] = c;
indices.RemoveAt(i);
break;
}
}
triangleIndices[indexCount++] = indices[0];
triangleIndices[indexCount++] = indices[1];
triangleIndices[indexCount++] = indices[2];
return true;
}
}
}