I draw a rectangle using WinAPI tools. How to rotate:

  POINT pi; // структура точка // Определяю центр прямоугольника pt.x = (начало отрезка + конец отрезка) / 2; pt.y = (начало отрезка по вертикали + конец отрезка) / 2; // координаты хранятся в двух массивах coordinate_x и _y соответственно for (int i(0); i < 4; i++) { // вычитаю из каждой точки центр прямоугольника coordinate_x[i] -= pt.x; coordinate_y[i] -= pt.y; // поворачиваю точку по формулам: // xNew = x * Cos(A) + y * Sin(A) // yNew = y * Cos(A) - x * Sin(A) // _gradus - подаётся в градусах, поэтому конвертирую в радианы int new_x = cos(_gradus * (PI / 180))*coordinate_x[i] - sin(_gradus * (PI / 180)) * coordinate_y[i]; int new_y = sin(_gradus * (PI / 180)) * coordinate_x[i] + cos(_gradus * (PI / 180))*coordinate_y[i]; // прибавляю к повернутой точке центр прямоугольника и записываю обратно в массив coordinate_x[i] = pt.x + new_x; coordinate_y[i] = pt.y + new_y; } 

Already then draw again. But the rotation goes around the top left. Where am I wrong?

  • What is the начало отрезка + конец отрезка ? What is the начало отрезка по вертикали + конец отрезка ? How did you manage to compile it? - AnT
  • pt.x = (coord_left_x + coord_right_x) / 2; pt.y = (coord_y_y + coord_bottom_y) / 2; Here, come? It can be assumed based on the structure of the RECT (rectangle). - Range
  • And now what is coord_left_x , coord_right_x , coord_top_y , coord_bottom_y ? Where do these values ​​come from? If you have nonsense in these values, then everything will rotate around the wrong point. Why is the obviously critical information missing in the question text? - AnT
  • 2
    "But the rotation goes around the left upper point" - the usual sign that the center of rotation was not moved to the origin. Read about the transformation matrix. The rotation matrix is ​​obtained from the sequential multiplication of 3 matrices : "the matrix of the shift of the center of rotation to the origin", "the matrix of rotation by a certain angle" and "the matrix of the reverse shift of the center of rotation". Or you can use a ready-made matrix. - mega
  • 2
    @mega literally an hour ago found an article, read, understood. I did, everything worked, but the rectangle began to lose scale. But, having rummaged, I realized that in one place the float was implicitly reduced to int, hence the loss. Oh, the whole world is built on matrices, and today I was convinced of this .. - Range

1 answer 1

But the rotation goes around the top left.

The usual sign that the center of rotation was not moved to the origin. The easiest way to use the transformation matrix (transition matrix) .

The rotation matrix is obtained from the sequential multiplication of 3 matrices:

  1. rotational center shift matrix to the origin
  2. rotation angle matrix
  3. rotation center shift matrix

Or you can use a ready rotation matrix around an arbitrary point:

In the formulas {x, y} are your initial coordinates of the rectangle (or any other arbitrary shape), and {x*, y*} are the final coordinates of an already rotated shape around the point {m, n} . The angle is given in radians.

The pictures are a simplified formula. In fact, the final coordinates will be in the vector {x*, y*, h} : instead of one, the additional component is h . Usual Cartesian coordinates from this vector can be obtained as: {x*/h, y*/h} , i.e. divide all 3 components by h .