整體思路:射線法。
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對象考慮的面,所以並未考慮重疊面的問題。