Question

I am working on a project involving iOS 7 UIDyanmics and and UIPushBehavior. As part of this I am trying to calculate the angle (expressed in radians) of an object based on two CGPoints. In my research I've found this answer but its expressed in Java.

Find the Angle between two Bearings

If someone has the Objective-C equivalent that would be fantastic.

Was it helpful?

Solution

Given two points...

#include <math.h>
CGPoint p0;
CGPoint p1;

The angle is the arctan of the quotient of the change in each dimension...

double dx = p0.x-p1.x;
double dy = p0.y-p1.y;

double angle = (dx)? atan(dy/dx) : NAN;

FYI, for next time, the SO culture strongly encourages questioners to show research, produce an attempt at a solution.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top