计算点击点的角度 [英] Calculate the angle of a click point

查看:130
本文介绍了计算点击点的角度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作WPF控件(旋钮).我正在尝试计算数学,以便根据鼠标在圆内的单击位置来计算角度(0到360度).

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.

例如,如果我单击图像上X,Y的位置,我将得到一个X,Y点.我也有中心点,无法弄清楚如何获得角度.

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.

我的下面的代码:

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;
}

这篇关于计算点击点的角度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆