From 50a597fa1de2793747ce70016d0de166162d6eac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Stra=C3=9Fburger?= Date: Wed, 21 Sep 2016 00:50:40 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=93=90=20converting=20polygons=20to=20tri?= =?UTF-8?q?angles=20to=20draw=20them=20filled,=20adapted=20lib?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 3 +++ package.json | 3 ++- termap.coffee | 46 +++++++++++++++++++++++++++++++++------------- 3 files changed, 38 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 01096de..d6a9d94 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,9 @@ No web browser around? No worries - discover the planet in your console! * [x] support for point labels * [x] dynamic decluttering of labels * [ ] centering text labels +* [x] filled polygons + * [x] convert polygons to triangles + * [x] implement fillTriangle into drawille-canvas-blessed-contrib * [ ] lat/lng-center + zoom based viewport * [ ] bbox awareness * [ ] zoom -> scale calculation diff --git a/package.json b/package.json index bf25d5a..e73e2cc 100644 --- a/package.json +++ b/package.json @@ -21,9 +21,10 @@ "license": "MIT", "dependencies": { "coffee-script": "^1.10.0", - "drawille-canvas-blessed-contrib": "^0.1.3", + "drawille-canvas-blessed-contrib": "git+https://github.com/rastapasta/drawille-canvas-blessed-contrib.git", "keypress": "^0.2.1", "pbf": "^3.0.0", + "pnltri": "^2.1.1", "rbush": "^2.0.1", "sphericalmercator": "^1.0.5", "term-mouse": "^0.1.1", diff --git a/termap.coffee b/termap.coffee index 6b8c102..9e002e0 100644 --- a/termap.coffee +++ b/termap.coffee @@ -1,4 +1,4 @@ -Canvas = require 'drawille-canvas-blessed-contrib' +Canvas = require '../drawille-canvas-blessed-contrib' VectorTile = require('vector-tile').VectorTile Protobuf = require 'pbf' keypress = require 'keypress' @@ -7,6 +7,7 @@ zlib = require 'zlib' TermMouse = require 'term-mouse' mercator = new (require('sphericalmercator'))() LabelBuffer = require __dirname+'/src/LabelBuffer' +triangulator = new (require('pnltri')).Triangulator() utils = deg2rad: (angle) -> @@ -215,7 +216,7 @@ class Termap visible = false points = for point in points - p = [point.x/scale, point.y/scale] + p = x: point.x/scale, y: point.y/scale if not visible and @_isOnScreen p visible = true p @@ -223,19 +224,20 @@ class Termap wasDrawn = false switch feature.type - when "polygon", "line" - @canvas.beginPath() - @canvas.moveTo points.shift()... - @canvas.lineTo point... for point in points - @canvas.stroke() + when "line" + @_drawWithLines points + + when "polygon" + unless points.length > 3 and @_drawWithTriangles points + @_drawWithLines points wasDrawn = true when "point" text = feature.properties.house_num or @config.icons[feature.properties.maki] or "◉" for point in points - if labelBuffer.writeIfPossible text, point... - @canvas.fillText text, point... + if labelBuffer.writeIfPossible text, point.x, point.y + @canvas.fillText text, point.x, point.y wasDrawn = true if wasDrawn @@ -248,11 +250,29 @@ class Termap @isDrawing = false + _drawWithTriangles: (points) -> + try + triangles = triangulator.triangulate_polygon [points] + catch + return false + + for triangle in triangles + @canvas.fillTriangle points[triangle[0]], points[triangle[1]], points[triangle[2]] + + true + + _drawWithLines: (points) -> + @canvas.beginPath() + first = points.shift() + @canvas.moveTo first.x, first.y + @canvas.lineTo point.x, point.y for point in points + @canvas.stroke() + _isOnScreen: (point) -> - point[0]+@view[0]>=4 and - point[0]+@view[0]<@width-4 and - point[1]+@view[1]>=0 and - point[1]+@view[1]<@height + point.x+@view[0]>=4 and + point.x+@view[0]<@width-4 and + point.y+@view[1]>=0 and + point.y+@view[1]<@height _write: (text) -> process.stdout.write text