Skip to content

Commit

Permalink
Invert order of calc of segment intersections
Browse files Browse the repository at this point in the history
When the input has two already exactly-overlapping segments with a third
that crosses the two, rounding errors can cause slightly different
intersection points to be computed. It appears that calculating the
intersections in this order avoids these rounding errors from creating
disjoint or extraneous segments (at least for this test case, which is
the only one I know about now... there could be other circumstances
which still fail, in which case a bigger fix will be necessary).

Fixes #19
  • Loading branch information
mfogel committed May 24, 2018
1 parent a9977e2 commit 4ab6557
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ The Martinez-Rueda-Feito polygon clipping algorithm is used to compute the resul

### vNext (in developement)

* Fix bug with overlapping segments ([#19](https://github.com/mfogel/polygon-clipping/issues/19))
* Add benchmarks `npm run bench` ([#15](https://github.com/mfogel/polygon-clipping/issues/15))

### v0.6.1 (2018-04-01)
Expand Down
4 changes: 2 additions & 2 deletions src/sweep-line.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class SweepLine {

// Check for intersections against the previous segment in the sweep line
if (prevSeg) {
const prevInters = segment.getIntersections(prevSeg)
const prevInters = prevSeg.getIntersections(segment)
if (prevInters.length > 0) {
const newEventsFromSplit = this._possibleSplit(prevSeg, prevInters)
for (let i = 0, iMax = newEventsFromSplit.length; i < iMax; i++) {
Expand All @@ -53,7 +53,7 @@ class SweepLine {

// Check for intersections against the next segment in the sweep line
if (nextSeg) {
const nextInters = segment.getIntersections(nextSeg)
const nextInters = nextSeg.getIntersections(segment)
if (nextInters.length > 0) {
const newEventsFromSplit = this._possibleSplit(nextSeg, nextInters)
for (let i = 0, iMax = newEventsFromSplit.length; i < iMax; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
[-69.0043512, 44.296],
[-69.0043512, 44.2966448],
[-69.0045759, 44.2970255],
[-69.0045298581688, 44.297005473122546],
[-69.00452985816878, 44.297005473122546],
[-69.004, 44.298],
[-69.006, 44.296]
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
[
[-89.42, 30.234475],
[-89.306147, 30.1781849],
[-89.30625006083005, 30.234475],
[-89.30625006083004, 30.234475],
[-89.42, 30.234475]
]
]
Expand Down
17 changes: 17 additions & 0 deletions test/end-to-end/parallel-lines-with-crossing/args.geojson
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": null,
"geometry": {
"type": "MultiPolygon",
"coordinates": [
[[[541, -42], [538.88, -41.92], [540.09, -43.74], [541, -42]]],
[[[538, -44], [540.09, -43.74], [538.88, -41.92], [538, -44]]],
[[[541, -44], [539.87, -43.37], [538.73, -44.88], [541, -44]]]
]
}
}
]
}
21 changes: 21 additions & 0 deletions test/end-to-end/parallel-lines-with-crossing/union.geojson
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"type": "Feature",
"properties": null,
"geometry": {
"type": "MultiPolygon",
"coordinates": [
[
[
[538, -44],
[539.538903654485, -43.808557440111905],
[538.73, -44.88],
[541, -44],
[540.1901555424297, -43.548493797991746],
[541, -42],
[538.88, -41.92],
[538, -44]
]
]
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
[
[
[-1.1741342, 50.6250111],
[-0.1, 49.43659688282012],
[-0.1, 49.43659688282011],
[-0.1, 49],
[0.1, 49],
[0.0001, 49.4995],
[0.0001, 49.499500000000005],
[0.0001, 50.6251],
[-1.1741342, 50.6250111]
]
Expand Down

0 comments on commit 4ab6557

Please sign in to comment.