private PointCollection ClipPolygon(Polygon polygon, double Left, double Top, double Right, double Bottom) { Point point; Point point2; PointCollection clippedPolygon = new PointCollection(); double xTop = 0.0; double yLeft = 0.0; double xBottom = 0.0; double yRight = 0.0; int num5 = 0; int num6 = -1; double num7 = 0.0; double num8 = 0.0; double num9 = 0.0; double num10 = 0.0; for (int i = 0; i < polygon.get_Points().get_Count(); i++) { point = polygon.get_Points().get_Item(i); if (point.get_X() < num7) { num7 = point.get_X(); } if (point.get_X() > num8) { num8 = point.get_X(); } if (point.get_Y() < num9) { num9 = point.get_Y(); } if (point.get_Y() > num10) { num10 = point.get_Y(); } if (((Left <= point.get_X()) && (point.get_X() <= Right)) && ((Top <= point.get_Y()) && (point.get_Y() <= Bottom))) { num6 = i; break; } } if (num6 == -1) { for (int k = 0; k < polygon.get_Points().get_Count(); k++) { point = polygon.get_Points().get_Item(k); point2 = (k == (polygon.get_Points().get_Count() - 1)) ? polygon.get_Points().get_Item(0) : polygon.get_Points().get_Item(k + 1); this.CalculateLineIntersection(point, point2, ref yLeft, ref xTop, ref yRight, ref xBottom, Left, Top, Right, Bottom); if (((((Top <= yLeft) && (yLeft <= Bottom)) && (((point.get_X() < Left) && (Left < point2.get_X())) || ((point2.get_X() < Left) && (Left < point.get_X())))) || (((Left <= xTop) && (xTop <= Right)) && (((point.get_Y() < Top) && (Top < point2.get_Y())) || ((point2.get_Y() < Top) && (Top < point.get_Y()))))) || ((((Top <= yRight) && (yRight <= Bottom)) && (((point.get_X() < Right) && (Right < point2.get_X())) || ((point2.get_X() < Right) && (Right < point.get_X())))) || (((Left <= xBottom) && (xBottom <= Right)) && (((point.get_Y() < Bottom) && (Bottom < point2.get_Y())) || ((point2.get_Y() < Bottom) && (Bottom < point.get_Y())))))) { this.ClipLine(point, point2, yLeft, xTop, yRight, xBottom, Left, Top, Right, Bottom, ref clippedPolygon); num5 = clippedPolygon.get_Count(); num6 = (k == (polygon.get_Points().get_Count() - 1)) ? 0 : (k + 1); break; } } } if (num6 == -1) { if (((num7 <= Left) && (Right <= num8)) && ((num9 <= Top) && (Bottom <= num10))) { clippedPolygon.Add(new Point(Left, Top)); clippedPolygon.Add(new Point(Right, Top)); clippedPolygon.Add(new Point(Right, Bottom)); clippedPolygon.Add(new Point(Left, Bottom)); } return clippedPolygon; } for (int j = num6; j < (polygon.get_Points().get_Count() + num6); j++) { point = polygon.get_Points().get_Item((j < polygon.get_Points().get_Count()) ? j : (j - polygon.get_Points().get_Count())); point2 = ((j + 1) < polygon.get_Points().get_Count()) ? polygon.get_Points().get_Item(j + 1) : polygon.get_Points().get_Item((j + 1) - polygon.get_Points().get_Count()); this.CalculateLineIntersection(point, point2, ref yLeft, ref xTop, ref yRight, ref xBottom, Left, Top, Right, Bottom); if (((Left <= point.get_X()) && (point.get_X() <= Right)) && ((Top <= point.get_Y()) && (point.get_Y() <= Bottom))) { if (((clippedPolygon.get_Count() == 0) || (clippedPolygon.get_Item(clippedPolygon.get_Count() - 1).get_X() != point.get_X())) || (clippedPolygon.get_Item(clippedPolygon.get_Count() - 1).get_Y() != point.get_Y())) { clippedPolygon.Add(point); } if (((point2.get_X() < Left) || (Right < point2.get_X())) || ((point2.get_Y() < Top) || (Bottom < point2.get_Y()))) { if (((Top <= yLeft) && (yLeft <= Bottom)) && ((point2.get_X() < Left) && (Left < point.get_X()))) { clippedPolygon.Add(new Point(Left, yLeft)); } else if (((Left <= xTop) && (xTop <= Right)) && ((point2.get_Y() < Top) && (Top < point.get_Y()))) { clippedPolygon.Add(new Point(xTop, Top)); } else if (((Top <= yRight) && (yRight <= Bottom)) && ((point.get_X() < Right) && (Right < point2.get_X()))) { clippedPolygon.Add(new Point(Right, yRight)); } else if (((Left <= xBottom) && (xBottom <= Right)) && ((point.get_Y() < Bottom) && (Bottom < point2.get_Y()))) { clippedPolygon.Add(new Point(xBottom, Bottom)); } } } else { if (clippedPolygon.get_Count() > 0) { if (((clippedPolygon.get_Item(clippedPolygon.get_Count() - 1).get_X() == Left) && (point.get_X() < Left)) && (Left <= point2.get_X())) { if ((yLeft < Top) && (clippedPolygon.get_Item(clippedPolygon.get_Count() - 1).get_Y() > Top)) { clippedPolygon.Add(new Point(Left, Top)); } else if ((yLeft > Bottom) && (clippedPolygon.get_Item(clippedPolygon.get_Count() - 1).get_Y() < Bottom)) { clippedPolygon.Add(new Point(Left, Bottom)); } } if (((clippedPolygon.get_Item(clippedPolygon.get_Count() - 1).get_Y() == Top) && (point.get_Y() < Top)) && (Top <= point2.get_Y())) { if ((xTop < Left) && (clippedPolygon.get_Item(clippedPolygon.get_Count() - 1).get_X() > Left)) { clippedPolygon.Add(new Point(Left, Top)); } else if ((xTop > Right) && (clippedPolygon.get_Item(clippedPolygon.get_Count() - 1).get_X() < Right)) { clippedPolygon.Add(new Point(Right, Top)); } } if (((clippedPolygon.get_Item(clippedPolygon.get_Count() - 1).get_X() == Right) && (point2.get_X() <= Right)) && (Right < point.get_X())) { if ((yRight < Top) && (clippedPolygon.get_Item(clippedPolygon.get_Count() - 1).get_Y() > Top)) { clippedPolygon.Add(new Point(Right, Top)); } else if ((yRight > Bottom) && (clippedPolygon.get_Item(clippedPolygon.get_Count() - 1).get_Y() < Bottom)) { clippedPolygon.Add(new Point(Right, Bottom)); } } if (((clippedPolygon.get_Item(clippedPolygon.get_Count() - 1).get_Y() == Bottom) && (point2.get_Y() <= Bottom)) && (Bottom < point.get_Y())) { if ((xBottom > Right) && (clippedPolygon.get_Item(clippedPolygon.get_Count() - 1).get_X() < Right)) { clippedPolygon.Add(new Point(Right, Bottom)); } else if ((xBottom < Left) && (clippedPolygon.get_Item(clippedPolygon.get_Count() - 1).get_X() > Left)) { clippedPolygon.Add(new Point(Left, Bottom)); } } } if (((Left <= point2.get_X()) && (point2.get_X() <= Right)) && ((Top <= point2.get_Y()) && (point2.get_Y() <= Bottom))) { if (((Top <= yLeft) && (yLeft <= Bottom)) && ((point.get_X() < Left) && (Left < point2.get_X()))) { if ((((clippedPolygon.get_Item(clippedPolygon.get_Count() - 1).get_Y() == Top) && (point.get_Y() == Top)) && (point2.get_Y() == Top)) || (((clippedPolygon.get_Item(clippedPolygon.get_Count() - 1).get_Y() == Bottom) && (point.get_Y() == Bottom)) && (point2.get_Y() == Bottom))) { clippedPolygon.RemoveAt(clippedPolygon.get_Count() - 1); } else { clippedPolygon.Add(new Point(Left, yLeft)); } } else if (((Left <= xTop) && (xTop <= Right)) && ((point.get_Y() < Top) && (Top < point2.get_Y()))) { if ((((clippedPolygon.get_Item(clippedPolygon.get_Count() - 1).get_X() == Left) && (point.get_X() == Left)) && (point2.get_X() == Left)) || (((clippedPolygon.get_Item(clippedPolygon.get_Count() - 1).get_X() == Right) && (point.get_X() == Right)) && (point2.get_X() == Right))) { clippedPolygon.RemoveAt(clippedPolygon.get_Count() - 1); } else { clippedPolygon.Add(new Point(xTop, Top)); } } else if (((Top <= yRight) && (yRight <= Bottom)) && ((point2.get_X() < Right) && (Right < point.get_X()))) { if ((((clippedPolygon.get_Item(clippedPolygon.get_Count() - 1).get_Y() == Top) && (point.get_Y() == Top)) && (point2.get_Y() == Top)) || (((clippedPolygon.get_Item(clippedPolygon.get_Count() - 1).get_Y() == Bottom) && (point.get_Y() == Bottom)) && (point2.get_Y() == Bottom))) { clippedPolygon.RemoveAt(clippedPolygon.get_Count() - 1); } else { clippedPolygon.Add(new Point(Right, yRight)); } } else if (((Left <= xBottom) && (xBottom <= Right)) && ((point2.get_Y() < Bottom) && (Bottom < point.get_Y()))) { if ((((clippedPolygon.get_Item(clippedPolygon.get_Count() - 1).get_X() == Left) && (point.get_X() == Left)) && (point2.get_X() == Left)) || (((clippedPolygon.get_Item(clippedPolygon.get_Count() - 1).get_X() == Right) && (point.get_X() == Right)) && (point2.get_X() == Right))) { clippedPolygon.RemoveAt(clippedPolygon.get_Count() - 1); } else { clippedPolygon.Add(new Point(xBottom, Bottom)); } } } else { this.ClipLine(point, point2, yLeft, xTop, yRight, xBottom, Left, Top, Right, Bottom, ref clippedPolygon); } } } if (num5 > 0) { for (int m = 0; m < num5; m++) { clippedPolygon.RemoveAt(clippedPolygon.get_Count() - 1); } return clippedPolygon; } if ((clippedPolygon.get_Item(0).get_X() == clippedPolygon.get_Item(clippedPolygon.get_Count() - 1).get_X()) && (clippedPolygon.get_Item(0).get_Y() == clippedPolygon.get_Item(clippedPolygon.get_Count() - 1).get_Y())) { clippedPolygon.RemoveAt(clippedPolygon.get_Count() - 1); } return clippedPolygon; }
private void ClipLine(Point point, Point NextPoint, double yLeft, double xTop, double yRight, double xBottom, double Left, double Top, double Right, double Bottom, ref PointCollection ClippedPolygon) { if ((xTop == Left) && (yLeft == Top)) { if (((ClippedPolygon.get_Count() == 0) || (ClippedPolygon.get_Item(ClippedPolygon.get_Count() - 1).get_X() != Left)) || (ClippedPolygon.get_Item(ClippedPolygon.get_Count() - 1).get_Y() != Top)) { ClippedPolygon.Add(new Point(Left, Top)); } } else if ((xTop == Right) && (yRight == Top)) { if (((ClippedPolygon.get_Count() == 0) || (ClippedPolygon.get_Item(ClippedPolygon.get_Count() - 1).get_X() != Right)) || (ClippedPolygon.get_Item(ClippedPolygon.get_Count() - 1).get_Y() != Top)) { ClippedPolygon.Add(new Point(Right, Top)); } } else if ((xBottom == Right) && (yRight == Bottom)) { if (((ClippedPolygon.get_Count() == 0) || (ClippedPolygon.get_Item(ClippedPolygon.get_Count() - 1).get_X() != Right)) || (ClippedPolygon.get_Item(ClippedPolygon.get_Count() - 1).get_Y() != Bottom)) { ClippedPolygon.Add(new Point(Right, Bottom)); } } else if ((xBottom == Left) && (yLeft == Bottom)) { if (((ClippedPolygon.get_Count() == 0) || (ClippedPolygon.get_Item(ClippedPolygon.get_Count() - 1).get_X() != Left)) || (ClippedPolygon.get_Item(ClippedPolygon.get_Count() - 1).get_Y() != Bottom)) { ClippedPolygon.Add(new Point(Left, Bottom)); } } else if (((Top <= yLeft) && (yLeft <= Bottom)) && ((Left <= xTop) && (xTop <= Right))) { if (((point.get_X() < Left) && (point.get_Y() > Top)) && ((NextPoint.get_X() > Left) && (NextPoint.get_Y() < Top))) { ClippedPolygon.Add(new Point(Left, yLeft)); ClippedPolygon.Add(new Point(xTop, Top)); } else if (((point.get_X() > Left) && (point.get_Y() < Top)) && ((NextPoint.get_Y() > Top) && (NextPoint.get_X() < Left))) { ClippedPolygon.Add(new Point(xTop, Top)); ClippedPolygon.Add(new Point(Left, yLeft)); } } else if (((Left <= xTop) && (xTop <= Right)) && ((Top <= yRight) && (yRight <= Bottom))) { if (((point.get_X() > Right) && (point.get_Y() > Top)) && ((NextPoint.get_X() < Right) && (NextPoint.get_Y() < Top))) { ClippedPolygon.Add(new Point(Right, yRight)); ClippedPolygon.Add(new Point(xTop, Top)); } else if (((point.get_X() < Right) && (point.get_Y() < Top)) && ((NextPoint.get_X() > Right) && (NextPoint.get_Y() > Top))) { ClippedPolygon.Add(new Point(xTop, Top)); ClippedPolygon.Add(new Point(Right, yRight)); } } else if (((Left <= xBottom) && (xBottom <= Right)) && ((Top <= yRight) && (yRight <= Bottom))) { if (((point.get_X() > Right) && (point.get_Y() < Bottom)) && ((NextPoint.get_X() < Right) && (NextPoint.get_Y() > Bottom))) { ClippedPolygon.Add(new Point(Right, yRight)); ClippedPolygon.Add(new Point(xBottom, Bottom)); } else if (((point.get_X() < Right) && (point.get_Y() > Bottom)) && ((NextPoint.get_X() > Right) && (NextPoint.get_Y() < Bottom))) { ClippedPolygon.Add(new Point(xBottom, Bottom)); ClippedPolygon.Add(new Point(Right, yRight)); } } else if (((Left <= xBottom) && (xBottom <= Right)) && ((Top <= yLeft) && (yLeft <= Bottom))) { if (((point.get_X() < Left) && (point.get_Y() < Bottom)) && ((NextPoint.get_X() > Left) && (NextPoint.get_Y() > Bottom))) { ClippedPolygon.Add(new Point(Left, yLeft)); ClippedPolygon.Add(new Point(xBottom, Bottom)); } else if (((point.get_X() > Left) && (point.get_Y() > Bottom)) && ((NextPoint.get_X() < Left) && (NextPoint.get_Y() < Bottom))) { ClippedPolygon.Add(new Point(xBottom, Bottom)); ClippedPolygon.Add(new Point(Left, yLeft)); } } else if (((Left <= xTop) && (xTop <= Right)) && ((Left <= xBottom) && (xBottom <= Right))) { if ((point.get_Y() < Top) && (NextPoint.get_Y() > Bottom)) { ClippedPolygon.Add(new Point(xTop, Top)); ClippedPolygon.Add(new Point(xBottom, Bottom)); } else if ((point.get_Y() > Bottom) && (NextPoint.get_Y() < Top)) { ClippedPolygon.Add(new Point(xBottom, Bottom)); ClippedPolygon.Add(new Point(xTop, Top)); } } else if (((Top <= yLeft) && (yLeft <= Bottom)) && ((Top <= yRight) && (yRight <= Bottom))) { if ((point.get_X() < Left) && (NextPoint.get_X() > Right)) { ClippedPolygon.Add(new Point(Left, yLeft)); ClippedPolygon.Add(new Point(Right, yRight)); } else if ((point.get_X() > Right) && (NextPoint.get_X() < Left)) { ClippedPolygon.Add(new Point(Right, yRight)); ClippedPolygon.Add(new Point(Left, yLeft)); } } }