计算点击点的角度

Ric*_*zak 10 c# math wpf

我正在制作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;
}
Run Code Online (Sandbox Code Playgroud)

Ere*_*mez 9

你几乎是对的:

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;
}
Run Code Online (Sandbox Code Playgroud)