围绕另一个点旋转一个点的函数
因此,我在这里有一个数学函数,假定返回一个旋转点,并采用原始点,旋转(原点)旋转的点和弧度将其旋转。
但是,它仅以一半速度旋转(又称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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
该功能对我来说还可以。如果您想知道的话,旋转只是将向量乘以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.
您的代码看起来不错。我们是否同意弧度为180°的PI是PI?
Your code seems fine. Do we agree that 180° in degrees is Pi in radians ?
功能还是不错的但是,您可能想稍微熟悉一下线性代数:您基本上将
旋转计算为由对角线上的 cos(弧度)和非对角线上的 +/- sin(弧度)组成的矩阵。因此,如果您让线性代数库计算该矩阵,则整个函数就是一个单行函数;如果你分解出 -origin 部分(记住,线性代数是线性的),它就变成:
其中第二部分是点不变的并且可以预先计算。
The function is fine. However, you might want to fresh up on your linear algebra a bit: You basically compute
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:
where the second part is point-invariant and can be precomputed.