简体   繁体   English

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

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

I am trying to programmatically find the point created from rotating a vector around it's origin point (could be anywhere in 2D space).我正在尝试以编程方式找到通过围绕其原点旋转矢量创建的点(可能在 2D 空间中的任何位置)。示例图在这里

We see that we have our line (or vector for the math) A at some point of (x, y) that might be anywhere in 2D space.我们看到我们在(x, y)的某个点处有我们的线(或数学的向量) A ,它可能位于 2D 空间中的任何位置。 It runs to point B at some (x, y) .它在某个(x, y)处运行到B点。 We rotate it by Theta which then moves to some point C at an (x, y) .我们通过Theta旋转它,然后在(x, y)处移动到某个点C The problem for me comes with trying to programmatically use math to solve for such.对我来说,问题在于尝试以编程方式使用数学来解决这些问题。

Originally the thought was to form a triangle and use trig but this angle could be exactly 180 (unlikely but possible) which obviously no triangle can work.最初的想法是形成一个三角形并使用 trig,但这个角度可能正好是 180(不太可能但可能),显然没有三角形可以工作。 Would anyone have ideas?有人会有想法吗?

I am using C# and my own vector object (below) to test out the creation of lines.我正在使用 C# 和我自己的向量 object(如下)来测试线条的创建。 Any help is appreciated!任何帮助表示赞赏!

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
    }
}

You can convert cartesian coordinates ( x, y ) into polar ones ( R, fi ), add theta to fi and then convert back to cartesian:您可以将笛卡尔坐标 ( 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));
}

the only possible difficulty is to compute fi which can be done with a help of Math.Atan2 method.唯一可能的困难是计算fi这可以借助Math.Atan2方法来完成。

I think it would be best to use the following code.我认为最好使用以下代码。 I've made some minor modifications and supplements to your code.我对您的代码做了一些小的修改和补充。

The calculation part of 'theta' was slightly modified. 'theta' 的计算部分略有修改。 And, you can refer to the rotation algorithm from the following URL.并且,您可以参考以下URL中的旋转算法。

Rotation (mathematics)旋转(数学)

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);
    }
}

Another option:另外的选择:

// 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;
}

inspired by this answer受到这个答案的启发

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

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