/// <summary> /// Accurate Clip Polygon with Line /// </summary> /// <param name="polygon"></param> /// <param name="line"></param> /// <param name="resultFeatureSet"></param> /// <returns></returns> public static bool Accurate_ClipPolygonWithLine( ref IFeature polygon, ref IFeature line, ref IFeatureSet resultFeatureSet) { if (polygon != null && line != null) { bool boundsIntersect = line.Crosses(polygon); if (boundsIntersect == false) { return false; } // find if all of the line is inside, outside, or part in and out of polygon // line might intersect polygon mutliple times int numPoints = line.NumPoints; bool[] ptsInside = new bool[numPoints]; int numInside = 0; int numOutside = 0; int numParts = polygon.NumPoints; if (numParts == 0) { numParts = 1; } Coordinate[][] polyVertArray = new Coordinate[numParts][]; ConvertPolyToVertexArray(ref polygon, ref polyVertArray); // check each point in the line to see if the entire line is either // inside of the polygon or outside of it (we know it's inside polygon bounding box). for (int i = 0; i <= numPoints - 1; i++) { Point currPt = new Point(line.Coordinates[i]); if (polygon.Covers(currPt)) { ptsInside[i] = true; numInside += 1; } else { ptsInside[i] = false; numOutside += 1; } } if (numInside == numPoints) { // case: all points are inside polygon - check for possible intersections if (ProcessAllInside(ref line, ref polygon, ref resultFeatureSet) == false) { return false; } } else if (numOutside == numPoints) { // case: all points are outside of the polygon - check for possible intersections if (ProcessAllOutside(ref line, ref polygon, ref resultFeatureSet) == false) { return false; } } else { // case: part of line is inside and part is outside - find inside segments. if (ProcessPartInAndOut(ref ptsInside, ref line, ref polygon, ref resultFeatureSet) == false) { return false; } } } else { return false; } return true; }