gpt4 book ai didi

ios - 线路交叉点 - GPS 坐标

转载 作者:行者123 更新时间:2023-11-30 11:44:24 26 4
gpt4 key购买 nike

我需要检测两条线之间是否有交集。线是一对CLLocationCooperative2D。我找到了这个https://www.movable-type.co.uk/scripts/latlong.html#intersection并尝试在 swift 中实现它

static func intersection(point1: CLLocationCoordinate2D, bearing1: Double, point2: CLLocationCoordinate2D, bearing2: Double) -> CLLocationCoordinate2D {
let lat1 = Math.radians(degrees: point1.latitude)
let lon1 = Math.radians(degrees: point1.longitude)

let lat2 = Math.radians(degrees: point2.latitude)
let lon2 = Math.radians(degrees: point2.longitude)

let toSqrt = pow(sin((lat2 - lat1) / 2), 2) + cos(lat1) * cos(lat2) * pow(sin((lon2 - lon1) / 2), 2)
let dist12 = 2 * asin(sqrt(toSqrt))

let a = acos((sin(lat2) - sin(lat1) * cos(dist12)) / (sin(dist12) * cos(lat1)))
let b = acos((sin(lat1) - sin(lat2) * cos(dist12)) / (sin(dist12) * cos(lat2)))

let o12: Double
let o21: Double

if sin(lon2 - lon1) > 0 {
o12 = a
o21 = 2.0 * .pi - b
} else {
o12 = 2.0 * .pi - a
o21 = b
}

let alpha1 = bearing1 - o12
let alpha2 = o21 - bearing2

let alpha3 = acos(-cos(alpha1) * cos(alpha2) + sin(alpha1) * sin(alpha2) * cos(dist12))

let dist13 = atan2(sin(dist12) * sin(alpha1) * sin(alpha2), cos(alpha2) + cos(alpha1) * cos(alpha3))

let lat3 = asin(sin(lat1) * cos(dist13) + cos(lat1) * sin(dist12) * cos(bearing1))

let deltaLon13 = atan2(sin(bearing1) * sin(dist12) * cos(lat1), cos(dist12) - sin(lat1) * sin(lat3))

let lon3 = lon1 + deltaLon13

return CLLocationCoordinate2D(latitude: Math.degrees(radians: lat3), longitude: Math.degrees(radians: lon3))
}

不幸的是,它没有按预期工作。这是示例输出: enter image description here

有没有简单的方法来检查这些线是否相交?我什至不需要交点。

最佳答案

我发现执行中存在错误。应该是:

    let lat3 = asin(sin(lat1) * cos(dist13) + cos(lat1) * sin(dist13) * cos(bearing1))

let deltaLon13 = atan2(sin(bearing1) * sin(dist13) * cos(lat1), cos(dist13) - sin(lat1) * sin(lat3))

关于ios - 线路交叉点 - GPS 坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48997641/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com