Algorithm Implementation and Application of Point Rotation Around Arbitrary Center in 2D Space

Nov 23, 2025 · Programming · 14 views · 7.8

Keywords: 2D Rotation | Geometric Transformation | Game Development | Collision Detection | C++ Programming

Abstract: This paper thoroughly explores the mathematical principles and programming implementation of point rotation around an arbitrary center in 2D space. By analyzing the derivation process of rotation matrices, it explains in detail the three-step operation strategy of translation-rotation-inverse translation. Combining practical application scenarios in card games, it provides complete C++ implementation code and discusses specific application methods in collision detection. The article also compares performance differences among different implementation approaches, offering systematic solutions for geometric transformation problems in game development.

Mathematical Foundation of Rotation Algorithm

In two-dimensional coordinate systems, point rotation operations are based on rotation matrix theory in linear algebra. For a point P(x,y) rotating counterclockwise by angle θ around center point C(cx,cy), the mathematical principle can be decomposed into three fundamental steps.

First, translate the coordinate system to move the rotation center to the origin: P' = (x-cx, y-cy). This step simplifies the complex problem to standard rotation around the origin, laying the foundation for subsequent calculations.

Second, apply the standard 2D rotation matrix for transformation. The rotation matrix is defined as:

R(θ) = [cosθ  -sinθ]
        [sinθ   cosθ]

The transformed new coordinates are: P'' = (x'·cosθ - y'·sinθ, x'·sinθ + y'·cosθ).

Finally, restore the point to the original coordinate system through inverse translation: P''' = (x''+cx, y''+cy). This stepwise approach is not only mathematically rigorous but also possesses clear logical structure in programming implementation.

Algorithm Implementation Details

Based on the above mathematical principles, we design the following C++ implementation:

struct POINT {
    float x;
    float y;
};

POINT rotate_point(float cx, float cy, float angle, POINT p) {
    // Calculate trigonometric function values
    float s = sin(angle);
    float c = cos(angle);
    
    // Step 1: Translate to origin
    float translated_x = p.x - cx;
    float translated_y = p.y - cy;
    
    // Step 2: Apply rotation matrix
    float rotated_x = translated_x * c - translated_y * s;
    float rotated_y = translated_x * s + translated_y * c;
    
    // Step 3: Inverse translation to restore coordinate system
    POINT result;
    result.x = rotated_x + cx;
    result.y = rotated_y + cy;
    
    return result;
}

This implementation strictly follows the three steps of mathematical derivation, with clear code structure that is easy to understand and maintain. In practical applications, attention must be paid to the unit of the angle parameter to ensure consistency with trigonometric function requirements.

Application in Card Games

In the fan-out effect of card games, each card can be modeled as a rectangular polygon. By calculating the rotated positions of the four corner points of the card, precise collision detection areas can be constructed.

In specific implementation, first define the original boundaries of the card:

POINT corners[4] = {
    {card.x, card.y},
    {card.x + card.width, card.y},
    {card.x + card.width, card.y + card.height},
    {card.x, card.y + card.height}
};

Then apply the rotation function to each corner point:

POINT rotated_corners[4];
for(int i = 0; i < 4; i++) {
    rotated_corners[i] = rotate_point(center_x, center_y, angle, corners[i]);
}

The four rotated points form the precise polygonal boundary of the card, which can be used for mouse click detection. This method is more accurate than simple detection based on bounding boxes, especially in scenarios where cards are densely arranged.

Performance Optimization and Alternative Approaches

Although the above implementation has clear logic, optimization can be considered in performance-sensitive scenarios. A common optimization is to precompute and cache trigonometric function values to avoid repeated calculations in loops:

// Precomputation
float s = sin(angle);
float c = cos(angle);

// Batch process multiple points
for(auto& point : points) {
    float tx = point.x - cx;
    float ty = point.y - cy;
    point.x = tx * c - ty * s + cx;
    point.y = tx * s + ty * c + cy;
}

Another compact implementation combines the three steps into a single-line expression:

p'x = cosθ * (px-cx) - sinθ * (py-cy) + cx
p'y = sinθ * (px-cx) + cosθ * (py-cy) + cy

Although this form has more concise code, its readability is slightly poorer, making it suitable for use when the algorithm principles are fully understood.

Practical Application Considerations

In game development practice, several key points require special attention. First is the consistency of the coordinate system, ensuring all calculations are performed in the same coordinate system. Second is the issue of floating-point precision, which may generate cumulative errors during extensive calculations.

For collision detection, in addition to point rotation, the combined effects of other geometric transformations need to be considered. In actual card games, it may also be necessary to handle composite transformations such as scaling and translation, requiring the establishment of a complete transformation matrix system.

Finally, algorithm selection should be based on specific requirements. Use the complete three-step method in scenarios requiring highest precision, and consider optimized versions in performance-prioritized scenarios. Understanding the advantages and disadvantages of various implementations helps make appropriate choices in different situations.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.