C#+AE 判断点是否在面内的方法
public double getXMinValue(IPolygon nPolygon)
{
IPolygon sPolygon;
sPolygon = nPolygon;
IPointCollection pPointCollection;
pPointCollection = sPolygon as IPointCollection;
int n = pPointCollection.PointCount;
double[] coordX = new double[n];
for (int i = 0; i < n; i++)
{
IPoint point = pPointCollection.get_Point(i);
coordX[i] = point.X;
}
//对数组进行从小到大排序
System.Array.Sort(coordX);
Xmin = coordX[0];
return Xmin;
}
向左画射线并得到交点个数的代码:
try
{
ILine newLine = new LineClass();
IPoint toPoint = new PointClass();
toPoint.PutCoords(getXMinValue(pPolygon) - 20.0000000000000, pPoint.Y);
newLine.PutCoords(pPoint, toPoint);//给新的直线赋予起始坐标
//苗师兄想法
IPolyline l = new PolylineClass();
l.FromPoint = pPoint;
l.ToPoint = toPoint;
IGeometryCollection pPolyline = new PolylineClass();
ISegmentCollection pPath;
pPath = new PathClass();
object missing1 = Type.Missing;
object missing2 = Type.Missing;
pPath.AddSegment(newLine as ISegment, ref missing1, ref missing2);
pPolyline.AddGeometry(pPath as IGeometry, ref missing1, ref missing2);
IElement element = DrawLineSymbol(pPolyline as IGeometry, pColor);
pGraph.AddElement(element, 0);
pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
ITopologicalOperator pTopo = pPolygon as ITopologicalOperator;//pPolygon多边形
IGeometryCollection pGeoCol = pTopo.Intersect((IGeometry)l, esriGeometryDimension.esriGeometry0Dimension) as IGeometryCollection; //执行到这一句有问题;
IPointCollection pPointCol = pGeoCol as IPointCollection;
if ((pPointCol.PointCount) % 2 == 0)
{
MessageBox.Show("射线和多边形的交点个数为:"+pPointCol.PointCount.ToString()+",点在多边形外。");
}
else
{
MessageBox.Show("射线和多边形的交点个数为:"+pPointCol.PointCount.ToString()+",点在多边形内。");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
本文是按照Polygon对象考虑的面,所以并未考虑重叠面的问题。