mirror of
https://github.com/rastapasta/mapscii.git
synced 2024-11-25 01:23:58 +01:00
📐 adding hole support to polygon triangulation
This commit is contained in:
parent
73a56dce9d
commit
f2cbeb465e
@ -44,20 +44,15 @@ module.exports = class Canvas
|
|||||||
background: (x, y, color) ->
|
background: (x, y, color) ->
|
||||||
@buffer.setBackground x, y, color
|
@buffer.setBackground x, y, color
|
||||||
|
|
||||||
polygon: (polylines, color) ->
|
polygon: (rings, color) ->
|
||||||
vertices = []
|
vertices = []
|
||||||
holes = []
|
holes = []
|
||||||
|
|
||||||
xs = {}
|
for ring in rings
|
||||||
ys = {}
|
if vertices.length
|
||||||
#
|
holes.push vertices.length/2
|
||||||
# for points in polylines
|
for point in ring
|
||||||
# if vertices.length
|
vertices = vertices.concat point
|
||||||
# continue
|
|
||||||
# holes.push vertices.length/2
|
|
||||||
polylines.push polylines[0]
|
|
||||||
for point in polylines
|
|
||||||
vertices = vertices.concat point
|
|
||||||
|
|
||||||
try
|
try
|
||||||
triangles = earcut vertices, holes
|
triangles = earcut vertices, holes
|
||||||
|
@ -14,6 +14,8 @@ Styler = require './Styler'
|
|||||||
Tile = require './Tile'
|
Tile = require './Tile'
|
||||||
utils = require './utils'
|
utils = require './utils'
|
||||||
|
|
||||||
|
simplify = require 'simplify-js'
|
||||||
|
|
||||||
module.exports = class Renderer
|
module.exports = class Renderer
|
||||||
cache: {}
|
cache: {}
|
||||||
config:
|
config:
|
||||||
@ -210,16 +212,17 @@ module.exports = class Renderer
|
|||||||
if feature.style.minzoom and tile.zoom < feature.style.minzoom
|
if feature.style.minzoom and tile.zoom < feature.style.minzoom
|
||||||
return false
|
return false
|
||||||
|
|
||||||
points = @_scaleAndReduce tile, feature, feature.points
|
|
||||||
|
|
||||||
switch feature.style.type
|
switch feature.style.type
|
||||||
when "line"
|
when "line"
|
||||||
width = feature.style.paint['line-width']
|
width = feature.style.paint['line-width']
|
||||||
width = width.stops[0][1] if width instanceof Object
|
width = width.stops[0][1] if width instanceof Object
|
||||||
|
|
||||||
|
points = @_scaleAndReduce tile, feature, feature.points
|
||||||
@canvas.polyline points, feature.color, width if points.length
|
@canvas.polyline points, feature.color, width if points.length
|
||||||
|
|
||||||
when "fill"
|
when "fill"
|
||||||
|
points = (@_scaleAndReduce tile, feature, p, false for p in feature.points)
|
||||||
@canvas.polygon points, feature.color
|
@canvas.polygon points, feature.color
|
||||||
# if points.length is 3
|
# if points.length is 3
|
||||||
# @canvas._filledTriangle points[0], points[1], points[2], feature.color
|
# @canvas._filledTriangle points[0], points[1], points[2], feature.color
|
||||||
@ -281,7 +284,13 @@ module.exports = class Renderer
|
|||||||
outside = null
|
outside = null
|
||||||
scaled.push [lastX, lastY]
|
scaled.push [lastX, lastY]
|
||||||
|
|
||||||
scaled.push [x, y]
|
scaled.push [x, y] #x: x, y: y
|
||||||
|
|
||||||
|
if scaled.length < 2
|
||||||
|
if feature.style.type isnt "symbol"
|
||||||
|
return []
|
||||||
|
# else
|
||||||
|
# scaled = ([point.x, point.y] for point in simplify scaled, 2, false)
|
||||||
|
|
||||||
if filter
|
if filter
|
||||||
if scaled.length is 2
|
if scaled.length is 2
|
||||||
@ -291,7 +300,4 @@ module.exports = class Renderer
|
|||||||
|
|
||||||
@_seen[ka] = @_seen[kb] = true
|
@_seen[ka] = @_seen[kb] = true
|
||||||
|
|
||||||
if scaled.length < 2 and feature.style.type isnt "symbol"
|
|
||||||
return []
|
|
||||||
|
|
||||||
scaled
|
scaled
|
||||||
|
@ -74,15 +74,25 @@ class Tile
|
|||||||
# use feature.loadGeometry() again as soon as we got a 512 extent tileset
|
# use feature.loadGeometry() again as soon as we got a 512 extent tileset
|
||||||
geometries = feature.loadGeometry() #@_reduceGeometry feature, 8
|
geometries = feature.loadGeometry() #@_reduceGeometry feature, 8
|
||||||
|
|
||||||
for points in (if style.type is "fill" then [geometries[0]] else geometries)
|
if style.type is "fill"
|
||||||
nodes.push @_addBoundaries
|
nodes.push @_addBoundaries true,
|
||||||
id: feature.id
|
id: feature.id
|
||||||
layer: name
|
layer: name
|
||||||
style: style
|
style: style
|
||||||
properties: feature.properties
|
properties: feature.properties
|
||||||
points: points
|
points: geometries
|
||||||
color: colorCode
|
color: colorCode
|
||||||
|
|
||||||
|
else
|
||||||
|
for points in geometries
|
||||||
|
nodes.push @_addBoundaries false,
|
||||||
|
id: feature.id
|
||||||
|
layer: name
|
||||||
|
style: style
|
||||||
|
properties: feature.properties
|
||||||
|
points: points
|
||||||
|
color: colorCode
|
||||||
|
|
||||||
|
|
||||||
tree = rbush 18
|
tree = rbush 18
|
||||||
tree.load nodes
|
tree.load nodes
|
||||||
@ -91,13 +101,13 @@ class Tile
|
|||||||
|
|
||||||
@layers = layers
|
@layers = layers
|
||||||
|
|
||||||
_addBoundaries: (data) ->
|
_addBoundaries: (deep, data) ->
|
||||||
minX = Infinity
|
minX = Infinity
|
||||||
maxX = -Infinity
|
maxX = -Infinity
|
||||||
minY = Infinity
|
minY = Infinity
|
||||||
maxY = -Infinity
|
maxY = -Infinity
|
||||||
|
|
||||||
for p in data.points
|
for p in (if deep then data.points[0] else data.points)
|
||||||
minX = p.x if p.x < minX
|
minX = p.x if p.x < minX
|
||||||
maxX = p.x if p.x > maxX
|
maxX = p.x if p.x > maxX
|
||||||
minY = p.y if p.y < minY
|
minY = p.y if p.y < minY
|
||||||
|
Loading…
Reference in New Issue
Block a user