簡體   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