When dealing with a multi-touch rotation I was searching for a simple method to calculate the angle of two fingers. Well, actually more precisely the angle between the vector from one finger to the second finger and the horizontal X Axis.

While I was still searching my friend Christian Pfandler came up with this function:

- (CGFloat)angleFromPoints:(CGPoint)pos1 otherPoint:(CGPoint)pos2
{
CGPoint vector = CGPointMake(pos1.x-pos2.x, pos1.y - pos2.y);
double angleCalc;
if (vector.y < 0)
{
// upper Half
angleCalc = atan2(-vector.y,vector.x);
}
else
{
angleCalc = atan2(vector.y,-vector.x)+M_PI;
}
return angleCalc;
} |

The regular atan(x) function seem to have a limitation by only returning values between 0 and +/- PI. Because of this there is also the atan2(y,x) function which is orders of magnitude more useful. To get a continuos angle for the full circle we have to treat the upper half and the lower half seperately though. I found this here.

I don’t quite understand it, but it seems to work. π Please keep in mind though that like all angles in Cocoa Touch this is in radians as well. So not 0…360, but 0…2*PI. If you know an even simpler method, please let me know.

### Like this:

Like Loading...

*Related*

When dealing with a multi-touch rotation I was searching for a simple method to calculate the angle of two fingers. Well, actually more precisely the angle between the vector from...

**Categories: **Recipes