Skip to content

Commit

Permalink
Merge branch 'bugfix/another-set-of-coords-from-60'
Browse files Browse the repository at this point in the history
Part of #60
  • Loading branch information
mfogel committed Mar 26, 2019
2 parents 6ae53d4 + 57b222a commit 67dbfec
Show file tree
Hide file tree
Showing 6 changed files with 262 additions and 115 deletions.
60 changes: 38 additions & 22 deletions src/segment.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,19 +216,42 @@ export default class Segment {
// but avoid using rounder.round for performance boost, and to avoid
// saving the result in the rounding trees

const cmpY = cmp(point.y, interPt.y)
if (cmpY !== 0) return cmpY

// depending on if our segment angles up or down,
// the x coord comparison means oppposite things
const cmpX = cmp(point.x, interPt.x)
if (cmpX < 0) {
if (this.leftSE.point.y < this.rightSE.point.y) return 1
if (this.leftSE.point.y > this.rightSE.point.y) return -1
// also, there is a fair amount of rounding error introduced when computing
// the closestPoint to a nearly vertical or horizontal segment. Thus, we use
// the more accurate coordinate for comparison of the two points

const lx = this.leftSE.point.x
const ly = this.leftSE.point.y
const rx = this.rightSE.point.x
const ry = this.rightSE.point.y

// is the segment upward sloping?
if (ry >= ly) {
// is the segment more vertical?
if (ry - ly > rx - lx) {
// use the X coordinate
const cmpX = cmp(interPt.x, point.x)
if (cmpX != 0) return cmpX
}
else {
// segment is more horizontal, so use Y coord
const cmpY = cmp(point.y, interPt.y)
if (cmpY != 0) return cmpY
}
}
if (cmpX > 0) {
if (this.leftSE.point.y < this.rightSE.point.y) return -1
if (this.leftSE.point.y > this.rightSE.point.y) return 1
else {
// segment is more downward sloping
// is the segment more vertical?
if (ly - ry > rx - lx) {
// use the X coordinate
const cmpX = cmp(point.x, interPt.x)
if (cmpX != 0) return cmpX
}
else {
// segment is more horizontal, so use the Y coordinate
const cmpY = cmp(point.y, interPt.y)
if (cmpY != 0) return cmpY
}
}

// on the line
Expand All @@ -249,11 +272,8 @@ export default class Segment {
*
* If no non-trivial intersection exists, return null
* Else, return null.
*
* The input 'processingPt' is the currnet position of the sweep line pass.
* It is used to avoid snapping to an already-processed endpoint.
*/
getIntersection (other, processingPt) {
getIntersection (other) {
// If bboxes don't overlap, there can't be any intersections
const tBbox = this.bbox()
const oBbox = other.bbox()
Expand All @@ -272,12 +292,8 @@ export default class Segment {
// does each endpoint touch the other segment?
// note that we restrict the 'touching' definition to only allow segments
// to touch endpoints that lie forward from where we are in the sweep line pass
const touchesOtherLSE = (
isInBbox(tBbox, olp) && this.comparePoint(olp) === 0 && SweepEvent.comparePoints(olp, processingPt) >= 0
)
const touchesThisLSE = (
isInBbox(oBbox, tlp) && other.comparePoint(tlp) === 0 && SweepEvent.comparePoints(tlp, processingPt) >= 0
)
const touchesOtherLSE = isInBbox(tBbox, olp) && this.comparePoint(olp) === 0
const touchesThisLSE = isInBbox(oBbox, tlp) && other.comparePoint(tlp) === 0
const touchesOtherRSE = isInBbox(tBbox, orp) && this.comparePoint(orp) === 0
const touchesThisRSE = isInBbox(oBbox, trp) && other.comparePoint(trp) === 0

Expand Down
6 changes: 3 additions & 3 deletions src/sweep-line.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export default class SweepLine {
// Check for intersections against the previous segment in the sweep line
let prevMySplitter = null
if (prevSeg) {
const prevInter = prevSeg.getIntersection(segment, event.point)
const prevInter = prevSeg.getIntersection(segment)
if (prevInter !== null) {
if (!segment.isAnEndpoint(prevInter)) prevMySplitter = prevInter
if (!prevSeg.isAnEndpoint(prevInter)) {
Expand All @@ -82,7 +82,7 @@ export default class SweepLine {
// Check for intersections against the next segment in the sweep line
let nextMySplitter = null
if (nextSeg) {
const nextInter = nextSeg.getIntersection(segment, event.point)
const nextInter = nextSeg.getIntersection(segment)
if (nextInter !== null) {
if (!segment.isAnEndpoint(nextInter)) nextMySplitter = nextInter
if (!nextSeg.isAnEndpoint(nextInter)) {
Expand Down Expand Up @@ -140,7 +140,7 @@ export default class SweepLine {
// since we're about to be removed from the sweep line, check for
// intersections between our previous and next segments
if (prevSeg && nextSeg) {
const inter = prevSeg.getIntersection(nextSeg, event.point)
const inter = prevSeg.getIntersection(nextSeg)
if (inter !== null) {
if (!prevSeg.isAnEndpoint(inter)) {
const newEventsFromSplit = this._splitSafely(prevSeg, inter)
Expand Down
2 changes: 1 addition & 1 deletion test/end-to-end/issue-37/intersection.geojson
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
[
[0.523985, 51.281651],
[0.5241, 51.2816],
[0.5240213684210545, 51.28168736842105],
[0.5240213684210538, 51.28168736842105],
[0.523985, 51.281651]
]
]
Expand Down
23 changes: 23 additions & 0 deletions test/end-to-end/nearly-vertical-segment/args.geojson
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": null,
"geometry": {
"type": "MultiPolygon",
"coordinates": [
[
[
[-9.999999999999996, -2.34],
[-9.999999999999982, 8.84],
[-9.999999999999991, 8],
[-9.999999999999985, -1],
[-9.999999999999996, -2.34]
]
]
]
}
}
]
}
25 changes: 25 additions & 0 deletions test/end-to-end/nearly-vertical-segment/union.geojson
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"type": "Feature",
"properties": null,
"geometry": {
"type": "MultiPolygon",
"coordinates": [
[
[
[-9.999999999999996, -2.34],
[-9.999999999999986, -1],
[-9.999999999999991, 3.806139852188744],
[-9.999999999999996, -2.34]
]
],
[
[
[-9.999999999999991, 3.806139852188744],
[-9.999999999999982, 8.84],
[-9.999999999999991, 8],
[-9.999999999999991, 3.806139852188744]
]
]
]
}
}
Loading

0 comments on commit 67dbfec

Please sign in to comment.