Majid Q.
Majid Q.

Reputation: 798

Angle of point around certain point

I have done some investigation on rotating a point at a certain angle from certain point and from question: C++: Rotating a vector around a certain point I have come up with following so far but it is not giving me correct result, can someone please identify the problem

const float angle = -90.0;
const float degreePerRadian = 57.2957795;
const float s = sin(angle / degreePerRadian);
const float c = cos(angle / degreePerRadian);
int p1x = 320;
int p1y = 480;
int p2x = 320;
int p2y = 320;

float centerX = (p2x - p1x) / 2 + p1x;
float centerY = (p2y - p1y) / 2 + p1y;
p2x -= centerX;
p2y -= centerY;
double newX = centerX + (p2x * c - p2y * s);
double newY = centerY + (p2x * s + p2y * c);

I get: newX = 240 and newY = 400

EDIT: I have a weird plane though

                                |
                                |    ^
                                |    |
                                |
       -------> ^ (320, 480)    |   (y+)
            90° |               |    |
                |               |    |
                |               |
                . (320, 320)    |
                                |
                                |
      --------------------------(0,0)
                <-- (x+) --

If and I want to find out 90 degree angle for the point (320, 480) if line falls to the left and to the right

In fact to be more precise the plain is upside down (basically I am using QGraphicsScene so top left is 0,0 and bottom right is width(), height()

                (x+) --->
     (0,0) --------------------------
     |
     |
     |      . (320, 320)
     |      |
     |      |
     |      | 90°
     |      . (320, 480)----------
     |
     |
(y+) |
     |
     |

Upvotes: 0

Views: 243

Answers (1)

MBo
MBo

Reputation: 80107

Your 16th line of code from link is incorrect. It should be:

double newY = centerY + (p2x * s + p2y * c);

Upvotes: 1

Related Questions