Prevents trying to create sub-polygons (triangles) with less than 3 non-collinear points when triangulating a polygon

fixes #55
This commit is contained in:
MartyMaro 2018-02-08 21:45:08 +01:00
parent dca0947077
commit bde26eca15

View File

@ -320,10 +320,12 @@ function Polygon:triangulate()
next, prev = next_idx[current], prev_idx[current]
local p,q,r = vertices[prev], vertices[current], vertices[next]
if isEar(p,q,r, concave) then
triangles[#triangles+1] = newPolygon(p.x,p.y, q.x,q.y, r.x,r.y)
next_idx[prev], prev_idx[next] = next, prev
concave[q] = nil
n_vert, skipped = n_vert - 1, 0
if not areCollinear(p, q, r) then
triangles[#triangles+1] = newPolygon(p.x,p.y, q.x,q.y, r.x,r.y)
next_idx[prev], prev_idx[next] = next, prev
concave[q] = nil
n_vert, skipped = n_vert - 1, 0
end
else
skipped = skipped + 1
assert(skipped <= n_vert, "Cannot triangulate polygon")