简体   繁体   中英

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+) |
     |
     |

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM