1
votes

I have three CGpoint and I would like calculate the angle.

I drawn a little schema :

enter image description here

I tried with this code :

CGPoint u ;
        u.x = 0;
        u.y = - middleRectY;

        CGPoint v ;
        v.x = x1 -  middelRectX;
        v.y = y1 - middleRectY;

        // formule = u.v / ( ||u|| * ||v||)

        double cosa = (double)((u.x * v.x + u.y * v.y)) / sqrt(u.x * u.x + u.y * u.y) * sqrt(v.x * v.x + v.y * v.y);

        // angle en degré

        double angle = (180.0 / M_PI) * acos(cosa);

        // Signe de l'angle

        int sign = (u.x * v.y - u.y * v.x) > 0 ? 1 : -1;

        rectYellow.transform = CGAffineTransformMakeRotation(angle*sign);

But my function return "nan" :/

Thx :)

1
What returns NaN exactly? Which function?WDUK
When I test with NSLog(@"%f",angle) , the console return "nan".VivienCormier

1 Answers

0
votes

I found the problem !

It just a probleme of parenthesis :

double cosa = ((u.x * v.x) + (u.y * v.y)) / (sqrt((u.x * u.x) + (u.y * u.y)) * sqrt((v.x * v.x) + (v.y * v.y)));

I don't understand why ? Because the parentheses aren't necessary for multiplication ...