これが、ジョルダン曲線定理のSwiftコードです。CLLocationCoordinate2D
正方形や多角形など、実際に何でも作成できる配列を提供するだけです。
私がこれを翻訳したより詳細な回答:
2Dポイントがポリゴン内にあるかどうかをどのように判断できますか?
元のウェブサイト:PNPOLY-ポリゴンテストのポイントインクルージョンW.ランドルフフランクリン(WRF)
extension CLLocationCoordinate2D {
func liesInsideRegion(region:[CLLocationCoordinate2D]) -> Bool {
var liesInside = false
var i = 0
var j = region.count-1
while i < region.count {
guard let iCoordinate = region[safe:i] else {break}
guard let jCoordinate = region[safe:j] else {break}
if (iCoordinate.latitude > self.latitude) != (jCoordinate.latitude > self.latitude) {
if self.longitude < (iCoordinate.longitude - jCoordinate.longitude) * (self.latitude - iCoordinate.latitude) / (jCoordinate.latitude-iCoordinate.latitude) + iCoordinate.longitude {
liesInside = !liesInside
}
}
i += 1
j = i+1
}
return liesInside
}
}
編集
安全な配列アイテム
extension MutableCollection {
subscript (safe index: Index) -> Iterator.Element? {
get {
guard startIndex <= index && index < endIndex else { return nil }
return self[index]
}
set(newValue) {
guard startIndex <= index && index < endIndex else { print("Index out of range."); return }
guard let newValue = newValue else { print("Cannot remove out of bounds items"); return }
self[index] = newValue
}
}
}