绕原点旋转点的适当三角学

发布于 2024-09-08 09:13:30 字数 497 浏览 9 评论 0原文

以下任一方法是否使用正确的数学来旋转点?如果是这样,哪一个是正确的?

POINT rotate_point(float cx,float cy,float angle,POINT p)
{
  float s = sin(angle);
  float c = cos(angle);

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

  // Which One Is Correct:
  // This?
  float xnew = p.x * c - p.y * s;
  float ynew = p.x * s + p.y * c;
  // Or This?
  float xnew = p.x * c + p.y * s;
  float ynew = -p.x * s + p.y * c;

  // translate point back:
  p.x = xnew + cx;
  p.y = ynew + cy;
}

Do either of the below approaches use the correct mathematics for rotating a point? If so, which one is correct?

POINT rotate_point(float cx,float cy,float angle,POINT p)
{
  float s = sin(angle);
  float c = cos(angle);

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

  // Which One Is Correct:
  // This?
  float xnew = p.x * c - p.y * s;
  float ynew = p.x * s + p.y * c;
  // Or This?
  float xnew = p.x * c + p.y * s;
  float ynew = -p.x * s + p.y * c;

  // translate point back:
  p.x = xnew + cx;
  p.y = ynew + cy;
}

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

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

发布评论

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

评论(3

烟─花易冷 2024-09-15 09:13:30

来自维基百科

要使用矩阵执行旋转,点 (x, y )要旋转的被写为向量,然后乘以根据角度 θ 计算的矩阵,如下所示:

https://upload.wikimedia.org/math/0/e/d/0ed0d28652a45d730d096a56e2d0d0a3.png

其中 (x′, y′) 是旋转后点的坐标,公式对于 x′ 和 y′ 可以看作

alt text

From Wikipedia

To carry out a rotation using matrices the point (x, y) to be rotated is written as a vector, then multiplied by a matrix calculated from the angle, θ, like so:

https://upload.wikimedia.org/math/0/e/d/0ed0d28652a45d730d096a56e2d0d0a3.png

where (x′, y′) are the co-ordinates of the point after rotation, and the formulae for x′ and y′ can be seen to be

alt text

只是在用心讲痛 2024-09-15 09:13:30

这取决于您如何定义角度。如果是逆时针测量(这是数学约定),那么正确的旋转是第一个:

// This?
float xnew = p.x * c - p.y * s;
float ynew = p.x * s + p.y * c;

但如果是顺时针测量,那么第二个是正确的:

// Or This?
float xnew = p.x * c + p.y * s;
float ynew = -p.x * s + p.y * c;

It depends on how you define angle. If it is measured counterclockwise (which is the mathematical convention) then the correct rotation is your first one:

// This?
float xnew = p.x * c - p.y * s;
float ynew = p.x * s + p.y * c;

But if it is measured clockwise, then the second is correct:

// Or This?
float xnew = p.x * c + p.y * s;
float ynew = -p.x * s + p.y * c;
空心↖ 2024-09-15 09:13:30

这是从我自己的矢量库中提取的..

//----------------------------------------------------------------------------------
// Returns clockwise-rotated vector, using given angle and centered at vector
//----------------------------------------------------------------------------------
CVector2D   CVector2D::RotateVector(float fThetaRadian, const CVector2D& vector) const
{
    // Basically still similar operation with rotation on origin
    // except we treat given rotation center (vector) as our origin now
    float fNewX = this->X - vector.X;
    float fNewY = this->Y - vector.Y;

    CVector2D vectorRes(    cosf(fThetaRadian)* fNewX - sinf(fThetaRadian)* fNewY,
                            sinf(fThetaRadian)* fNewX + cosf(fThetaRadian)* fNewY);
    vectorRes += vector;
    return vectorRes;
}

This is extracted from my own vector library..

//----------------------------------------------------------------------------------
// Returns clockwise-rotated vector, using given angle and centered at vector
//----------------------------------------------------------------------------------
CVector2D   CVector2D::RotateVector(float fThetaRadian, const CVector2D& vector) const
{
    // Basically still similar operation with rotation on origin
    // except we treat given rotation center (vector) as our origin now
    float fNewX = this->X - vector.X;
    float fNewY = this->Y - vector.Y;

    CVector2D vectorRes(    cosf(fThetaRadian)* fNewX - sinf(fThetaRadian)* fNewY,
                            sinf(fThetaRadian)* fNewX + cosf(fThetaRadian)* fNewY);
    vectorRes += vector;
    return vectorRes;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文