mirror of
https://github.com/rastapasta/mapscii.git
synced 2025-03-22 18:16:38 +01:00
📐 adding support for holes in polygon triangulation
This commit is contained in:
parent
c0da2871d6
commit
e841d634f9
@ -64,9 +64,18 @@ module.exports = class Canvas
|
|||||||
point = @_project x, y
|
point = @_project x, y
|
||||||
@buffer.setBackground point[0], point[1], color
|
@buffer.setBackground point[0], point[1], color
|
||||||
|
|
||||||
# TODO: support for polygon holes
|
polygon: (polylines, color) ->
|
||||||
polygon: (points, color) ->
|
|
||||||
vertices = []
|
vertices = []
|
||||||
|
holes = []
|
||||||
|
|
||||||
|
xs = {}
|
||||||
|
ys = {}
|
||||||
|
|
||||||
|
for points in polylines
|
||||||
|
if vertices.length
|
||||||
|
continue
|
||||||
|
holes.push vertices.length/2
|
||||||
|
|
||||||
lastPoint = [-1, -1]
|
lastPoint = [-1, -1]
|
||||||
for point in points
|
for point in points
|
||||||
point = @_project point[0], point[1]
|
point = @_project point[0], point[1]
|
||||||
@ -75,9 +84,24 @@ module.exports = class Canvas
|
|||||||
|
|
||||||
if point[0] isnt lastPoint[0] or point[1] isnt lastPoint[1]
|
if point[0] isnt lastPoint[0] or point[1] isnt lastPoint[1]
|
||||||
vertices = vertices.concat point[0], point[1]
|
vertices = vertices.concat point[0], point[1]
|
||||||
|
|
||||||
|
xs[point[0]] = ys[point[1]] = true
|
||||||
lastPoint = point
|
lastPoint = point
|
||||||
|
|
||||||
triangles = earcut vertices
|
# Check if we actually got a valid polygon after projection and clamping
|
||||||
|
if Object.keys(xs).length is 1 or Object.keys(ys).length is 1
|
||||||
|
if vertices.length
|
||||||
|
# TODO: a line-hole - skip it for now
|
||||||
|
continue
|
||||||
|
else
|
||||||
|
# TODO: a line instead of a polygon - skip it for now
|
||||||
|
return false
|
||||||
|
|
||||||
|
try
|
||||||
|
triangles = earcut vertices, holes
|
||||||
|
catch e
|
||||||
|
return false
|
||||||
|
|
||||||
extract = (pointId) ->
|
extract = (pointId) ->
|
||||||
[vertices[pointId*2], vertices[pointId*2+1]]
|
[vertices[pointId*2], vertices[pointId*2+1]]
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user