A plugin to find self-intersections in a Polygon or MultiPolygon, based on 2d-polygon-self-intersections.
- Install with npm:
npm install maptalks.isects
. - Download from dist directory.
- Use unpkg CDN:
https://unpkg.com/maptalks.isects/dist/maptalks.isects.min.js
As a plugin, maptalks.isects
must be loaded after maptalks.js
in browsers.
<script type="text/javascript" src="https://unpkg.com/maptalks/dist/maptalks.min.js"></script>
<script type="text/javascript" src="https://unpkg.com/maptalks.isects/dist/maptalks.isects.min.js"></script>
<script>
//find intersections of polygon
var polygon = new maptalks.Polygon([
[-10, 0],
[10, 0],
[10, 10],
[1, 10],
[1, -1],
[-1, -1],
[-1, 10],
[-10, 1]
])
.addTo(layer);
var sects = polygon.isects();
//find intersections of MultiPolygon
var multiPolygon = new maptalks.MultiPolygon([
[
[0, 20],
[20, 20],
[20, 30],
[11, 30],
[11, 11],
[9, 11],
[9, 30],
[0, 21]
],
[
[-20, 20],
[0, 20],
[0, 30],
[-9, 30],
[-9, 11],
[-11, 11],
[-11, 30],
[-20, 21]
]
], {
symbol : {
'polygonFill' : '#00f'
}
}).addTo(layer);
var mSects = multiPolygon.isects();
</script>
IE 9-11, Chrome, Firefox, other modern and mobile browsers.
isects
extends Polygon class and MultiPolygon class by adding a new method isects
to find self-intersections.
Find self-intersections
var isects = polygon.isects();
var isects2 = multiPolygon.isects();
Returns Array[]
an array containing ring indexes with self-intersections and intersections' coordinates, e.g.
var polygon = new maptalks.Polygon([
[-10, 0],
[10, 0],
[10, 10],
[1, 10],
[1, -1],
[-1, -1],
[-1, 10],
[-10, 1]
]);
var sects = polygon.isects();
[
//[{ring index}, [{coordinates of intersections}]]
[0, [[1,0], [-1,0]]]
]
//find intersections of MultiPolygon
var multiPolygon = new maptalks.MultiPolygon([
[
[0, 20],
[20, 20],
[20, 30],
[11, 30],
[11, 11],
[9, 11],
[9, 30],
[0, 21]
],
[
[-20, 20],
[0, 20],
[0, 30],
[-9, 30],
[-9, 11],
[-11, 11],
[-11, 30],
[-20, 21]
]
])
var mSects = multiPolygon.isects();
[
//[{polygon index}, [[{ring index}, [{coordinates of intersections}]]]]
[0,[[0,[[11,20],[9,20]]]]],
[1,[[0,[[-9,20],[-11,20]]]]]
]
We welcome any kind of contributions including issue reportings, pull requests, documentation corrections, feature requests and any other helps.
The only source file is index.js
.
It is written in ES6, transpiled by babel and tested with mocha and expect.js.
- Install dependencies
$ npm install
- Watch source changes and generate runnable bundle repeatedly
$ gulp watch
- Tests
$ npm test
- Watch source changes and run tests repeatedly
$ gulp tdd
- Package and generate minified bundles to dist directory
$ gulp minify
- Lint
$ npm run lint