Skip to content

Instantly share code, notes, and snippets.

@ezefranca
Last active April 2, 2023 13:24
Show Gist options
  • Select an option

  • Save ezefranca/ed856a9977ae92de58c47ca5c03a7f8a to your computer and use it in GitHub Desktop.

Select an option

Save ezefranca/ed856a9977ae92de58c47ca5c03a7f8a to your computer and use it in GitHub Desktop.
diferença de angulos objective-c
#define RADIANS_TO_DEGREES(radians) ((radians) * (180.0 / M_PI))
-(float)angleBetweenCoordinates:(CLLocationCoordinate2D)firstCoordinate
toCoordinate:(CLLocationCoordinate2D)secondCoordinate {
float deltaLongitude = secondCoordinate.longitude - firstCoordinate.longitude;
float deltaLatitude = secondCoordinate.latitude - firstCoordinate.latitude;
// Remember that M_PI * .5f = pi / 2
float angle = (M_PI * .5f) - atan(deltaLatitude / deltaLongitude);
// Compare the deltas to determine if the angle is to the right or left
// Remember that longitude is the Y-axis and latitude is the X-axis
// If deltaLongitude is positive, the angle calculated is returned
if (deltaLongitude > 0) {
return RADIANS_TO_DEGREES(angle);
}
// If deltaLongitude is negative, M_PI is added to the angle to find the angle
else if (deltaLongitude < 0) {
return RADIANS_TO_DEGREES(angle + M_PI);
}
// If deltaLatitude is negative, M_PI is returned
else if (deltaLatitude < 0) {
return RADIANS_TO_DEGREES(M_PI);
}
// If the coordinates are the same, return 0
return 0.0f;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment