Estoy tratando de determinar si un punto está dentro de un polígono. el Polígono está definido por una matriz de objetos Point. Puedo averiguar fácilmente si el punto está dentro de la caja delimitada del polígono, pero no estoy seguro de cómo decir si está dentro del polígono real o no. Si es posible, me gustaría usar solo C# y WinForms. Prefiero no invocar OpenGL o algo para hacer esta simple tarea.C# Punto en el polígono
Aquí está el código que tengo hasta ahora:
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;
}
¿el polígono es convexo? –
Siempre puedes usar la clase 'Region'. – leppie
@Saeed Creo que todos son convexos. @leppie, no estoy familiarizado con la clase 'Región'. ¿Quieres escribir un código para mí? –