Easiest way to get the angle of mouse position in WPF relative to the center of a circle

大城市里の小女人 提交于 2019-12-20 03:07:36

问题


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.


回答1:


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




回答2:


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.




回答3:


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).



来源:https://stackoverflow.com/questions/4784139/easiest-way-to-get-the-angle-of-mouse-position-in-wpf-relative-to-the-center-of

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!