Home >Backend Development >C++ >How to Rotate a Point Around Another Point in 2D Space?

How to Rotate a Point Around Another Point in 2D Space?

Susan Sarandon
Susan SarandonOriginal
2024-12-09 18:21:10615browse

How to Rotate a Point Around Another Point in 2D Space?

Rotation of a Point around Another Point in 2D Space

In a bid to create a card game where cards fan out, an individual encounters a predicament in determining which card lies beneath the cursor. This quandary demands a means of rotating the card's four vertex points to form a polygon for collision detection.

To facilitate this rotation, the Allegro API provides a convenient function:

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

However, to calculate the rotated points manually, it becomes necessary to devise a function:

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

where (cx, cy) represents the pivot point, angle denotes the counterclockwise rotation angle, and p is the original point to be rotated.

The essence of the solution lies in carrying out the following steps:

  1. Translate the point back to the origin, resulting in p.x -= cx and p.y -= cy.
  2. Perform the rotation utilizing the equations:

    • xnew = p.x * cos(angle) - p.y * sin(angle)
    • ynew = p.x * sin(angle) p.y * cos(angle)
  3. Translate the point back to its original coordinates via p.x = xnew cx and p.y = ynew cy.

By applying this approach, the function can effectively rotate a point around any pivot point on a 2D plane.

The above is the detailed content of How to Rotate a Point Around Another Point in 2D Space?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn