在WPF中获取鼠标位置相对于圆心的角度的最简单方法

发布于 2024-10-13 08:19:57 字数 534 浏览 2 评论 0原文

我有一个圆圈,当鼠标移动时我可以获得 e.GetPosition(this)。但是如何以编程方式获取相对于圆心的角度呢?

我在互联网上看到一些在 XAML 中绑定的示例时钟。这不是我想要的,我想从鼠标位置相对于圆心获取角度值。

这就是我尝试过的:

    private void ellipse1_MouseMove(object sender, MouseEventArgs e)
    {

        Point position = e.GetPosition(this);
        double x = position.X;
        double y = position.Y;
        double angle;
        double radians;
        radians = Math.Atan2(y, x);
        angle = radians * (180 / Math.PI);           

    }

角度似乎不正确,永远不会得到 0 或 90 180。

I have a circle when mousemove I can get e.GetPosition(this). But how to programmatically get the angle relative to the center of the circle ?

I saw on the internet some sample clocks with binding in XAML. That's not what I want, I want to get the angle value from mouse position relative to the center of a circle.

This is what I tried:

    private void ellipse1_MouseMove(object sender, MouseEventArgs e)
    {

        Point position = e.GetPosition(this);
        double x = position.X;
        double y = position.Y;
        double angle;
        double radians;
        radians = Math.Atan2(y, x);
        angle = radians * (180 / Math.PI);           

    }

Angle doesn't seem correct NEVER get 0 nor 90 180.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

埖埖迣鎅 2024-10-20 08:19:57

好的,MouseEventArgs 公开了函数“GetPosition”,该函数请求一个 UI 元素来为您提供相对鼠标位置。这基本上就是您想要做的。

private void ellipse1_MouseMove(object sender, MouseEventArgs e)
{
  // This will get the mouse cursor relative to the upper left corner of your ellipse.
  // Note that nothing will happen until you are actually inside of your ellipse.
  Point curPoint = e.GetPosition(ellipse1);

  // Assuming that your ellipse is actually a circle.
  Point center = new Point(ellipse1.Width / 2, ellipse1.Height / 2);

  // A bit of math to relate your mouse to the center...
  Point relPoint = new Point(curPoint.X - center.X, curPoint.Y - center.Y);

  // The fruit of your labor.
  Console.WriteLine("({0}:{1})", relPoint.X, relPoint.Y);
}

从您的评论和其余帖子看来,既然您拥有了正确的信息,您就可以自己处理实际的角度计算部分。就单位而言,WPF 使用与设备无关的坐标系。所以半径为 50 的圆不一定是 50 个像素。这一切都取决于您的系统、屏幕分辨率等。这有点无聊,但如果您真的感兴趣,这将为您解释一些。

http://msdn.microsoft.com/en-us/library/ms748373.aspx

OK, the MouseEventArgs expose the function 'GetPosition' which asks for a UI element that will give you the relative mouse position. This is basically what you want to do.

private void ellipse1_MouseMove(object sender, MouseEventArgs e)
{
  // This will get the mouse cursor relative to the upper left corner of your ellipse.
  // Note that nothing will happen until you are actually inside of your ellipse.
  Point curPoint = e.GetPosition(ellipse1);

  // Assuming that your ellipse is actually a circle.
  Point center = new Point(ellipse1.Width / 2, ellipse1.Height / 2);

  // A bit of math to relate your mouse to the center...
  Point relPoint = new Point(curPoint.X - center.X, curPoint.Y - center.Y);

  // The fruit of your labor.
  Console.WriteLine("({0}:{1})", relPoint.X, relPoint.Y);
}

It seems that from your comments and the rest of the posts that you can handle the actual angle computation part yourself now that you have the right information. As far as the units are concerned, WPF uses a device independent system of coordinates. So a circle with a radius of 50 will not necessarily be 50 pixels. It all depends on your system, screen resolution, etc. It's all kind of boring, but this will explain some it for you if you are really interested.

http://msdn.microsoft.com/en-us/library/ms748373.aspx

梦在夏天 2024-10-20 08:19:57

您可以使用 Atan2 http://msdn.microsoft.com/en -us/library/system.math.atan2.aspx

public static double Atan2(
    double y,
    double x
)

只需将 y 作为鼠标 y 坐标和圆心之间的增量传递即可,对于 x 也是如此。
注意结果是用辐射度表示的,如果是圆,有相对于圆的X,Y可以通过radius-y,radius-x,如果是椭圆可以通过height/2-y,宽度/2-x。

You can use Atan2 http://msdn.microsoft.com/en-us/library/system.math.atan2.aspx.

public static double Atan2(
    double y,
    double x
)

just pass y as delta between your mouse y coord and the center of the circle, and the same for x.
Pay attention that the result is expressed in radiant.if it is a circle and you have X,Y relative to the circle you can pass radius-y,radius-x, if it is an ellipse you can pass height/2-y, width/2-x.

捎一片雪花 2024-10-20 08:19:57

三角学可以为你做到这一点。

因此,您需要使用反正切来执行此操作(可在 System.Math.ATan 中找到)。

您还需要考虑角度为 pi/2(或 90 度)倍数的情况。

Trigonometry can do this for you.

So you will want to use the Arc Tangent to do this (which is found at System.Math.ATan).

You will also need to account for the cases where the angle is a multiple of pi/2 (or 90 degrees).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文