[英]How to determine if a point is within an ellipse?
我在寻找经典的“包含”方法时遇到了麻烦,该方法在一个点位于矩形、椭圆或其他对象内时返回。 这些对象位于画布中。
我试过 VisualTreeHelper.FindElementsInHostCoordinates 但我找不到方法。
我怎样才能做到这一点?
这对我有用。 *(如果觉得有用请投票!)
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;
}
使用 C# 就不是那么简单了。
首先你需要一个GraphicsPath
。 然后将其初始化为您想要的形状,对于椭圆,请使用AddEllipse
方法。 然后使用IsVisible
方法检查您的点是否包含在形状内。 您可以使用各种AddLine
方法之一来测试任意形状。
例如。
Rectangle myEllipse = new Rectangle(20, 20, 100, 50);
// use the bounding box of your ellipse instead
GraphicsPath myPath = new GraphicsPath();
myPath.AddEllipse(myEllipse);
bool pointWithinEllipse = myPath.IsVisible(40,30);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.