Find Angle between 2 polylines

522 Views Asked by At

I am drawing MKPolyLines between 2 sets of coordinates on a MKPolyLineView in MkMapView. One line is the actual route travelled from one point to the other, while the other line is the shortest distance between the 2 points. I have successfully drawn the 2 sets of lines. Now I need to find the angle with direction in between the 2 lines. Despite my efforts I have not been able to come up with anything helpful. Help needed.

This is how I have drawn polylines.

self.routeLine = [MKPolyline polylineWithCoordinates:pointsToUse count:[array count]];
self.straightLine = [MKPolyline polylineWithCoordinates:straightLinePoints count:2];

[self.map addOverlay:self.routeLine];
[self.map addOverlay:self.straightLine];

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
if(overlay == self.routeLine){
// ylineView *polyLineView = [[MKPolylineView alloc] initWithPolyline:self.polyLine];
polyLineView = [[MKPolylineView alloc] initWithPolyline:self.routeLine];
polyLineView.lineWidth = 2;
polyLineView.strokeColor = [UIColor lightGrayColor];
polyLineView.backgroundColor = [UIColor grayColor];
return polyLineView;
}
else{
    polyLineView = [[MKPolylineView alloc] initWithPolyline:self.straightRouteLine];
    polyLineView.lineWidth = 2;
    polyLineView.strokeColor = [UIColor redColor];
    polyLineView.backgroundColor = [UIColor redColor];
    return polyLineView;
}
}
1

There are 1 best solutions below

0
On

I wrote some code that does just this in Python. I can post the code later when I get home, but it looks something like this:

angle = atan2(y2 - y1, x2 - x1) * 180 / PI;