I am making a WPF control (knob). I am trying to figure out the math to calculate the angle (0 to 360) based on a mouse click position inside the circle.
For instance, if I click where the X,Y is on the image, I would have a point X,Y. I have the centerpoint as well, and cannot figure out how to get the angle.
My code below:
internal double GetAngleFromPoint(Point point, Point centerPoint)
{
double dy = (point.Y - centerPoint.Y);
double dx = (point.X - centerPoint.X);
double theta = Math.Atan2(dy,dx);
double angle = (theta * 180) / Math.PI;
return angle;
}
First note that a clock is a circle made of 360 degrees, and that each number represents an angle and the separation between them is 360/12 = 30. And at 2:00, the minute hand is on the 12 and the hour hand is on the 2. The correct answer is 2 * 30 = 60 degrees.
Formula for angle between two Vectors The cosine of the angle between two vectors is equal to the sum of the product of the individual constituents of the two vectors, divided by the product of the magnitude of the two vectors. =| A | | B | cosθ.
You've got it almost right:
internal double GetAngleFromPoint(Point point, Point centerPoint)
{
double dy = (point.Y - centerPoint.Y);
double dx = (point.X - centerPoint.X);
double theta = Math.Atan2(dy,dx);
double angle = (90 - ((theta * 180) / Math.PI)) % 360;
return angle;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With