Home >Backend Development >C++ >How do I Rotate a 2D Point Around Another Point?

How do I Rotate a 2D Point Around Another Point?

Barbara Streisand
Barbara StreisandOriginal
2024-12-08 00:02:13228browse

How do I Rotate a 2D Point Around Another Point?

Rotating a Point about Another Point (2D)

To determine the points that define a shape for collision detection, it is necessary to rotate the points around a pivot point. In this case, the pivot point is the center of the shape.

To rotate a point p around another point (cx, cy) by an angle angle in radians, follow these steps:

  1. Translate the point back to the origin by subtracting (cx, cy):

    p.x -= cx;
    p.y -= cy;
  2. Rotate the point counter-clockwise:

    float xnew = p.x * cos(angle) - p.y * sin(angle);
    float ynew = p.x * sin(angle) + p.y * cos(angle);
  3. Translate the point back by adding (cx, cy):

    p.x = xnew + cx;
    p.y = ynew + cy;

Using this method, you can rotate any point around any other point and determine the shape of an object for collision detection.

The above is the detailed content of How do I Rotate a 2D Point Around Another Point?. 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