コード例 #1
0
ファイル: Polygon.cs プロジェクト: pskrip/elmanager
        internal List <Polygon> Cut(Vector v1, Vector v2, double cutRadius)
        {
            var clone = new Polygon(this);

            Vector.MarkDefault = VectorMark.Selected;
            int numberOfIntersections = 0;

            for (int i = 0; i < Vertices.Count; i++)
            {
                Vector isectPoint = GeometryUtils.GetIntersectionPoint(this[i], this[i + 1], v1, v2);
                if ((object)isectPoint != null)
                {
                    clone.InsertIntersection(isectPoint, Constants.Tolerance);
                    numberOfIntersections++;
                }
            }

            Vector.MarkDefault = VectorMark.None;
            if (numberOfIntersections % 2 == 0 && numberOfIntersections > 0)
            {
                var result = new List <Polygon> {
                    new Polygon()
                };
                int  k           = 0;
                bool isBeginning = true;
                while (k != clone.Count)
                {
                    if (clone.Vertices[k].Mark == VectorMark.Selected)
                    {
                        Vector cutVector = clone[k + 1] - clone[k - 1];
                        cutVector /= cutVector.Length;
                        var angleAbs = Math.Abs(cutVector.AngleBetween(v2 - v1));
                        if (angleAbs < Constants.Tolerance)
                        {
                            return(null);
                        }

                        cutVector *= cutRadius / Math.Sin(angleAbs * Constants.DegToRad);
                        double distance1 = (clone[k + 1] - clone[k]).Length;
                        double distance2 = (clone[k] - clone[k - 1]).Length;
                        if (cutVector.Length > Math.Min(distance1, distance2))
                        {
                            cutVector = cutVector / cutVector.Length * Math.Min(distance1, distance2) / 2;
                        }
                        if (!isBeginning)
                        {
                            result[result.Count - 1].Add(clone.Vertices[k] - cutVector);
                            result[0].Add(clone.Vertices[k] + cutVector);
                        }
                        else
                        {
                            result.Add(new Polygon());
                            result[result.Count - 1].Add(clone.Vertices[k] + cutVector);
                            result[0].Add(clone.Vertices[k] - cutVector);
                        }

                        isBeginning = !isBeginning;
                    }
                    else
                    {
                        if (isBeginning)
                        {
                            result[0].Add(clone.Vertices[k]);
                        }
                        else
                        {
                            result[result.Count - 1].Add(clone.Vertices[k]);
                        }
                    }

                    k++;
                }

                result.ForEach(p => p.IsGrass = IsGrass);
                return(result.Any(polygon => polygon.Count < 3) ? null : result);
            }

            return(null);
        }