0

I need to calculate whether or not a given coordinate is in front of, or behind another coordinate with bearing, and get the distance.

I've been working on this for a full day at this point and don't seem to be able to get a reliable answer, clearly my geometry skills are lacking!

enum RelativePosition {
    case behind(distance: Double)
    case inFront(distance: Double)
    case equal
}

extension CLLocationCoordinate2D {
    func relativeTo(point: CLLocationCoordinate2D, bearing: Double) -> RelativePosition {
        // what should this be?
    }
}

Any help hugely appreciated.

Rich Lowenberg
  • 1,089
  • 1
  • 8
  • 12
  • What is meaning of "in front of" and "behind" for points/coordinates? Could you show a picture with some examples? – MBo Dec 03 '21 at 18:17
  • First calculate the [bearing](https://stackoverflow.com/questions/3809337/calculating-bearing-between-two-cllocationcoordinate2ds) required to get from A to B. Then you have to compare the required bearing with your current bearing (heading). If the absolute value of the difference between the two bearings is <90 then the point is "in front" if is >90 it is "behind" And if is 90 then it is "beside" – Paulw11 Dec 03 '21 at 19:32
  • @MBo once you add the bearing there's a sense of in front or behind. Eg from point A with a bearing of 90, anything east of that point is in front, anything west of it is behind. Make sense? – Rich Lowenberg Dec 03 '21 at 23:07
  • @Paulw11 this is a much better approach than I'd thought of, I'll try that! It doesn't give me the distance from the line but I can work without that. – Rich Lowenberg Dec 03 '21 at 23:07
  • @Rich Lowenberg [Picture](https://i.stack.imgur.com/MJHW9.png) So you have base point A, destination B, and consider point C and D as "in front", and E and F points as behind ones? Or you have not base point at all and look only at pairs like `B;F` ? – MBo Dec 04 '21 at 03:19
  • What do you mean by "the distance from the line"? Do you mean the angle between your heading and the bearing to the point? If the point is in front of you (difference<90) then the angle from your heading to the required heading is `difference`. If the point is behind you then you can take the reciprocal of your current heading (ie if you are heading at 10° then the reciprocal is 190°) and compute the distance based on the difference and the reciprocal – Paulw11 Dec 05 '21 at 11:12
  • My use case here is I'm analyzing racing lines for car races. I'm calculating how far ahead or behind a car is from another car, and using a line perpendicular to the primary car's position to measure that (rather than measuring directly from point to point, which is inaccurate if the racing lines are different). But I think I've got it from here, thank you! @MBo the thing you're missing is there is a bearing given with one of the coordinates, that's what determines in front or behind. 3 pieces of info: a base coordinate, a bearing, and a coordinate to compare to. – Rich Lowenberg Dec 06 '21 at 16:58
  • I still believe that small picture would be useful to make the problem more clear. – MBo Dec 06 '21 at 17:03

0 Answers0