计算点击点的角度
我正在制作WPF控件(旋钮)。 我试图找出数学来计算角度(0到360)基于圆圈内的鼠标点击位置。
例如,如果我点击X,Y在图像上的位置,我会得到一个点X,Y。 我也有中心点,无法弄清楚如何获得角度。
我的代码如下:
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; }
你几乎是对的:
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);
正确的计算是这样的:
var theta = Math.Atan2(dx, -dy); var angle = ((theta * 180 / Math.PI) + 360) % 360;
您也可以让Vector.AngleBetween进行计算:
var v1 = new Vector(dx, -dy); var v2 = new Vector(0, 1); var angle = (Vector.AngleBetween(v1, v2) + 360) % 360;