[英]Calculate the angle of a click point
I am making a WPF control (knob). 我正在制作WPF控件(旋钮)。 I am trying to figure out the math to calculate the angle (0 to 360) based on a mouse click position inside the circle.
我试图找出数学来计算角度(0到360)基于圆圈内的鼠标点击位置。
For instance, if I click where the X,Y is on the image, I would have a point X,Y. 例如,如果我点击X,Y在图像上的位置,我会得到一个点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;
}
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;
}
你需要
double theta = Math.Atan2(dx,dy);
The correct calculation is this: 正确的计算是这样的:
var theta = Math.Atan2(dx, -dy);
var angle = ((theta * 180 / Math.PI) + 360) % 360;
You could also let Vector.AngleBetween do the calculation: 您也可以让Vector.AngleBetween进行计算:
var v1 = new Vector(dx, -dy);
var v2 = new Vector(0, 1);
var angle = (Vector.AngleBetween(v1, v2) + 360) % 360;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.