-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModule.lua
More file actions
215 lines (184 loc) · 6.13 KB
/
Module.lua
File metadata and controls
215 lines (184 loc) · 6.13 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
local function Area(Poly)
local N = #Poly;
local A = 0;
local P = N - 1;
for Q = 1, N, 2 do
local L1, L2 = Poly[P], Poly[Q + 1];
local R1, R2 = Poly[Q], Poly[P + 1];
A = A + (L1 * L2 - R1 * R2);
P = Q;
end
return A * 0.5;
end;
local function InsideTriangle(Ax, Ay, Bx, By, Cx, Cy, Px, Py)
local ax = (Cx - Bx);
local ay = (Cy - By);
local bx = (Ax - Cx);
local by = (Ay - Cy);
local cx = (Bx - Ax);
local cy = (By - Ay);
local apx = (Px - Ax);
local apy = (Py - Ay);
local bpx = (Px - Bx);
local bpy = (Px - By);
local cpx = (Px - Cx);
local cpy = (Px - Cy);
local acbp = ax * bpy - ay * bpx;
local bccp = cx * cpy - by * cpx;
local ccap = cx * apy - cy * apx;
return (acbp >= 0) and (bccp >= 0) and (ccap >= 0);
end;
local function Snip(C, U, V, W, N, V2)
local Ax, Ay, Bx, By, Cx, Cy, Px, Py;
Ax = C[V[U]];
Ay = C[V[U] + 1];
Bx = C[V[V2]];
By = C[V[V2] + 1];
Cx = C[V[W]];
Cy = C[V[W] + 1];
if (0.000001 > (((Bx - Ax) * (Cy - Ay)) - ((By - Ay) * (Cx - Ax)))) then
return false;
end;
for p = 1, N do
if not ((p == U) or (p == V) or (p == W)) then
Px = C[V[p]];
Py = C[V[p] + 1];
if (InsideTriangle(Ax, Ay, Bx, By, Cx, Cy, Px, Py)) then
return false;
end;
end;
end
return true;
end;
local function GroupByN(ToGroup, N)
local Result = {};
local Current = {};
N = N or 3;
for i, v in next, ToGroup do
table.insert(Current, v);
if (#Current >= N) then
table.insert(Result, Current);
Current = {};
end;
end
return Result;
end;
local function Triangulate(Poly)
local Result = {};
local NV = math.floor(#Poly / 2);
local V = {};
if (#Poly < 6) then return end;
if Area(Poly) >= 0 then
for I = 1, NV do
V[I] = I * 2 - 1;
end
else
for I = 1, NV do
V[I] = #Poly - I * 2 + 1;
end
end;
local Count = NV * 2;
local V2 = NV;
while (NV > 2) do
Count = Count - 1;
if (Count < 0) then
return nil;
end;
local U = V2;
if (U > NV) then U = 1 end;
V2 = U + 1;
if (V2 > NV) then V2 = 1 end;
local W = V2 + 1;
if (W > NV) then W = 1 end;
if (Snip(Poly, U, V, W, NV, V2)) then
local A, B, C = V[U], V[V2], V[W];
table.insert(Result, Poly[A]); table.insert(Result, Poly[A + 1]);
table.insert(Result, Poly[B]); table.insert(Result, Poly[B + 1]);
table.insert(Result, Poly[C]); table.insert(Result, Poly[C + 1]);
table.remove(V, V2);
NV = NV - 1;
Count = NV * 2;
end;
end
return GroupByN(Result, 6), Area(Poly);
end;
local V2 = Vector2.new;
local StartPosition = Vector2.new(0,0);
local OldDrawing = Drawing;
local Drawing = {};
Drawing.Fonts = OldDrawing.Fonts;
function Drawing.new(Type)
if (Type == "Polygon") then
local function Triangle(A, B, C)
local NewTriangle = OldDrawing.new("Triangle");
NewTriangle.Visible = true;
NewTriangle.PointA = StartPosition + A;
NewTriangle.PointB = StartPosition + B;
NewTriangle.PointC = StartPosition + C;
NewTriangle.Color = Color3.fromRGB(255, 255, 255);
NewTriangle.Filled = true;
return NewTriangle;
end;
local PolygonMeta = {
Sides = 0,
Color = Color3.fromRGB(255, 255, 255),
Area = 0,
Visible = true
};
local DrawingObjects = {};
return setmetatable({}, {
__newindex = function(_, Index, Value)
if ((Index == "Color") or (Index == "Colour")) then
table.foreach(DrawingObjects, function(_, V)
V.Color = Value;
end)
elseif (Index == "Points") then
local Points = Value;
local TriangulatedNew, NewArea = Triangulate(Points);
if (TriangulatedNew) then
rawset(PolygonMeta, "Area", NewArea);
rawset(PolygonMeta, "Sides", (#Points / 2) - 1);
coroutine.wrap(function()
table.foreach(DrawingObjects, function(_, V)
V:Remove();
end)
for _, v in next, TriangulatedNew do
local Pa = V2(v[1], -v[2]);
local Pb = V2(v[3], -v[4]);
local Pc = V2(v[5], -v[6]);
local Triad = Triangle(Pa, Pb, Pc);
table.insert(DrawingObjects, Triad);
end
end)();
else
return warn("Error triangulating polygon.");
end
elseif (Index == "Visible") then
if (not (Value == rawget(PolygonMeta, "Visible"))) then
table.foreach(DrawingObjects, function(_, V)
V.Visible = Value;
end)
rawset(PolygonMeta, "Visible", Value);
end
end
end;
__index = function(_, Index)
if (Index == "Remove" or Index == "Destroy") then
return function()
coroutine.wrap(function()
table.foreach(DrawingObjects, function(_, V)
V:Remove();
end)
table.clear(PolygonMeta);
setmetatable(PolygonMeta, {});
end)();
end
end;
return rawget(PolygonMeta, Index);
end;
});
else
return OldDrawing.new(Type);
end
end;
return Drawing;