public static Dictionary <string, object> SplitByDistance(Polygon2d polyOutline, Random ran, double distance = 10, int dir = 0, double spacing = 0) { if (!ValidateObject.CheckPoly(polyOutline)) { return(null); } double extents = 5000, spacingProvided; List <Point2d> polyOrig = polyOutline.Points; if (spacing == 0) { spacingProvided = BuildLayout.SPACING; } else { spacingProvided = spacing; } List <Point2d> poly = PolygonUtility.SmoothPolygon(polyOrig, spacingProvided); if (!ValidateObject.CheckPointList(poly)) { return(null); } Dictionary <int, object> obj = PointUtility.PointSelector(ran, poly); Point2d pt = (Point2d)obj[0]; int orient = (int)obj[1]; Line2d splitLine = new Line2d(pt, extents, dir); // push this line right or left or up or down based on ratio if (dir == 0) { splitLine = LineUtility.Move(splitLine, 0, orient * distance); } else { splitLine = LineUtility.Move(splitLine, orient * distance, 0); } Dictionary <string, object> intersectionReturn = MakeIntersections(poly, splitLine, spacingProvided); List <Point2d> intersectedPoints = (List <Point2d>)intersectionReturn["IntersectedPoints"]; List <Polygon2d> splittedPoly = (List <Polygon2d>)intersectionReturn["PolyAfterSplit"]; List <Point2d> ptA = (List <Point2d>)intersectionReturn["PointASide"]; List <Point2d> ptB = (List <Point2d>)intersectionReturn["PointBSide"]; Polygon2d polyA = new Polygon2d(ptA, 0), polyB = new Polygon2d(ptB, 0); //List<Polygon2d> splittedPoly = new List<Polygon2d>(); //splittedPoly.Add(polyA); splittedPoly.Add(polyB); return(new Dictionary <string, object> { { "PolyAfterSplit", (splittedPoly) }, { "SplitLine", (splitLine) }, { "IntersectedPoints", (intersectedPoints) }, { "PointASide", (ptA) }, { "PointBSide", (ptB) } }); }
public static Dictionary <string, object> SplitByDistanceFromPoint(Polygon2d polyOutline, double distance = 10, int dir = 0, double space = 0) { if (polyOutline == null || polyOutline.Points == null || polyOutline.Points.Count == 0) { return(null); } if (space == 0) { space = BuildLayout.SPACING2; } double extents = 5000; int threshValue = 50; List <Point2d> polyOrig = polyOutline.Points; List <Point2d> poly = new List <Point2d>(); if (polyOrig.Count > threshValue) { poly = polyOrig; } else { poly = PolygonUtility.SmoothPolygon(polyOrig, space); } if (poly == null || poly.Count == 0) { return(null); } int lowInd = PointUtility.LowestPointFromList(poly); Point2d lowPt = poly[lowInd]; Line2d splitLine = new Line2d(lowPt, extents, dir); if (dir == 0) { splitLine = LineUtility.Move(splitLine, 0, 1 * distance); } else { splitLine = LineUtility.Move(splitLine, 1 * distance, 0); } Dictionary <string, object> intersectionReturn = MakeIntersections(poly, splitLine, space); List <Point2d> intersectedPoints = (List <Point2d>)intersectionReturn["IntersectedPoints"]; List <Polygon2d> splittedPoly = (List <Polygon2d>)intersectionReturn["PolyAfterSplit"]; List <Point2d> ptA = (List <Point2d>)intersectionReturn["PointASide"]; List <Point2d> ptB = (List <Point2d>)intersectionReturn["PointBSide"]; return(new Dictionary <string, object> { { "PolyAfterSplit", (splittedPoly) }, { "SplitLine", (splitLine) }, { "IntersectedPoints", (intersectedPoints) }, { "PointASide", (ptA) }, { "PointBSide", (ptB) } }); }
public static Dictionary <string, object> SplitByLine(Polygon2d polyOutline, Line2d inputLine, double distance = 5) { if (!ValidateObject.CheckPoly(polyOutline)) { return(null); } List <Point2d> polyOrig = polyOutline.Points; List <Point2d> poly = PolygonUtility.SmoothPolygon(polyOrig, BuildLayout.SPACING); Line2d splitLine = new Line2d(inputLine); Point2d centerPoly = PointUtility.CentroidInPointLists(poly); bool checkSide = ValidateObject.CheckPointSide(splitLine, centerPoly); int orient = ValidateObject.CheckLineOrient(splitLine); if (orient == 0) { if (!checkSide) { splitLine = LineUtility.Move(splitLine, 0, -1 * distance); } else { splitLine = LineUtility.Move(splitLine, 0, 1 * distance); } } else { if (checkSide) { splitLine = LineUtility.Move(splitLine, -1 * distance, 0); } else { splitLine = LineUtility.Move(splitLine, 1 * distance, 0); } } Dictionary <string, object> intersectionReturn = MakeIntersections(poly, splitLine, BuildLayout.SPACING); List <Polygon2d> splittedPoly = (List <Polygon2d>)intersectionReturn["PolyAfterSplit"]; return(new Dictionary <string, object> { { "PolyAfterSplit", (splittedPoly) }, { "SplitLine", (splitLine) } }); }
public static Dictionary <string, object> SplitByRatio(Polygon2d polyOutline, double ratio = 0.5, int dir = 0) { if (polyOutline == null) { return(null); } if (polyOutline != null && polyOutline.Points == null) { return(null); } double extents = 5000; double minimumLength = 2, minWidth = 10, aspectRatio = 0, eps = 0.1; List <Point2d> polyOrig = polyOutline.Points; List <Point2d> poly = PolygonUtility.SmoothPolygon(polyOrig, BuildLayout.SPACING); List <double> spans = PolygonUtility.GetSpansXYFromPolygon2d(poly); double horizontalSpan = spans[0], verticalSpan = spans[1]; Point2d polyCenter = PolygonUtility.CentroidOfPoly(Polygon2d.ByPoints(poly)); if (horizontalSpan < minimumLength || verticalSpan < minimumLength) { return(null); } if (horizontalSpan > verticalSpan) { dir = 1; aspectRatio = horizontalSpan / verticalSpan; } else { dir = 0; aspectRatio = verticalSpan / horizontalSpan; } // adjust ratio if (ratio < 0.15) { ratio = ratio + eps; } if (ratio > 0.85) { ratio = ratio - eps; } if (horizontalSpan < minWidth || verticalSpan < minWidth) { ratio = 0.5; } Line2d splitLine = new Line2d(polyCenter, extents, dir); double shift = ratio - 0.5; if (dir == 0) { splitLine = LineUtility.Move(splitLine, 0, shift * verticalSpan); } else { splitLine = LineUtility.Move(splitLine, shift * horizontalSpan, 0); } Dictionary <string, object> intersectionReturn = MakeIntersections(poly, splitLine, BuildLayout.SPACING); List <Point2d> intersectedPoints = (List <Point2d>)intersectionReturn["IntersectedPoints"]; List <Polygon2d> splittedPoly = (List <Polygon2d>)intersectionReturn["PolyAfterSplit"]; return(new Dictionary <string, object> { { "PolyAfterSplit", (splittedPoly) }, { "SplitLine", (splitLine) }, { "IntersectedPoints", (intersectedPoints) } }); }
internal static Dictionary <string, object> PolygonPolygonCommonEdgeDict(Polygon2d poly, Polygon2d other) { bool check = false; if (poly == null || other == null) { return(null); } double eps = 200; Polygon2d polyReg = new Polygon2d(poly.Points); Polygon2d otherReg = new Polygon2d(other.Points); Dictionary <string, object> UpdatedCenters = ComputePolyCentersAlign(polyReg, otherReg); Point2d centerPoly = (Point2d)UpdatedCenters["CenterPolyA"]; Point2d centerOther = (Point2d)UpdatedCenters["CenterPolyB"]; polyReg = (Polygon2d)UpdatedCenters["PolyA"]; otherReg = (Polygon2d)UpdatedCenters["PolyB"]; //make vectors Vector2d centerToCen = new Vector2d(centerPoly, centerOther); Vector2d centerToCenX = new Vector2d(centerToCen.X, 0); Vector2d centerToCenY = new Vector2d(0, centerToCen.Y); //make centerLine Line2d centerLine = new Line2d(centerPoly, centerOther); Vector2d keyVec; if (centerToCenX.Length > centerToCenY.Length) { keyVec = new Vector2d(centerToCenX.X, centerToCenX.Y); } else { keyVec = new Vector2d(centerToCenY.X, centerToCenY.Y); } //check line poly intersection between centertocen vector and each polys Line2d lineInPolyReg = CodeToBeTested.LinePolygonIntersectionReturnLine(polyReg.Points, centerLine, centerOther); Line2d lineInOtherReg = CodeToBeTested.LinePolygonIntersectionReturnLine(otherReg.Points, centerLine, centerPoly); //find distance d1 and d2 from two centers to linepolyintersection line Point2d projectedPtOnPolyReg = GraphicsUtility.ProjectedPointOnLine(lineInPolyReg, centerPoly); Point2d projectedPtOnOtherReg = GraphicsUtility.ProjectedPointOnLine(lineInOtherReg, centerOther); double dist1 = PointUtility.DistanceBetweenPoints(centerPoly, projectedPtOnPolyReg); double dist2 = PointUtility.DistanceBetweenPoints(centerOther, projectedPtOnOtherReg); double totalDistance = 2 * (dist1 + dist2); Line2d lineMoved = new Line2d(lineInPolyReg.StartPoint, lineInPolyReg.EndPoint); lineMoved = LineUtility.Move(lineMoved, centerPoly); Point2d projectedPt = GraphicsUtility.ProjectedPointOnLine(lineMoved, centerOther); double distance = PointUtility.DistanceBetweenPoints(projectedPt, centerOther); bool isNeighbour = false; if (totalDistance - eps < distance && distance < totalDistance + eps) { isNeighbour = true; } else { isNeighbour = false; } return(new Dictionary <string, object> { { "Neighbour", (isNeighbour) }, { "SharedEdgeA", (lineInPolyReg) }, { "SharedEdgeB", (lineInOtherReg) }, { "LineMoved", (lineMoved) }, { "CenterToCenterLine", (centerLine) }, { "CenterPolyPoint", (centerPoly) }, { "CenterPolyOtherPoint", (centerOther) }, }); }