项目作者: manubb

项目描述 :
Fast and accurate tessellation of planar graphs with convex polygons
高级语言: JavaScript
项目地址: git://github.com/manubb/graph-draw.git
创建时间: 2017-08-03T14:42:41Z
项目社区:https://github.com/manubb/graph-draw

开源协议:MIT License

下载


graph-draw

A JavaScript library for tessellating undirected planar graphs for Node and browsers.

screenshot

The algorithm is designed to avoid local overdraw. A typical non local overdraw (expected) situation:
overdraw

The library can be used for example to draw boundaries or polylines on a Leaflet map using leaflet-pixi-overlay (see the demos).

Demo

A very basic demo.

A polyline on a map (173 edges tessellated in 335 triangles).

French cities boundaries (186722 edges tessellated in 769041 triangles).

A rail network.

Installation

graph-draw is available as a npm package:

  1. npm install graph-draw

In Node:

  1. const graphDraw = require('graph-draw');

In browsers, include one file from the dist directory. (Files with name that contains “bundle” include libtess.)

Usage

  1. const vertices = [[0, 0], [100, 0], [100, 100], [0, 100]];
  2. // coordinates of the vertices
  3. const edges = [[0, 1], [1, 2], [2, 3], [3, 0], [1, 3]];
  4. // each edge is specified with the indices of the linked vertices
  5. // each edge must appear exactly once in the list
  6. // ([0, 1] and [1, 0] are the same edge)
  7. const graph = {vertices, edges};
  8. const strokeWidth = 10;
  9. const polygons = [];
  10. const polygonCallBack = convexPolygon => polygons.push(convexPolygon);
  11. graphDraw(graph, strokeWidth, polygonCallBack);

The polygonCallBack is executed on each polygon of the tessellation. Now, polygons contains a list of convex polygons (which can be easily converted into triangle strips or triangle fans):

  1. [
  2. [[x1, y1], [x2, y2], [x3, y3], [x4, y4]],
  3. [[a1, b1], [a2, b2], [a3, b3]],
  4. ...
  5. ]

Those convex polygons can have between 3 and 8 edges.

Limiting miters

When the angle between two consecutive edges is close to 2π, long miter situations occur. For example:

  1. const vertices = [
  2. [0, -200],
  3. [100 , -100],
  4. [30, -200]
  5. ];
  6. const edges = [
  7. [0, 1],
  8. [1, 2]
  9. ];
  10. const graph = {vertices, edges};
  11. const strokeWidth = 20;
  12. graphDraw(graph, strokeWidth, polygonCallBack);

produces:
miter

To avoid this, graphDraw function accepts a fourth (optional) maxAngle parameter which is an angle between π and 2π. If the angle between two consecutive edges is above maxAngle, the miter will be replaced by two triangles approximating a round join. For example:

  1. graphDraw(graph, strokeWidth, polygonCallBack, Math.PI);

will produce:
round

Sponsors

Thanks to Stadia Maps for providing the Stamen Toner Lite map tiles in our project demos.