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
Release: 2024-12-08 00:02:13
Original
227 people have browsed it

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;
    Copy after login
  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);
    Copy after login
  3. Translate the point back by adding (cx, cy):

    p.x = xnew + cx;
    p.y = ynew + cy;
    Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template