//sorts a list of points form a polyline public static List <Point2d> SortPoints(List <Point2d> pointList) { if (!ValidateObject.CheckPointList(pointList)) { return(null); } Point2d cen = PolygonUtility.CentroidOfPoly(Polygon2d.ByPoints(pointList)); Vector2d vecX = new Vector2d(0, 100); List <double> angList = new List <double>(); int[] indList = new int[pointList.Count]; for (int i = 0; i < pointList.Count; i++) { Vector2d CenterToPt = new Vector2d(cen, pointList[i]); double dotValue = vecX.Dot(CenterToPt); double angValue = Math.Atan2(CenterToPt.Y - vecX.Y, CenterToPt.X - vecX.X); angList.Add(angValue); indList[i] = i; } List <int> newIndexList = new List <int>(); newIndexList = BasicUtility.SortIndex(angList); List <Point2d> sortedPointList = new List <Point2d>(); for (int i = 0; i < pointList.Count; i++) { sortedPointList.Add(pointList[newIndexList[i]]); } return(sortedPointList); }
//make the given poly all points orthonogonal to each other internal static Polygon2d MakePolyPointsOrtho(Polygon2d poly) { Polygon2d polyReg = new Polygon2d(poly.Points); List <Point2d> ptForOrthoPoly = new List <Point2d>(); for (int i = 0; i < polyReg.Points.Count; i++) { Point2d pt = Point2d.ByCoordinates(polyReg.Points[i].X, polyReg.Points[i].Y); ptForOrthoPoly.Add(pt); } for (int i = 0; i < polyReg.Points.Count; i++) { int a = i, b = i + 1; double eps = 50; if (i == polyReg.Points.Count - 1) { b = 0; } Line2d line = new Line2d(polyReg.Points[a], polyReg.Points[b]); if (ValidateObject.CheckLineOrient(line) == -1) { //double diffX = Math.Abs(line.StartPoint.X - line.EndPoint.X); //double diffY = Math.Abs(line.StartPoint.Y - line.EndPoint.Y); Point2d cenPoly = PolygonUtility.CentroidOfPoly(polyReg); Point2d ptEndA = Point2d.ByCoordinates(polyReg.Points[a].X + eps, polyReg.Points[a].Y); Line2d refLineA = Line2d.ByStartPointEndPoint(polyReg.Points[a], ptEndA); Point2d ptEndB = Point2d.ByCoordinates(polyReg.Points[b].X + eps, polyReg.Points[b].Y); Line2d refLineB = Line2d.ByStartPointEndPoint(polyReg.Points[b], ptEndB); Point2d projectedPtA = GraphicsUtility.ProjectedPointOnLine(refLineB, polyReg.Points[a]); Point2d projectedPtB = GraphicsUtility.ProjectedPointOnLine(refLineA, polyReg.Points[b]); Vector2d vecA = new Vector2d(projectedPtA, cenPoly); Vector2d vecB = new Vector2d(projectedPtB, cenPoly); double vecALength = vecA.Length; double vecBLength = vecB.Length; if (vecALength > vecBLength) { //ptForOrthoPoly[i] = projectedPtA; ptForOrthoPoly.Insert(b, projectedPtB); } else { //ptForOrthoPoly[i] = projectedPtB; ptForOrthoPoly.Insert(b, projectedPtA); } /* * if (diffX > diffY) * { * Point2d ptEndA = Point2d.ByCoordinates(polyReg.Points[a].X, polyReg.Points[a].Y + eps); * Line2d refLineA = Line2d.ByStartPointEndPoint(polyReg.Points[a], ptEndA); * refLineA = LineUtility.extend(refLineA); * * Point2d ptEndB = Point2d.ByCoordinates(polyReg.Points[b].X, polyReg.Points[b].Y + eps); * Line2d refLineB = Line2d.ByStartPointEndPoint(polyReg.Points[b], ptEndB); * refLineB = LineUtility.extend(refLineB); * * Point2d projectedPtA = GraphicsUtility.ProjectedPointOnLine(refLineB, polyReg.Points[a]); * Point2d projectedPtB = GraphicsUtility.ProjectedPointOnLine(refLineA, polyReg.Points[b]); * * Vector2d vecA = new Vector2d(projectedPtA, cenPoly); * Vector2d vecB = new Vector2d(projectedPtB, cenPoly); * double vecALength = vecA.Length; * double vecBLength = vecB.Length; * if(vecALength < vecBLength) * { * //ptForOrthoPoly[i] = projectedPtA; * ptForOrthoPoly.Insert(b, projectedPtB); * } * else * { * //ptForOrthoPoly[i] = projectedPtB; * ptForOrthoPoly.Insert(b, projectedPtA); * } * } * else * { * * Point2d ptEndA = Point2d.ByCoordinates(polyReg.Points[a].X + eps, polyReg.Points[a].Y); * Line2d refLineA = Line2d.ByStartPointEndPoint(polyReg.Points[a], ptEndA); * refLineA = LineUtility.extend(refLineA); * * Point2d ptEndB = Point2d.ByCoordinates(polyReg.Points[b].X + eps, polyReg.Points[b].Y); * Line2d refLineB = Line2d.ByStartPointEndPoint(polyReg.Points[b], ptEndB); * refLineB = LineUtility.extend(refLineB); * * Point2d projectedPtA = GraphicsUtility.ProjectedPointOnLine(refLineB, polyReg.Points[a]); * Point2d projectedPtB = GraphicsUtility.ProjectedPointOnLine(refLineA, polyReg.Points[b]); * * Vector2d vecA = new Vector2d(projectedPtA, cenPoly); * Vector2d vecB = new Vector2d(projectedPtB, cenPoly); * double vecALength = vecA.Length; * double vecBLength = vecB.Length; * if (vecALength < vecBLength) * { * //ptForOrthoPoly[i] = projectedPtA; * ptForOrthoPoly.Insert(b, projectedPtB); * } * else * { * //ptForOrthoPoly[i] = projectedPtB; * ptForOrthoPoly.Insert(b, projectedPtB); * } * } */ } } return(new Polygon2d(ptForOrthoPoly)); }
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) } }); }