Angle required to rotate a polygon towards the direction of a vector

72 Views Asked by At

I have a problem where I need to rotate a polygon so it has the same direction as the vector $v_1$ (the pointy head face $y$-axis +ve).

I tried a solution where I take two vectors one the $y$-axis: $((0,0), (0, 1))$ and the $v_1((1,1),(2,3))$ and try to find the angle between them to rotate the polygon, but it doesn't seem to be right as the polygon does not face in the direction of the vector. I have tried the algorithms from here Stackoverflow question. Any help would be greatly appreciated Image of the Problem I face

1

There are 1 best solutions below

4
On BEST ANSWER

If you really must have an explicit angle, then, using the usual mathematical convention that positive angles represent counterclockwise rotations, it’s simply $\theta = -\arctan\frac{v_x}{v_y}$, where $(v_x,v_y) = head - tail$ of your vector. For your example, this would be $-\arctan{2-1\over3-1} = -\arctan\frac12$. If you have an ATAN2 function available that accepts the numerator and denominator separately, use that to eliminate ambiguities, but be sure that you get the arguments in the right order.

However, if you’re trying to implement this rotation, there’s no real need to compute the angle explicitly (and doing so can introduce extra imprecision). For standard rotation formulas, what you need are the sine and cosine of the angle, not the angle itself. These can be computed directly from the coordinates of the target direction vector: $$\cos\theta = {v_y\over\sqrt{v_x^2+v_y^2}}, \sin\theta = -{v_x\over\sqrt{v_x^2+v_y^2}}.$$ For your example, these work out to be $\cos\theta = 1/\sqrt5$ and $\sin\theta=-2/\sqrt5$.