IT story

다른 점을 기준으로 점 회전 (2D)

hot-time 2020. 7. 12. 09:30
반응형

다른 점을 기준으로 점 회전 (2D)


카드가 펼쳐지는 곳에서 카드 게임을 만들려고합니다. 현재 기능을 가진 Allegro API를 사용하여 Im을 표시하려면 다음을 수행하십시오.

al_draw_rotated_bitmap(OBJECT_TO_ROTATE,CENTER_X,CENTER_Y,X
        ,Y,DEGREES_TO_ROTATE_IN_RADIANS);

이것으로 팬 효과를 쉽게 만들 수 있습니다. 문제는 마우스 아래에있는 카드를 아는 것입니다. 이를 위해 다각형 충돌 테스트를 생각했습니다. 카드의 4 포인트를 회전시켜 다각형을 구성하는 방법을 잘 모르겠습니다. 기본적으로 Allegro와 동일한 작업을 수행해야합니다.

예를 들어, 카드의 4 포인트는 다음과 같습니다.

card.x

card.y

card.x + card.width

card.y + card.height

다음과 같은 기능이 필요합니다.

POINT rotate_point(float cx,float cy,float angle,POINT p)
{
}

감사


먼저 피벗 점을 빼고 (cx,cy)회전 한 다음 점을 다시 추가하십시오.

테스트되지 않은 :

POINT rotate_point(float cx,float cy,float angle,POINT p)
{
  float s = sin(angle);
  float c = cos(angle);

  // translate point back to origin:
  p.x -= cx;
  p.y -= cy;

  // rotate point
  float xnew = p.x * c - p.y * s;
  float ynew = p.x * s + p.y * c;

  // translate point back:
  p.x = xnew + cx;
  p.y = ynew + cy;
  return p;
}

각도 세타별로 점을 (px, py)중심으로 점 을 회전하면 다음과 같은 결과가 나타 (ox, oy)납니다.

p'x = cos(theta) * (px-ox) - sin(theta) * (py-oy) + ox

p'y = sin(theta) * (px-ox) + cos(theta) * (py-oy) + oy

이것은 포인트를 2D로 회전시키는 쉬운 방법입니다.


화면의 좌표계는 왼손잡이입니다. 즉, x 좌표가 왼쪽에서 오른쪽으로 증가 하고 y 좌표가 위에서 아래로 증가합니다. 원점 O (0, 0)는 화면의 왼쪽 상단에 있습니다.

여기에 이미지 설명을 입력하십시오

시계 회전 원점 주위 이하의 식으로 주어진다 좌표 (x, y)를 갖는 점 :

여기에 이미지 설명을 입력하십시오

여기서 (x ', y')는 회전 후 점과 각도 세타의 좌표, 회전 각도 (라디안이어야 함, 즉 PI / 180을 곱해야 함)입니다.

To perform rotation around a point different from the origin O(0,0), let's say point A(a, b) (pivot point). Firstly we translate the point to be rotated, i.e. (x, y) back to the origin, by subtracting the coordinates of the pivot point, (x - a, y - b). Then we perform the rotation and get the new coordinates (x', y') and finally we translate the point back, by adding the coordinates of the pivot point to the new coordinates (x' + a, y' + b).

Following the above description:

a 2D clockwise theta degrees rotation of point (x, y) around point (a, b) is:

Using your function prototype: (x, y) -> (p.x, p.y); (a, b) -> (cx, cy); theta -> angle:

POINT rotate_point(float cx, float cy, float angle, POINT p){

     return POINT(cos(angle) * (p.x - cx) - sin(angle) * (p.y - cy) + cx,
                  sin(angle) * (p.x - cx) + cos(angle) * (p.y - cy) + cy);
}

float s = sin(angle); // angle is in radians
float c = cos(angle); // angle is in radians

For clockwise rotation :

float xnew = p.x * c + p.y * s;
float ynew = -p.x * s + p.y * c;

For counter clockwise rotation :

float xnew = p.x * c - p.y * s;
float ynew = p.x * s + p.y * c;

참고 URL : https://stackoverflow.com/questions/2259476/rotating-a-point-about-another-point-2d

반응형