Custom Rotation Function

Start

That’s right, I made this video today mainly to fill in the gaps from the last video. Still, I would recommend you watch the video before reading this blog, as I don’t intend to write too much about it here.

Caveat

  1. math.h and windows.h header files are required to use this function.
  2. If a PI definition is missing or a more precise PI definition is needed, the statement #define PI acos( -1.0 ) can be used directly.
  3. If you don’t know how to convert between radians and angles, see #define D2R(a) (a * (PI / 180)) and #define R2D(a) (a * (180 / PI)), where the former converts angles to radians and the latter converts radians to angles

Main Function

1
2
3
4
5
void GetRotateRectPosition(POINT pt, SIZE sz, float angle, PPOINT ppt){
ppt[0].x = pt.x + sin(angle) * pt.y - cos(angle) * pt.x, ppt[0].y = pt.y - cos(angle) * pt.y - sin(angle) * pt.x;
ppt[1].x = ppt[0].x + cos(angle) * sz.cx, ppt[1].y = ppt[0].y + sin(angle) * sz.cx;
ppt[2].x = ppt[0].x - sin(angle) * sz.cy, ppt[2].y = ppt[0].y + cos(angle) * sz.cy;
}

The parameter pt denotes the centre of rotation, the parameter sz denotes the size of the rectangle, the parameter angle denotes the arc of rotation (in the positive clockwise direction), and the parameter ppt is used to pass the coordinates of the three vertices of the rotated rectangle.

Take An Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <windows.h>
#include <math.h>
#define PI acos( -1.0 )

#define D2R(a) (a * (PI / 180))
#define R2D(a) (a * (180 / PI))

void GetRotateRectPosition(POINT pt, SIZE sz, float angle, PPOINT ppt){
ppt[0].x = pt.x + sin(angle) * pt.y - cos(angle) * pt.x, ppt[0].y = pt.y - cos(angle) * pt.y - sin(angle) * pt.x;
ppt[1].x = ppt[0].x + cos(angle) * sz.cx, ppt[1].y = ppt[0].y + sin(angle) * sz.cx;
ppt[2].x = ppt[0].x - sin(angle) * sz.cy, ppt[2].y = ppt[0].y + cos(angle) * sz.cy;
}

int main(){
int w, h, angle;
POINT pt;
POINT ppt[3];
SIZE sz;
HDC hdc;

for(;;){
hdc = GetDC(0);
w = GetSystemMetrics(0), h = GetSystemMetrics(1);
pt.x = w / 2, pt.y = h / 2;
sz.cx = w, sz.cy = h;
angle = 1;
GetRotateRectPosition(pt, sz, D2R(angle), ppt);
PlgBlt(hdc, ppt, hdc, 0, 0, w, h, 0, 0, 0);
ReleaseDC(0, hdc);
DeleteObject(hdc);
Sleep(100);
}

return 0;
}

Here’s an example where you can see that it’s rotated around the centre of the rectangle, and the results don’t show any surprises, as shown below.
step-0
step-1
step-2

Ending

None Ending…


Custom Rotation Function
http://hopejieshuo.github.io/2023/10/03/Screen-Rotate-New/
Author
hopejieshuo
Posted on
2023-10-03
Licensed under