Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate the angle of a click point

Tags:

c#

math

wpf

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.

circle image

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;
}
like image 668
Rick Rat Avatar asked Jan 19 '13 10:01

Rick Rat


People also ask

How do you calculate the angle of a clock?

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.

What is the formula to find the angle between two vectors?

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


1 Answers

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;
}
like image 162
Eren Ersönmez Avatar answered Sep 28 '22 02:09

Eren Ersönmez