--!strict -- codecs/model/Polygon: 2D polygons with holes to triangles, for formats that store regions as outlines (Doom map -- sectors, ...). Loops are classified by nesting (a loop inside an odd number of other loops is a hole of the -- smallest loop around it), oriented, each hole bridged to its outer loop at a mutually visible vertex, then the -- result is ear-clipped (reflex-vertex test; a polygon that never yields a clean ear clips its least bad corner so -- every input terminates). New in formats-luau, after the classic "ear clipping with hole bridging" method -- (D. Eberly, Triangulation by Ear Clipping); no code taken. -- -- local tris = Polygon.triangulate({ { 0, 0, 10, 0, 10, 10, 0, 10 }, { 2, 2, 4, 2, 4, 4, 2, 4 } }) -- -- tris = { loop, index, loop, index, loop, index, ... }: three corners per triangle, each a (loop, vertex) pair -- -- (1-based), counter-clockwise -- -- Never throws; degenerate loops (fewer than 3 points, zero area) are ignored. local Polygon = {} type Pt = { x: number, y: number, loop: number, index: number } local function area(pts: { Pt }): number local a = 0 local n = #pts for i = 1, n do local p, q = pts[i], pts[i % n + 1] a += p.x * q.y - q.x * p.y end return a / 2 end local function inside(pts: { Pt }, x: number, y: number): boolean local c = false local n = #pts local j = n for i = 1, n do local a, b = pts[i], pts[j] if (a.y > y) ~= (b.y > y) and x < (b.x - a.x) * (y - a.y) / (b.y - a.y) + a.x then c = not c end j = i end return c end local function cross(o: Pt, a: Pt, b: Pt): number return (a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x) end -- segments p1-p2 and q1-q2 cross properly (touching at an endpoint does not count) local function crosses(p1: Pt, p2: Pt, q1: Pt, q2: Pt): boolean if (p1.x == q1.x and p1.y == q1.y) or (p1.x == q2.x and p1.y == q2.y) or (p2.x == q1.x and p2.y == q1.y) or (p2.x == q2.x and p2.y == q2.y) then return false end local d1, d2 = cross(q1, q2, p1), cross(q1, q2, p2) local d3, d4 = cross(p1, p2, q1), cross(p1, p2, q2) return ((d1 > 0 and d2 < 0) or (d1 < 0 and d2 > 0)) and ((d3 > 0 and d4 < 0) or (d3 < 0 and d4 > 0)) end local function visible(m: Pt, v: Pt, rings: { { Pt } }): boolean for _, r in rings do local n = #r for i = 1, n do if crosses(m, v, r[i], r[i % n + 1]) then return false end end end return true end -- splice hole h into ring outer (both oriented: outer counter-clockwise, hole clockwise) at mutually visible -- vertices; others = every ring the bridge must not cross local function bridge(outer: { Pt }, h: { Pt }, others: { { Pt } }): { Pt } local mi = 1 for i, p in h do if p.x > h[mi].x then mi = i end end local m = h[mi] local best, bestD = 0, math.huge for pass = 1, 2 do for i, v in outer do if pass == 2 or v.x >= m.x then local d = (v.x - m.x) ^ 2 + (v.y - m.y) ^ 2 if d < bestD and visible(m, v, others) then best, bestD = i, d end end end if best > 0 then break end end if best == 0 then best = 1 end local out: { Pt } = {} for i = 1, best do table.insert(out, outer[i]) end local n = #h for k = 0, n do table.insert(out, h[(mi - 1 + k) % n + 1]) end for i = best, #outer do table.insert(out, outer[i]) end return out end local function inTri(a: Pt, b: Pt, c: Pt, p: Pt): boolean return cross(a, b, p) >= 0 and cross(b, c, p) >= 0 and cross(c, a, p) >= 0 end -- ear clipping of one counter-clockwise ring local function clip(ring: { Pt }, out: { number }) local pts = table.clone(ring) local guard = #pts * #pts + 16 while #pts > 3 and guard > 0 do guard -= 1 local n = #pts local found, bestI, bestScore = 0, 1, -math.huge for i = 1, n do local a, b, c = pts[(i - 2) % n + 1], pts[i], pts[i % n + 1] local cr = cross(a, b, c) if cr > 0 then local ok = true for k = 1, n do local p = pts[k] if p ~= a and p ~= b and p ~= c and not (p.x == b.x and p.y == b.y) and not (p.x == a.x and p.y == a.y) and not (p.x == c.x and p.y == c.y) and inTri(a, b, c, p) then ok = false break end end if ok then found = i break end end if cr > bestScore then bestI, bestScore = i, cr end end local i = if found > 0 then found else bestI local a, b, c = pts[(i - 2) % n + 1], pts[i], pts[i % n + 1] if cross(a, b, c) ~= 0 then table.insert(out, a.loop) table.insert(out, a.index) table.insert(out, b.loop) table.insert(out, b.index) table.insert(out, c.loop) table.insert(out, c.index) end table.remove(pts, i) end if #pts == 3 and cross(pts[1], pts[2], pts[3]) > 0 then for k = 1, 3 do table.insert(out, pts[k].loop) table.insert(out, pts[k].index) end end end -- triangulate(loops) -> { loop, index, ... }: loops[i] = { x1, y1, x2, y2, ... } (closed implicitly) function Polygon.triangulate(loops: { { number } }): { number } local rings: { { Pt } } = {} local areas: { number } = {} for li, l in loops do local pts: { Pt } = {} for k = 1, #l - 1, 2 do local x, y = l[k], l[k + 1] if type(x) == "number" and type(y) == "number" and x == x and y == y then table.insert(pts, { x = x, y = y, loop = li, index = (k + 1) // 2 }) end end local a = if #pts >= 3 then area(pts) else 0 if a ~= 0 then table.insert(rings, pts) table.insert(areas, math.abs(a)) if a < 0 then -- counter-clockwise for now; holes are flipped below local r: { Pt } = {} for k = #pts, 1, -1 do table.insert(r, pts[k]) end rings[#rings] = r end end end -- nesting: the parent of a ring is the smallest ring containing its first point local n = #rings local depth, parent = table.create(n, 0), table.create(n, 0) for i = 1, n do local p = rings[i][1] for j = 1, n do if j ~= i and areas[j] > areas[i] and inside(rings[j], p.x, p.y) then depth[i] += 1 if parent[i] == 0 or areas[j] < areas[parent[i]] then parent[i] = j end end end end local holes: { [number]: { number } } = {} for i = 1, n do if depth[i] % 2 == 1 and parent[i] > 0 then local list = holes[parent[i]] or {} holes[parent[i]] = list table.insert(list, i) end end local out: { number } = {} for i = 1, n do if depth[i] % 2 == 0 then local ring = rings[i] local hs = holes[i] if hs then table.sort(hs, function(a: number, b: number): boolean local ma, mb = -math.huge, -math.huge for _, p in rings[a] do ma = math.max(ma, p.x) end for _, p in rings[b] do mb = math.max(mb, p.x) end return ma > mb end) local flipped: { { Pt } } = {} for _, h in hs do local r: { Pt } = {} local src = rings[h] for k = #src, 1, -1 do table.insert(r, src[k]) end table.insert(flipped, r) end for k, h in flipped do local others: { { Pt } } = { ring } for k2 = k + 1, #flipped do table.insert(others, flipped[k2]) end ring = bridge(ring, h, others) end end clip(ring, out) end end return out end return Polygon