-
-
Save nixta/b83fe91cb986532c7e1374b089e75c7a to your computer and use it in GitHub Desktop.
Runtime Line Intersection Points
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
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.It should only be a problem in very specific use cases.
This relates to this GeoNet discussion.