📐 adding support for holes in polygon triangulation

This commit is contained in:
Michael Straßburger 2016-10-03 20:01:30 +02:00
parent c0da2871d6
commit e841d634f9

View File

@ -64,9 +64,18 @@ module.exports = class Canvas
point = @_project x, y
@buffer.setBackground point[0], point[1], color
# TODO: support for polygon holes
polygon: (points, color) ->
polygon: (polylines, color) ->
vertices = []
holes = []
xs = {}
ys = {}
for points in polylines
if vertices.length
continue
holes.push vertices.length/2
lastPoint = [-1, -1]
for point in points
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]
vertices = vertices.concat point[0], point[1]
xs[point[0]] = ys[point[1]] = true
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) ->
[vertices[pointId*2], vertices[pointId*2+1]]