-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathempower.lua
278 lines (231 loc) · 7.54 KB
/
empower.lua
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
-- MIT License
--
-- Copyright (c) 2017 Mikael Lind
--
-- Permission is hereby granted, free of charge, to any person obtaining a copy
-- of this software and associated documentation files (the "Software"), to deal
-- in the Software without restriction, including without limitation the rights
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-- copies of the Software, and to permit persons to whom the Software is
-- furnished to do so, subject to the following conditions:
--
-- The above copyright notice and this permission notice shall be included in all
-- copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-- SOFTWARE.
--
-- See: https://github.com/elemel/empower
local empower = {}
local function cross2(x1, y1, x2, y2)
return x1 * y2 - x2 * y1
end
local function normalize2(x, y)
local length = math.sqrt(x * x + y * y)
if length == 0 then
return 1, 0, 0
end
return x / length, y / length, length
end
local Circle = {}
Circle.__index = Circle
function empower.newCircle(x, y, radius)
local circle = setmetatable({}, Circle)
circle.x = x or 0
circle.y = y or 0
circle.radius = radius or 1
assert(circle.radius >= 0)
return circle
end
function Circle:clone()
return empower.newCircle(self.x, self.y, self.radius)
end
-- We have [1]:
--
-- d == d1 + d2 => d2 == d - d1
--
-- And [2]:
--
-- d1 ^ 2 - r1 ^ 2 == d2 ^ 2 - r2 ^ 2
--
-- Insert [1] into [2]:
--
-- d1 ^ 2 - r1 ^ 2 == (d - d1) ^ 2 - r2 ^ 2 =>
-- d1 ^ 2 - r1 ^ 2 == d ^ 2 - 2 * d * d1 + d1 ^ 2 - r2 ^ 2 =>
-- 2 * d * d1 - r1 ^ 2 == d ^ 2 - r2 ^ 2 =>
-- 2 * d * d1 == d ^ 2 + r1 ^ 2 - r2 ^ 2 =>
-- d1 == (d ^ 2 + r1 ^ 2 - r2 ^ 2) / (2 * d)
function Circle:getRadicalAxis(other)
local directionX, directionY, distance = normalize2(other.x - self.x, other.y - self.y)
local distance1
if self.radius == other.radius then
distance1 = distance / 2
else
distance1 = (distance ^ 2 + self.radius ^ 2 - other.radius ^ 2) / (2 * distance)
end
local x1 = self.x + directionX * distance1
local y1 = self.y + directionY * distance1
local x2 = x1 - directionY
local y2 = y1 + directionX
return x1, y1, x2, y2
end
local Diagram = {}
Diagram.__index = Diagram
function empower.newDiagram(polygon)
local diagram = setmetatable({}, Diagram)
diagram.polygon = assert(polygon)
diagram.circles = {}
diagram.circlePolygons = {}
diagram.nextIndex = 1
return diagram
end
function Diagram:addCircle(circle)
circle = circle:clone()
local polygon = self.polygon:clone()
local emptyIndices = {}
for i, circle2 in pairs(self.circles) do
local polygon2 = assert(self.circlePolygons[i])
local x1, y1, x2, y2 = circle:getRadicalAxis(circle2)
polygon:clip(x1, y1, x2, y2)
polygon2:clip(x2, y2, x1, y1)
if #polygon2.vertices < 6 then
table.insert(emptyIndices, i)
end
end
while true do
local emptyIndex = table.remove(emptyIndices)
if not emptyIndex then
break
end
self.circles[emptyIndex] = nil
self.circlePolygons[emptyIndex] = nil
end
if #polygon.vertices < 6 then
return nil
end
local index = self.nextIndex
self.nextIndex = self.nextIndex + 1
self.circles[index] = circle
self.circlePolygons[index] = polygon
return index
end
local Polygon = {}
Polygon.__index = Polygon
function empower.newPolygon(...)
local polygon = setmetatable({}, Polygon)
polygon.vertices = {...}
assert(#polygon.vertices % 2 == 0)
assert(#polygon.vertices >= 6)
return polygon
end
function Polygon:clone()
return empower.newPolygon(unpack(self.vertices))
end
function Polygon:getBounds()
local x1 = math.huge
local y1 = math.huge
local x2 = -math.huge
local y2 = -math.huge
for i = 1, #self.vertices, 2 do
local x = self.vertices[i]
x1 = math.min(x1, x)
x2 = math.max(x2, x)
end
for i = 2, #self.vertices, 2 do
local y = self.vertices[i]
y1 = math.min(y1, y)
y2 = math.max(y2, y)
end
return x1, y1, x2, y2
end
function Polygon:clip(x1, y1, x2, y2)
local vertices = self.vertices
self.vertices = {}
local x3 = vertices[#vertices - 1]
local y3 = vertices[#vertices]
for i = 1, #vertices, 2 do
local x4 = vertices[i]
local y4 = vertices[i + 1]
if self:isPointInside(x4, y4, x1, y1, x2, y2) then
if not self:isPointInside(x3, y3, x1, y1, x2, y2) then
local x, y = self:intersectLines(x3, y3, x4, y4, x1, y1, x2, y2)
table.insert(self.vertices, x)
table.insert(self.vertices, y)
end
table.insert(self.vertices, x4)
table.insert(self.vertices, y4)
else
if self:isPointInside(x3, y3, x1, y1, x2, y2) then
local x, y = self:intersectLines(x3, y3, x4, y4, x1, y1, x2, y2)
table.insert(self.vertices, x)
table.insert(self.vertices, y)
end
end
x3 = x4
y3 = y4
end
end
function Polygon:isPointInside(x, y, x1, y1, x2, y2)
return (x - x1) * (y2 - y1) - (y - y1) * (x2 - x1) < 0
end
-- See: https://en.wikipedia.org/wiki/Line%E2%80%93line_intersection
function Polygon:intersectLines(x1, y1, x2, y2, x3, y3, x4, y4)
local divisor = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4)
local x = (x1 * y2 - y1 * x2) * (x3 - x4) - (x1 - x2) * (x3 * y4 - y3 * x4)
local y = (x1 * y2 - y1 * x2) * (y3 - y4) - (y1 - y2) * (x3 * y4 - y3 * x4)
return x / divisor, y / divisor
end
-- See: https://en.wikipedia.org/wiki/Centroid#Centroid_of_a_polygon
function Polygon:getArea()
local area = 0
local x1 = self.vertices[#self.vertices - 1]
local y1 = self.vertices[#self.vertices]
for i = 1, #self.vertices, 2 do
local x2 = self.vertices[i]
local y2 = self.vertices[i + 1]
area = area + (x1 * y2 - x2 * y1)
x1 = x2
y1 = y2
end
return 0.5 * area
end
-- See: https://en.wikipedia.org/wiki/Centroid#Centroid_of_a_polygon
function Polygon:getCentroid()
local centroidX = 0
local centroidY = 0
local area = 0
local x1 = self.vertices[#self.vertices - 1]
local y1 = self.vertices[#self.vertices]
for i = 1, #self.vertices, 2 do
local x2 = self.vertices[i]
local y2 = self.vertices[i + 1]
local z = (x1 * y2 - x2 * y1)
centroidX = centroidX + (x1 + x2) * z
centroidY = centroidY + (y1 + y2) * z
area = area + z
x1 = x2
y1 = y2
end
local scale = 1 / (3 * area)
return scale * centroidX, scale * centroidY, 0.5 * area
end
function Polygon:containsPoint(x, y)
local x1 = self.vertices[#self.vertices - 1]
local y1 = self.vertices[#self.vertices]
for i = 1, #self.vertices, 2 do
local x2 = self.vertices[i]
local y2 = self.vertices[i + 1]
if cross2(x - x1, y - y1, x2 - x1, y2 - y1) > 0 then
return false
end
x1 = x2
y1 = y2
end
return true
end
return empower