繁体   English   中英

寻找旋转向量的新点的算法

[英]Algorithm for finding new points of rotating a vector

我正在尝试以编程方式找到通过围绕其原点旋转矢量创建的点(可能在 2D 空间中的任何位置)。示例图在这里

我们看到我们在(x, y)的某个点处有我们的线(或数学的向量) A ,它可能位于 2D 空间中的任何位置。 它在某个(x, y)处运行到B点。 我们通过Theta旋转它,然后在(x, y)处移动到某个点C 对我来说,问题在于尝试以编程方式使用数学来解决这些问题。

最初的想法是形成一个三角形并使用 trig,但这个角度可能正好是 180(不太可能但可能),显然没有三角形可以工作。 有人会有想法吗?

我正在使用 C# 和我自己的向量 object(如下)来测试线条的创建。 任何帮助表示赞赏!

struct Vector2D {
    double x, y, theta;
    Vector2D(double x, double y) {
        (this.x, this.y) = (x, y);
        theta = x != 0 ? Math.Atan(y / x) : 0;
    }
    double Magnitude() {
        return Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2));
    }
    (double,double) PointFromRotation(double angle) {
        // This is where I need some help...
        
        return (0,0); // hopefully a point of x and y from the angle argument
    }
}

您可以将笛卡尔坐标 ( x, y ) 转换为极坐标( R, fi ),将theta添加到fi然后再转换回笛卡尔坐标:

// Rotate B around A by angle theta
private static (double x, double y) Rotate(
  (double x, double y) A, 
  (double x, double y) B, 
  double theta) {
  
  double fi = Math.Atan2(B.y - A.y, B.x - A.x) + theta;
  double R = Math.Sqrt((A.y - B.y) * (A.y - B.y) + (A.x - B.x) * (A.x - B.x));

  return (A.x + R * Math.Cos(fi), A.y + R * Math.Sin(fi));
}

唯一可能的困难是计算fi这可以借助Math.Atan2方法来完成。

我认为最好使用以下代码。 我对您的代码做了一些小的修改和补充。

'theta' 的计算部分略有修改。 并且,您可以参考以下URL中的旋转算法。

旋转(数学)

struct Vector2D
{
    public double x;
    public double y;
    public double theta;

    public Vector2D(double x, double y)
    {
        (this.x, this.y) = (x, y);
        theta = x != 0 ? Math.Atan(y / x) : Math.Sign(y) * Math.PI / 2.0;
    }

    public double Magnitude()
    {
        return Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2));
    }

    public (double x, double y) PointFromRotation(double angle)
    {
        double Sint = Math.Sin(angle);
        double Cost = Math.Cos(angle);

        double rX = x * Cost - y * Sint;
        double rY = x * Sint + y * Cost;

        return (rX, rY);
    }
}

另外的选择:

// Rotate B around A by angle theta clockwise
private static (double x, double y) Rotate(
      (double x, double y) A,
      (double x, double y) B,
      double theta)
{
        double s = Math.Sin(theta);
        double c = Math.Cos(theta);

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

        // rotate point clockwise
        double xnew = B.x * c - B.y * s;
        double ynew = B.x * s + B.y * c;

        // translate point back:
        B.x = xnew + A.x;
        B.y = ynew + A.y;

        return B;
}

受到这个答案的启发

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM