-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathBooleanPointOnLine.swift
More file actions
46 lines (35 loc) · 1.65 KB
/
BooleanPointOnLine.swift
File metadata and controls
46 lines (35 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#if canImport(CoreLocation)
import CoreLocation
#endif
import Foundation
// Ported from https://github.com/Turfjs/turf/blob/master/packages/turf-boolean-point-on-line
// and from https://github.com/Turfjs/turf/blob/master/packages/turf-point-on-feature
extension LineSegment {
/// Tests if *Coordinate3D* is on the segment.
public func checkIsOnSegment(_ coordinate: Coordinate3D) -> Bool {
let coordinate = coordinate.projected(to: projection)
let ab = sqrt((second.longitude - first.longitude) * (second.longitude - first.longitude)
+ (second.latitude - first.latitude) * (second.latitude - first.latitude))
let ap = sqrt((coordinate.longitude - first.longitude) * (coordinate.longitude - first.longitude)
+ (coordinate.latitude - first.latitude) * (coordinate.latitude - first.latitude))
let pb = sqrt((second.longitude - coordinate.longitude) * (second.longitude - coordinate.longitude)
+ (second.latitude - coordinate.latitude) * (second.latitude - coordinate.latitude))
return abs(ab - (ap + pb)) < GISTool.equalityDelta
}
/// Tests if *Point* is on the segment.
public func checkIsOnSegment(_ point: Point) -> Bool {
checkIsOnSegment(point.coordinate)
}
}
extension LineStringGeometry {
/// Tests if *Coordinate3D* is on the segment.
public func checkIsOnLine(_ coordinate: Coordinate3D) -> Bool {
lineSegments.contains { (segment) in
segment.checkIsOnSegment(coordinate)
}
}
/// Tests if *Point* is on the segment.
public func checkIsOnLine(_ point: Point) -> Bool {
checkIsOnLine(point.coordinate)
}
}