polygon triangulation
如果你想对一个封闭的区域进行三角剖分
譬如一个省份的行政区域进行三角剖分
譬如你想对一个省份进行三角剖分,但是又需要排除点该省的某一个市的区域 那么你需要用到Earcut
维基百科地址:
https://en.wikipedia.org/wiki/Polygon_triangulation
Usage
/**
* Triangulates the given polygon
*
* @param data is a flat array of vertice coordinates like [x0,y0, x1,y1, x2,y2, ...].
* @param holeIndices is an array of hole indices if any (e.g. [5, 8] for a 12-vertice input would mean one hole with vertices 5–7 and another with 8–11).
* @param dim is the number of coordinates per vertice in the input array
* @return List containing groups of three vertice indices in the resulting array forms a triangle.
*/
var triangles = earcut(double[] data, int[] holeIndices, int dim);
//sample useage for triangulating a polygon
var triangles = earcut([10,0, 0,50, 60,60, 70,10]); // returns [1,0,3, 3,2,1]
// triangulating a polygon with 3d coords
earcut([10,0,1, 0,50,2, 60,60,3, 70,10,4], null, 3);
// triangulating a polygon with a hole
earcut([0,0, 100,0, 100,100, 0,100, 20,20, 80,20, 80,80, 20,80], [4]);
// [3,0,4, 5,4,0, 3,4,7, 5,0,1, 2,3,7, 6,5,1, 2,7,6, 6,1,2]
[4]的意思是这8个顶点中 从第4个开始是hole的顶点,前面的是polygon的顶点
https://github.com/mapbox/earcut (Javascript)
https://github.com/mapbox/earcut.hpp (C++11)