Skip to content

Commit f61d0a7

Browse files
committed
Improve handling of detecting intersections across multiple rings
1 parent a9a224b commit f61d0a7

File tree

8 files changed

+58
-8
lines changed

8 files changed

+58
-8
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 1.2.0
2+
- Improve handling of detecting intersections across multiple rings
3+
14
## 1.1.2
25
- Fixed up some typos in the documentation.
36

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "sweepline-intersections",
3-
"version": "1.1.2",
3+
"version": "1.2.0",
44
"description": "A module to check if a polygon self-intersects using a sweepline algorithm",
55
"main": "dist/sweeplineIntersections.js",
66
"module": "dist/sweeplineIntersections.esm.js",

src/Event.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11

22
export default class Event {
33

4-
constructor (p) {
4+
constructor (p, ringId) {
55
this.p = {
66
x: p[0],
77
y: p[1]
88
}
9+
this.ringId = ringId
910

1011
this.otherEvent = null
1112
this.isLeftEndpoint = null

src/fillQueue.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ export default function fillEventQueue (geojson, eventQueue) {
1111
processFeature(geojson, eventQueue)
1212
}
1313
}
14+
15+
let ringId = 0
1416
function processFeature (featureOrGeometry, eventQueue) {
1517
const geom = featureOrGeometry.type === 'Feature' ? featureOrGeometry.geometry : featureOrGeometry
1618
let coords = geom.coordinates
@@ -22,12 +24,12 @@ function processFeature (featureOrGeometry, eventQueue) {
2224
for (let ii = 0; ii < coords[i].length; ii++) {
2325
let currentP = coords[i][ii][0]
2426
let nextP = null
25-
27+
ringId = ringId + 1
2628
for (let iii = 0; iii < coords[i][ii].length - 1; iii++) {
2729
nextP = coords[i][ii][iii + 1]
2830

29-
const e1 = new Event(currentP)
30-
const e2 = new Event(nextP)
31+
const e1 = new Event(currentP, ringId)
32+
const e2 = new Event(nextP, ringId)
3133

3234
e1.otherEvent = e2
3335
e2.otherEvent = e1

src/utils.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ export function areCoordsSame (p1, p2) {
55
export function testSegmentIntersect (seg1, seg2) {
66
if (seg1 === null || seg2 === null) return false
77

8-
if (seg1.rightSweepEvent.isSamePoint(seg2.leftSweepEvent) ||
8+
if (seg1.leftSweepEvent.ringId === seg2.leftSweepEvent.ringId &&
9+
(seg1.rightSweepEvent.isSamePoint(seg2.leftSweepEvent) ||
10+
seg1.rightSweepEvent.isSamePoint(seg2.leftSweepEvent) ||
911
seg1.rightSweepEvent.isSamePoint(seg2.rightSweepEvent) ||
1012
seg1.leftSweepEvent.isSamePoint(seg2.leftSweepEvent) ||
11-
seg1.leftSweepEvent.isSamePoint(seg2.rightSweepEvent)) return false
13+
seg1.leftSweepEvent.isSamePoint(seg2.rightSweepEvent))) return false
1214

1315
const x1 = seg1.leftSweepEvent.p.x
1416
const y1 = seg1.leftSweepEvent.p.y

test/fixtures/notSimple/chileKinked.geojson

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"type": "Feature",
3+
"properties": {
4+
"expectedIntersections": 3
5+
},
6+
"geometry": {
7+
"type": "MultiLineString",
8+
"coordinates": [[
9+
[7, 50],
10+
[8, 50],
11+
[9, 50]
12+
],
13+
[
14+
[8, 49],
15+
[8, 50],
16+
[8, 51]
17+
]]
18+
}
19+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"type": "Feature",
3+
"properties": {
4+
"expectedIntersections": 2
5+
},
6+
"geometry": {
7+
"type": "MultiLineString",
8+
"coordinates": [[
9+
[120, -20],
10+
[122.5, -16],
11+
[125, -15],
12+
[127.5, -16],
13+
[130, -20]
14+
],[
15+
[120, -20],
16+
[122.5, -24],
17+
[125, -25],
18+
[127.5, -24],
19+
[130, -20]
20+
]
21+
]
22+
}
23+
}

0 commit comments

Comments
 (0)