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) }
            });
        }