如何确定一个点是否在椭圆内? [英] How to determine if a point is within an ellipse?

查看:2706
本文介绍了如何确定一个点是否在椭圆内?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了麻烦,发现经典的Contains方法,如果它位于矩形,椭圆或其他对象内部的某个点上,它将返回。这些对象位于画布中。



我试过VisualTreeHelper.FindElementsInHostCoordinates,但找不到路。



有人帮助我吗?



此致敬礼!

解决方案


*(如果您觉得它有用,请投票!)

http://my.safaribooksonline.com/book/programming/csharp/9780672331985/graphics-with-windows-forms- and-gdiplus / ch17lev1sec22

  public bool包含(Ellipse Ellipse,Point location)
{
Point center = new Point(
Canvas.GetLeft(Ellipse)+(Ellipse.Width / 2),
Canvas.GetTop(Ellipse)+(Ellipse.Height / 2));

double _xRadius = Ellipse.Width / 2;
double _yRadius = Ellipse.Height / 2;


if(_xRadius< = 0.0 || _yRadius< = 0.0)
return false;
/ *这是圆形方程的更一般形式
*
* X ^ 2 / a ^ 2 + Y ^ 2 / b ^ 2 <= 1
* /

点归一化=新点(location.X - center.X,
location.Y - center.Y); $(b)
$ b return((double)(normalized.X * normalized.X)
/(_xRadius * _xRadius))+((double)(normalized.Y * normalized.Y)/(_yRadius * _yRadius))
<= 1.0;
}


I'm having troubles finding the classic 'Contains' method wich returns if a point it's within a rectangle, ellipse or some else object. These objects are in a canvas.

I've tried VisualTreeHelper.FindElementsInHostCoordinates but I can't find the way.

does Anybody help me?

Thanks in advance!

解决方案

This works for me. *(If you find it usefull please vote it!)

http://my.safaribooksonline.com/book/programming/csharp/9780672331985/graphics-with-windows-forms-and-gdiplus/ch17lev1sec22

 public bool Contains(Ellipse Ellipse, Point location)
        {
            Point center = new Point(
                  Canvas.GetLeft(Ellipse) + (Ellipse.Width / 2),
                  Canvas.GetTop(Ellipse) + (Ellipse.Height / 2));

            double _xRadius = Ellipse.Width / 2;
            double _yRadius = Ellipse.Height / 2;


            if (_xRadius <= 0.0 || _yRadius <= 0.0)
                return false;
            /* This is a more general form of the circle equation
             *
             * X^2/a^2 + Y^2/b^2 <= 1
             */

            Point normalized = new Point(location.X - center.X,
                                         location.Y - center.Y);

            return ((double)(normalized.X * normalized.X)
                     / (_xRadius * _xRadius)) + ((double)(normalized.Y * normalized.Y) / (_yRadius * _yRadius))
                <= 1.0;
        }

这篇关于如何确定一个点是否在椭圆内?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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