围绕另一个点旋转一个点的函数

发布于 2024-12-05 07:00:34 字数 686 浏览 1 评论 0原文

因此,我在这里有一个数学函数,假定返回一个旋转点,并采用原始点,旋转(原点)旋转的点和弧度将其旋转。

但是,它仅以一半速度旋转(又称180度运动= 90度旋转)

sf::Vector2f RotatePoint(sf::Vector2f origin, sf::Vector2f point, float radian) {   
    float s = sin(radian);   
    float c = cos(radian);  

    // translate point back to origin:  
    point.x -= origin.x;   
    point.y -= origin.y;   

    // rotate point   
    float xnew = point.x * c - point.y * s;   
    float ynew = point.x * s + point.y * c; 

    // translate point back to global coords:
    sf::Vector2f TranslatedPoint;
    TranslatedPoint.x = xnew + origin.x;  
    TranslatedPoint.y = ynew + origin.y; 

    return TranslatedPoint;
} 

so I've got a math function here that supposed to return a rotated point, and takes an original point, point to rotate around (origin) and radians to rotate it.

However it rotating only at half speed (aka 180 degree movement = 90 degree rotation)

sf::Vector2f RotatePoint(sf::Vector2f origin, sf::Vector2f point, float radian) {   
    float s = sin(radian);   
    float c = cos(radian);  

    // translate point back to origin:  
    point.x -= origin.x;   
    point.y -= origin.y;   

    // rotate point   
    float xnew = point.x * c - point.y * s;   
    float ynew = point.x * s + point.y * c; 

    // translate point back to global coords:
    sf::Vector2f TranslatedPoint;
    TranslatedPoint.x = xnew + origin.x;  
    TranslatedPoint.y = ynew + origin.y; 

    return TranslatedPoint;
} 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

淡淡绿茶香 2024-12-12 07:00:34

该功能对我来说还可以。如果您想知道的话,旋转只是将向量乘以2D Euclidean旋转矩阵(http://en.wikipedia.org/wiki/rotation_matrix)。我能想到的唯一错误是对功能用法的误解。例如,2*pi弧度= 360度,或旋转是逆时针。

The function looks ok to me. The rotation, in case you're wondering, is just multiplying a vector by the 2D Euclidean rotation matrix (http://en.wikipedia.org/wiki/Rotation_matrix ). The only errors I can think of are some misunderstanding of the usage of the function. E.g. that 2*PI radians = 360 degrees, or that the rotation is counterclockwise.

江心雾 2024-12-12 07:00:34

您的代码看起来不错。我们是否同意弧度为180°的PI是PI?

Your code seems fine. Do we agree that 180° in degrees is Pi in radians ?

谁与争疯 2024-12-12 07:00:34

功能还是不错的但是,您可能想稍微熟悉一下线性代数:您基本上将

return rotation * (point - origin) + origin;

旋转计算为由对角线上的 cos(弧度)和非对角线上的 +/- sin(弧度)组成的矩阵。因此,如果您让线性代数库计算该矩阵,则整个函数就是一个单行函数;如果你分解出 -origin 部分(记住,线性代数是线性的),它就变成:

return rotation * point + ( - rotation * origin + origin );

其中第二部分是点不变的并且可以预先计算。

The function is fine. However, you might want to fresh up on your linear algebra a bit: You basically compute

return rotation * (point - origin) + origin;

with rotation as the matrix consisting of cos(radian) on the diagonals and +/- sin(radian) on the off-diagonals. So, the whole function is a one-liner if you let your linear algebra library compute that matrix; and if you factor out the -origin part (remember, linear algebra is linear), it becomes:

return rotation * point + ( - rotation * origin + origin );

where the second part is point-invariant and can be precomputed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文