public static List <IContourPoint> TrectAngle(this TRect_Float rect_Float) { return(new List <IContourPoint> { new ContourPoint(rect_Float.X1, rect_Float.Y1, 0), new ContourPoint(rect_Float.X1, rect_Float.Y2, 0), new ContourPoint(rect_Float.X2, rect_Float.Y2, 0), new ContourPoint(rect_Float.X2, rect_Float.Y1, 0), }); }
public IContour CutContoursByWindow(IContours contours, TRect_Float rect_Float) { TContourBitEdit pointRes = new TContourBitEdit(); TContourEdit contourRes = new TContourEdit(); IContour contour; for (int i = 0; i < contours.GetContourCount(); i++) { contour = contours.GetContour(i); for (int y = 0; y < contour.GetContourBitCount(); y++) { bool lastPointAdd = false; for (int p = 0; p < contour.GetContourBit(y).GetPointCount(); p++) { if (p == contour.GetContourBit(y).GetPointCount() - 1 && y == contour.GetContourBitCount() - 1 && !contour.GetContourBit(y).IsClosed()) { break; } IContourPoint point1 = contour.GetContourBit(y).GetPoint(p); IContourPoint point2 = null; //если еще есть точки if (p < contour.GetContourBit(y).GetPointCount() - 1) { point2 = contour.GetContourBit(y).GetPoint(p + 1); } //когда последняя точка в контурбите но контурбиты еще есть (проверка на замыкание по идеи излишне) else if (y < contour.GetContourBitCount() - 1 && !contour.GetContourBit(y).IsClosed()) { point2 = contour.GetContourBit(y + 1).GetPoint(0); } //когда последняя точка в последнем контурбит, контурбит замкнут else if (y == contour.GetContourBitCount() - 1 && contour.GetContourBit(y).IsClosed()) { point2 = contour.GetContourBit(0).GetPoint(0); } //когда последняя точка в последнем контурбит, контурбит не замкнут else if (y == contour.GetContourBitCount() - 1 && !contour.GetContourBit(y).IsClosed()) { break; } ; List <(double z, double x, double y)> z = new List <(double z, double x, double y)>(); for (int j = 0; j < 4; j++) { IContourPoint pointbuf1 = rect_Float.TrectAngle()[j]; IContourPoint pointbuf2 = rect_Float.TrectAngle()[(j + 1) > 3 ? 0 : j + 1]; IContourPoint pointSide1 = pointbuf1.GetX() + pointbuf1.GetY() < pointbuf2.GetX() + pointbuf2.GetY() ? pointbuf1 : pointbuf2; IContourPoint pointSide2 = pointbuf1.GetX() + pointbuf1.GetY() > pointbuf2.GetX() + pointbuf2.GetY() ? pointbuf1 : pointbuf2; // вычисление коэфф z, если z<0 то отрезки пересекаются, если z=0 лежат на одной прямой z.Add(ZCalc(pointSide1, pointSide2, point1, point2)); if (z[j].z < 0) { //если пересекаются if (z[j].x >= pointSide1.GetX() && z[j].x <= pointSide2.GetX() && z[j].y >= pointSide1.GetY() && z[j].y <= pointSide2.GetY()) { if (!lastPointAdd) { pointRes.AddPoint(point1.GetX(), point1.GetY(), 0); lastPointAdd = true; } pointRes.AddPoint(point2.GetX(), point2.GetY(), 0); break; } else { lastPointAdd = false; } } else if (z[j].z == 0) { bool isCrossSide = CrossSide(pointSide1, pointSide2, point1, point2); if (isCrossSide) { if (!lastPointAdd) { pointRes.AddPoint(point1.GetX(), point1.GetY(), 0); lastPointAdd = true; } pointRes.AddPoint(point2.GetX(), point2.GetY(), 0); break; } } else if (z.Count == 4) { lastPointAdd = false; } } ; } if (pointRes.GetPointCount() > 0) { pointRes.SetClosed(contour.GetContourBit(y).IsClosed()); contourRes.AddContourBit(pointRes); } } } return(contourRes); }