Griffin
Griffin

Reputation: 2517

Equation to find the degrees of rotation of a once horizontal line

hey so I have a center of an object and i'm making a line with this center as the start of it, and the mouse position the end of it.

How do I find how rotated the second point (?,?) is around the stationary first point (0,0) ?
Note: if the second point were (1, 0) it would be roated 0 degrees.

Upvotes: 0

Views: 386

Answers (2)

Jonathan Grynspan
Jonathan Grynspan

Reputation: 43472

Remember SOHCAHTOA? ;) You can use the arctangent (via the standard atan2() function) to determine the angle of the line between the origin and an arbitrary point:

#include <cmath>

double mouseX = ...;
double mouseY = ...;
double angleInRadians = std::atan2(mouseY, mouseX);

Upvotes: 4

Petar Ivanov
Petar Ivanov

Reputation: 93000

If the angle of rotation is a, and the second point is (x,y) then:

a = arctan(y/x)

Upvotes: 2

Related Questions