Priyank Gandhi
Priyank Gandhi

Reputation: 1253

Find angle between Two Position

I have Latitude and Longitude of two position.

I already find out distance between this two position.

CLLocation *locA = [[CLLocation alloc] initWithLatitude:[player.strLatitude floatValue] longitude:[player.strLongitude floatValue]];
CLLocation *locB = [[CLLocation alloc] initWithLatitude:app.lat longitude:app.lag];
CLLocationDistance distance = [locB distanceFromLocation:locA];
NSLog(@"Distance%f",distance);
[locA release]; [locB release];

Now i want to find out angle between this two position.

In center there is one user position and i want to display other user location in that Cicle at apporipate angle.

Thanks for help.

enter image description here

Upvotes: 0

Views: 302

Answers (1)

Arthur
Arthur

Reputation: 1760

maybe this will help you

- (CGFloat)angleBetweenLinesInRadians:(CGPoint)line1Start 
                         line1End:(CGPoint)line1End 
                       line2Start:(CGPoint)line2Start 
                         line2End:(CGPoint)line2End 
{
CGFloat a = line1End.x - line1Start.x;
CGFloat b = line1End.y - line1Start.y;
CGFloat c = line2End.x - line2Start.x;
CGFloat d = line2End.y - line2Start.y;

CGFloat line1Slope = (line1End.y - line1Start.y) / (line1End.x - line1Start.x);
CGFloat line2Slope = (line2End.y - line2Start.y) / (line2End.x - line2Start.x);

CGFloat degs = acosf(((a*c) + (b*d)) / ((sqrt(a*a + b*b)) * (sqrt(c*c + d*d))));


return (line2Slope > line1Slope) ? degs : -degs;    

}

Upvotes: 1

Related Questions