Home >Backend Development >C++ >How Do You Rotate a Point Around Another Point in 2D Space?
Rotating a Point around Another Point in Two Dimensions
This article addresses the challenge of calculating the rotation of a point around another point in two dimensions. The task arises in the development of a card game where cards are fanned out. Allegro API's al_draw_rotated_bitmap function facilitates easy card fanning, but it hinders mouse interaction by obscuring the cards underneath. To address this, a polygon collision test is proposed, which requires the rotation of the card's four points.
The solution lies in utilizing a rotation function that performs the following operations:
This function, rotate_point, is implemented as follows:
POINT rotate_point(float cx, float cy, float angleInRads, POINT p) { float s = sin(angleInRads); float c = cos(angleInRads); // 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; }
By implementing this function, the four points of the card can be rotated to determine which card is beneath the mouse, enabling effective mouse interaction in the card game.
The above is the detailed content of How Do You Rotate a Point Around Another Point in 2D Space?. For more information, please follow other related articles on the PHP Chinese website!