그래픽스/DX11

신기한 로테이션 판정 수식

뽀또치즈맛 2025. 11. 28. 22:58


float dy = rc.max.x -rc.min.y;
float dx = rc.min.y - rc.max.x;
float angle_rad = atan2(dy, dx);
float cos_t = cos(angle_rad);
float sin_t = sin(angle_rad);
float center_x = (rc.min.x + rc.max.x) / 2.0f;
float center_y = (rc.min.y + rc.max.y) / 2.0f;

// Left-Top (원래 코드의 Left에 해당할 수 있음) 회전
float old_x_left = rc.min.x;
float old_y_left = rc.max.y;

Left.x = center_x + (old_x_left - center_x) * cos_t - (old_y_left - center_y) * sin_t;
Left.y = center_y + (old_x_left - center_x) * sin_t + (old_y_left - center_y) * cos_t;

// Right-Bottom (원래 코드의 Right에 해당할 수 있음) 회전
float old_x_right = rc.max.x;
float old_y_right = rc.min.y;

Right.x = center_x + (old_x_right - center_x) * cos_t - (old_y_right - center_y) * sin_t;
Right.y = center_y + (old_x_right - center_x) * sin_t + (old_y_right - center_y) * cos_t;

원래는
      float dy = rc.min.y -rc.max.y;
      float dx = rc.max.x - rc.min.x;
이게 정석이지만
결과값은 처음 코드가 맞다.

왜 그런지는 좌표에 기준하긴 하였지만,
절대적인 수식 값에 의한 계산이기 때문이다.

float dy = rc.max.x - rc.min.y;
float dx = rc.min.y - rc.max.x;

이 식은
dx, dy 크기가 같아져서 atan2에서 135° 방향의 대각선(=45° 판정과 사실상 동일한 절대기울기)를 만들어준다.

필자의 바닥판정 렉트가
top 2 buttom 1 right 24 left 0이라면
처음 식대로라면 dy = + 23, dx = -23 으로 수직 대칭을 만들어준다.

정석 판정식으로하면 top과 buttom까지의 차이가 y축은 1밖에 나지 않지만,
위의 수식대로 한다면 45도와 동일한 절대값이 나오게 된다.

실제로 rotation 값은 45도이다.

즉 로테이션 값이 계산 안된 상태에서 rect의 끝점끼리 45도를 내고 싶으면
45도를 판별하기 위의 야매식을 쓰면 된다.

'그래픽스 > DX11' 카테고리의 다른 글

던그리드 D2D11 모작  (0) 2026.01.07
DX - CPU에서 계산된 정보를 GPU로 넘기는 방법  (0) 2025.12.18
D3D11 Phong vs Blinn-Phong  (6) 2025.06.15
D3D11 Resize Viewport  (0) 2025.06.10
HLSL 개요  (2) 2025.06.03