0
votes

I have a piece of code that returns the angle between two vectors in the range of [0,360]. For this I used this question: Direct way of computing clockwise angle between 2 vectors. Now I need to create a function that takes a vector and an angle as input and returns a vector, that has the specified angle with the inputvector. The length of this vector doesn't matter. For this, I need to know how to reverse the effect of Atan2. The rest is pretty simple math.

        internal virtual double AngleWith(Vector2 direction, Vector2 location)
        {
            Vector2 normDir = Vector2.Normalize(direction);
            Vector2 normLoc = Vector2.Normalize(location);
            double dot = (normDir.X * normLoc.X) + (normDir.Y * normLoc.Y);
            double det = (normDir.X * normLoc.Y) - (normDir.Y * normLoc.X);
            return Math.Atan2(-det, -dot) * (180 / Math.PI) + 180;
        }

Any help is appreciated.

2
The inverse of atan2 is just tan.PMF
@PMF but atan2 take two arguments, while tan returns only one. To rephrase it: I need a way to find out what the two original inputs of atan2 were, based on its output.dexterdy Krataigos
The inverse will give the ratio between det and dot, not their absolute value. There's no way to exactly inverse atan2.PMF

2 Answers

0
votes

I don't know what you need this for, but arguably there is merit in transforming your vectors from the x,y-coordinate system to the polar coordinate system, in which points in a plane are given by their distance from the origin and the angle to a reference vector (for instance the x-axis in the explanation below), or

To convert from (x, y) to (r, t) with r being the distance between (x,y) and (0,0) and t being the angle in radians between the x-axis and the line connecting (0, 0) and (x, y), you use this:

(r, t) = (sqrt(x^x+y^y), atan(y/x))

The result can be stored in Vector2, just like with x and y. You just have to remember that the values inside don't signify x and y.

If you want the difference in angle, you can just subtract t2 and t1 of your polar coordinates (in radians, still need to convert to degrees).

If you need to add a certain angle in degrees, just add or subtract it to the t value of your polar coordinate.

To convert back to x and y, use

(x, y) = (r cos(t), r sin(t))
0
votes

The typical way to do this is with a rotation matrix.

RotatedX = x * sin ϴ - y * sin ϴ
RotatedY = x * sin ϴ + y * cos ϴ

Or use System.Numerics.Matrix3x2.CreateRotation(angle) and use it to transform your vector. Note that 'Clockwise' may depend on what coordinate conventions are used. So you might need to adjust the formula depending on your convention.