Skip to content

Instantly share code, notes, and snippets.

@nixta
Created January 4, 2017 21:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nixta/b83fe91cb986532c7e1374b089e75c7a to your computer and use it in GitHub Desktop.
Save nixta/b83fe91cb986532c7e1374b089e75c7a to your computer and use it in GitHub Desktop.
Runtime Line Intersection Points
extension AGSPolyline {
func intersectionsWith(line:AGSPolyline) -> [AGSPoint] {
var outPoints = [AGSPoint]()
let threshold = 1E-10
if let lineCut = AGSGeometryEngine.union(ofGeometry1: self, geometry2: line), let lineE = AGSGeometryEngine.intersection(ofGeometry1: self, geometry2: lineCut) as? AGSPolyline {
print("LineE: \(lineE)")
for part in lineE.parts.array() {
for i in 0 ..< part.points.count {
let point = part.points.point(at: i)
let distance = AGSGeometryEngine.distanceBetweenGeometry1(point, geometry2: line)
if distance < threshold {
outPoints.append(point)
}
}
}
}
return outPoints
}
}
let lineAB = AGSPolyline(points: [AGSPointMakeWGS84(0, 0), AGSPointMakeWGS84(10, 10)])
let lineCD = AGSPolyline(points: [AGSPointMakeWGS84(0, 10), AGSPointMakeWGS84(8.5, 7), AGSPointMakeWGS84(4, 6), AGSPointMakeWGS84(10, 0)])
let points = lineAB.intersectionsWith(line: lineCD)
print("Intersections at: \(points)")
@nixta
Copy link
Author

nixta commented Jan 5, 2017

This relies on an observed property of the generated output Polyline that it has vertices wherever it's crossed by the second geometry. In short, I get the coincident segment of the two polylines, which is effectively lineAB but with extra vertices along its length (wherever lineCD crossed it), and return only the vertices that are also on lineCD.

intersection ok

Note: This may produce additional results if lineCD has a segment that is directly aligned with a part or all of lineAB and which has in-line vertices. In the example below, you may not expect to see E4, but the above algorithm will return it.

intersection overlap

It should only be a problem in very specific use cases.

This relates to this GeoNet discussion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment