【发布时间】:2011-05-13 16:25:22
【问题描述】:
我正在尝试确定一个点是否在多边形内。 Polygon 由 Point 对象数组定义。我可以很容易地确定该点是否在多边形的有界框内,但我不确定如何判断它是否在实际多边形内。如果可能的话,我只想使用 C# 和 WinForms。我宁愿不调用 OpenGL 或其他东西来完成这个简单的任务。
这是我目前的代码:
private void CalculateOuterBounds()
{
//m_aptVertices is a Point[] which holds the vertices of the polygon.
// and X/Y min/max are just ints
Xmin = Xmax = m_aptVertices[0].X;
Ymin = Ymax = m_aptVertices[0].Y;
foreach(Point pt in m_aptVertices)
{
if(Xmin > pt.X)
Xmin = pt.X;
if(Xmax < pt.X)
Xmax = pt.X;
if(Ymin > pt.Y)
Ymin = pt.Y;
if(Ymax < pt.Y)
Ymax = pt.Y;
}
}
public bool Contains(Point pt)
{
bool bContains = true; //obviously wrong at the moment :)
if(pt.X < Xmin || pt.X > Xmax || pt.Y < Ymin || pt.Y > Ymax)
bContains = false;
else
{
//figure out if the point is in the polygon
}
return bContains;
}
【问题讨论】:
-
您总是可以只使用
Region类。 -
@Saeed 我相信它们都是凸的。 @leppie,我不熟悉
Region类。想为我写一些代码吗? -
@jb:不,很容易学。