1

I am trying to programmatically find the point created from rotating a vector around it's origin point (could be anywhere in 2D space). Example drawing here

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. It runs to point B at some (x, y). We rotate it by Theta which then moves to some point C at an (x, y). 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. Would anyone have ideas?

I am using C# and my own vector object (below) to test out the creation of lines. 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
    }
}
Peter O.
  • 32,158
  • 14
  • 82
  • 96
Zach Bell
  • 21
  • 3
  • 1
    Sounds like linear transformation is your friend here? You can translate the vector to the origin `(0,0)` then rotate it with a rotation matrix, then translate it back to the original position. – Jack T. Spades Jun 08 '21 at 20:18

3 Answers3

3

You can convert cartesian coordinates (x, y) into polar ones (R, fi), add theta to fi and then convert back to cartesian:

// 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.

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • Ah yes, this is where I remember saying in math class that I really should pay attention because it might come back to me someday.... This would be a really good static method though! – Zach Bell Jun 09 '21 at 15:24
  • I actually didn't have permission to upvote until just recently as I haven't commented enough. So I will gladly upvote more things on stack overflow. Hopefully I can help those in need as well as I have sought out this site for everything. – Zach Bell Jun 09 '21 at 18:15
3

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. And, you can refer to the rotation algorithm from the following 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);
    }
}
Ming Shou
  • 129
  • 7
2

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

rustyBucketBay
  • 4,320
  • 3
  • 17
  • 47
  • This was also really helpful to look at, thanks for the link of an original post. I couldn't find any direct answers using vector as the main term, but they are just points in the end. – Zach Bell Jun 09 '21 at 15:22