//offsets an input line by a given distance inside a poly
        public static Line2d OffsetLineInsidePoly(Line2d lineInp, Polygon2d poly, double distance)
        {
            if (lineInp == null || !ValidateObject.CheckPoly(poly))
            {
                return(null);
            }
            Point2d  ptStart = OffsetLinePointInsidePoly(lineInp, lineInp.StartPoint, poly, distance);
            Vector2d vec     = new Vector2d(lineInp.StartPoint, ptStart);
            Point2d  ptEnd   = VectorUtility.VectorAddToPoint(lineInp.EndPoint, vec);

            return(new Line2d(ptStart, ptEnd));
        }
        //offsets an input line by a given distance
        public static Line2d Offset(Line2d lineInp, double distance)
        {
            if (lineInp == null)
            {
                return(null);
            }
            Point2d  ptStart = OffsetLinePoint(lineInp, lineInp.StartPoint, distance);
            Vector2d vec     = new Vector2d(lineInp.StartPoint, ptStart);
            Point2d  ptEnd   = VectorUtility.VectorAddToPoint(lineInp.EndPoint, vec);

            return(new Line2d(ptStart, ptEnd));
        }
示例#3
0
        internal static Dictionary <string, object> AddPointToFitPoly(Polygon2d poly, Polygon2d containerPoly, double distance = 16, double area = 0, double thresDistance = 10, double recompute = 5)
        {
            if (!ValidateObject.CheckPoly(poly))
            {
                return(null);
            }
            if (distance < 1)
            {
                return(null);
            }

            Dictionary <string, object> lineOffsetCheckObj = ValidateObject.CheckLinesOffsetInPoly(poly, containerPoly, distance);
            List <int>             indicesFalse            = (List <int>)lineOffsetCheckObj["IndicesFalse"];
            List <List <Point2d> > pointsFalse             = (List <List <Point2d> >)lineOffsetCheckObj["PointsOutside"];
            List <Point2d>         probPointList           = new List <Point2d>();
            List <Point2d>         polyNewPoints           = new List <Point2d>();
            List <Line2d>          falseLines = new List <Line2d>();
            Point2d ptNewEnd = new Point2d(0, 0);
            Point2d otherPt = new Point2d(0, 0);
            Point2d probPt = new Point2d(0, 0);
            Line2d  line = new Line2d(ptNewEnd, otherPt);
            int     count = 0, maxTry = 50;
            double  ratio = 0, increment = .25;
            bool    added = false, checkOffPtNew = false;

            for (int i = 0; i < poly.Points.Count; i++)
            {
                int a = i, b = i + 1;
                if (i == poly.Points.Count - 1)
                {
                    b = 0;
                }
                polyNewPoints.Add(poly.Points[a]);
                if (indicesFalse[i] > -1)
                {
                    falseLines.Add(poly.Lines[i]);
                }
                if (poly.Lines[i].Length > thresDistance &&
                    indicesFalse[i] > -1 && pointsFalse[i] != null && pointsFalse[i].Count == 1 && !added && LayoutUtility.CheckLineGetsExternalWall(poly.Lines[i], containerPoly))
                {
                    probPointList.AddRange(pointsFalse[i]);
                    probPt = pointsFalse[i][0];
                    line   = poly.Lines[i];
                    Point2d midPt = LineUtility.LineMidPoint(line);
                    if (line.StartPoint.Compare(probPt))
                    {
                        otherPt = line.EndPoint;
                    }
                    else
                    {
                        otherPt = line.StartPoint;
                    }
                    Vector2d vecToOther = new Vector2d(probPt, otherPt);
                    while (!checkOffPtNew && count < maxTry && ratio < 0.9)
                    {
                        ratio   += increment;
                        ptNewEnd = VectorUtility.VectorAddToPoint(probPt, vecToOther, ratio);
                        Point2d offPtNew = LineUtility.OffsetLinePointInsidePoly(line, ptNewEnd, poly, distance);
                        checkOffPtNew = GraphicsUtility.PointInsidePolygonTest(poly, offPtNew);
                        count        += 1;
                    }
                    polyNewPoints.Add(ptNewEnd);
                    added = true;
                }
            }
            Polygon2d polyAdded = new Polygon2d(polyNewPoints, 0);

            return(new Dictionary <string, object>
            {
                { "PolyAddedPts", (polyAdded) },
                { "ProblemPoint", (probPt) },
                { "IsAdded", (added) },
                { "PointAdded", (ptNewEnd) },
                { "Trials", (count) },
                { "FinalRatio", (ratio) },
                { "ProblemLine", (line) },
                { "ProblemPtsList", (probPointList) },
                { "FalseLineList", (falseLines) }
            });
        }