New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Calculation of perpendicular point of a line (p1, p2) and a point (p3) near the line #2
Comments
Hi @phranck, This should be possible with However, This function returns the distance from let p1, p2, p3: Coordinate
let distance = p3.alongTrackDistance(toPath: (start: p1, end: p2))
let bearing = p1.bearing(to: p2)
let pointOnPath = destination(with: distance, bearing: bearing) |
@florianreinhart Yeah, that was my initial thought, too. However, this is more an approximation method than a direct calculation. (-; |
It should be quite accurate on a sphere (which is the model that is used for the calculation). You could try doing the coordinate calculation with |
Yeah, using |
This is key! So, while handling with |
Oh yeah! You need to convert your |
Just to illustrate - This is the code I use for getting the perpendicular point. As mentioned, it works very well, but for lines crossing -180/180. private func closestPointInLine(withStartPoint p1: MKMapPoint, endPoint p2: MKMapPoint, touchPoint pt: MKMapPoint, allowedPerimeter: Double) -> MKMapPoint? {
let lineLength = distance(from: p1, to: p2)
var u = ((pt.x - p1.x) * (p2.x - p1.x)) + ((pt.y - p1.y) * (p2.y - p1.y))
u = u / pow(lineLength, 2)
if u >= 0 && u <= 1 {
let px = p1.x + u * (p2.x - p1.x)
let py = p1.y + u * (p2.y - p1.y)
let p = MKMapPoint(x: px, y: py)
let touchDistance = distance(from: pt, to: p)
if touchDistance <= allowedPerimeter {
return p
}
}
return nil
}
private func distance(from p1: MKMapPoint, to p2: MKMapPoint) -> Double {
return sqrt(pow(p2.x - p1.x, 2) + pow(p2.y - p1.y, 2))
} |
Hey @florianreinhart!
First: Thank you very much for this great collection of useful geodesy functions!
This isn't a real issue but more a question. I'm currently struggling on calculating the perpendicular point of a line (p1, p2) and a point (p3) near the line.
Background: We are using
MKPolyLine
to draw lines on the map. The user should be able to tap (p3) on (or near to) a line segment (p1, p2). Now I need the closest point in the line (the perpendicular point) to the touch point, to show a popup with detailed informations at this line point. It should look like this:Now I'm asking myself if this is possible with your functions? Maybe I'm missing something, unfortunately I don't find a solution...
Question: Is this calculation possible with your library or is an entire new function needed to get the result?
Thanks in advance!
The text was updated successfully, but these errors were encountered: