Exemplo n.º 1
0
        /// <summary>
        /// Whether the two line segments inflect each other.
        /// </summary>
        /// <param name="lineSegment">the target linesegment</param>
        /// <returns>true if inflect</returns>
        public bool Inflects(LineSegment2D lineSegment)
        {
            if (this.TrulyContains(lineSegment.FirstPoint) || this.TrulyContains(lineSegment.LastPoint))
            {
                if (this.ToVector() * lineSegment.ToVector() < ZeroTolerance)
                {
                    return(true);
                }
            }

            if (this.Contains(lineSegment) || lineSegment.Contains(this))
            {
                return(true);
            }

            return(false);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Whether the two line segments inflect each other.
        /// </summary>
        /// <param name="lineSegment">the target linesegment</param>
        /// <returns>true if inflect</returns>
        public bool Inflects(LineSegment2D lineSegment)
        {
            if (this.TrulyContains(lineSegment.FirstPoint) || this.TrulyContains(lineSegment.LastPoint))
            {
                if (this.ToVector() * lineSegment.ToVector() < ZeroTolerance)
                {
                    return true;
                }
            }

            if (this.Contains(lineSegment) || lineSegment.Contains(this))
            {
                return true;
            }

            return false;
        }
        /// <summary>
        /// Construct a minimum link path of a polygon.
        /// </summary>
        public void BuildPath()
        {
            this.LinkDivisers.Clear();

            Point2D CurrentPoint = this.LastPoint;
            Point2D IntersectPoint = null;
            LineSegment2D CurrentLine = null;
            LineSegment2D IntersectLine = null;

            for (int i = this.Divisers.Count - 1; i >= 0; i--)
            {
                double position = -1.0;
                Polygon2D currentPolygon = this.SubDivision[i + 1];

                CurrentLine = this.Divisers[i];
                if (CurrentLine.Contains(CurrentPoint))
                {
                    this.LinkDivisers.Add(CurrentPoint);

                    Vector2D vector = CurrentLine.ToVector().Normal().Normalize();
                    IntersectPoint = CurrentLine.MidPoint + vector;

                    if (!this.SubDivision[i].Contains(IntersectPoint))
                    {
                        vector = -vector;
                    }

                    double length = CurrentLine.Length;

                    LineSegment2D testLine = null;

                    do
                    {
                        IntersectPoint = CurrentLine.MidPoint + vector * length;
                        length /= 2;
                        testLine = new LineSegment2D(CurrentPoint, IntersectPoint);
                    } while (!this.SubDivision[i].Contains(testLine));

                    continue;
                }

                for (int j = 0; j < currentPolygon.VertexCount; j++)
                {
                    Point2D point = currentPolygon.GetPoint(j);

                    if (CurrentPoint == point)
                    {
                        continue;
                    }

                    LineSegment2D lineSegment = new LineSegment2D(CurrentPoint, point);

                    if (currentPolygon.Contains(lineSegment) || currentPolygon.isEdge(lineSegment))
                    {

                        if (CurrentLine.LineIntersects(lineSegment))
                        {
                            IntersectPoint = CurrentLine.ToLine().Intersects(lineSegment.ToLine());
                            if (position < 0)
                            {
                                position = CurrentLine.GetPosition(IntersectPoint);
                            }
                            else
                            {
                                position += CurrentLine.GetPosition(IntersectPoint);
                                position /= 2;
                            }
                        }
                    }
                }

                if (position < -0.5)
                {
                    CurrentPoint = IntersectPoint + IntersectLine.ToVector().Normalize();
                    i++;
                    continue;
                }

                this.LinkDivisers.Add(CurrentPoint);
                IntersectPoint = CurrentLine.GetPoint(position);
                IntersectLine = new LineSegment2D(CurrentPoint, IntersectPoint);
                double extend = IntersectLine.Length;

                do
                {
                    extend /= 2;
                    CurrentPoint = IntersectLine.Extend(extend);
                } while (!this.SubDivision[i].Contains(CurrentPoint));

                //CurrentPoint = CurrentLine.GetPoint(position);
            }

            CurrentLine = new LineSegment2D(CurrentPoint, this.FirstPoint);

            if (this.Parent.Contains(CurrentLine))
            {
                this.LinkDivisers.Add(CurrentPoint);
                this.LinkDivisers.Add(this.FirstPoint);
            }
            else if (IntersectLine != null)
            {
                CurrentPoint = IntersectPoint + IntersectLine.ToVector().Normalize();
                this.LinkDivisers.Add(CurrentPoint);
                this.LinkDivisers.Add(this.FirstPoint);
            }
            else
            {
                Vector2D vector = CurrentLine.ToVector().Normal().Normalize();

                IntersectPoint = CurrentLine.MidPoint + vector;

                if (!this.Parent.Contains(IntersectPoint))
                {
                    vector = -vector;
                }

                IntersectPoint = CurrentLine.MidPoint;

                double length = CurrentLine.Length;

                do
                {
                    length /= 2;
                    CurrentPoint = IntersectPoint + vector * length;
                }
                while (!this.Parent.Contains(CurrentPoint));

                this.LinkDivisers.Add(this.LastPoint);
                this.LinkDivisers.Add(CurrentPoint);
                this.LinkDivisers.Add(this.FirstPoint);
            }
        }
        /// <summary>
        /// Construct a minimum link path of a polygon.
        /// </summary>
        public void BuildPath()
        {
            this.LinkDivisers.Clear();

            Point2D       CurrentPoint   = this.LastPoint;
            Point2D       IntersectPoint = null;
            LineSegment2D CurrentLine    = null;
            LineSegment2D IntersectLine  = null;

            for (int i = this.Divisers.Count - 1; i >= 0; i--)
            {
                double    position       = -1.0;
                Polygon2D currentPolygon = this.SubDivision[i + 1];

                CurrentLine = this.Divisers[i];
                if (CurrentLine.Contains(CurrentPoint))
                {
                    this.LinkDivisers.Add(CurrentPoint);

                    Vector2D vector = CurrentLine.ToVector().Normal().Normalize();
                    IntersectPoint = CurrentLine.MidPoint + vector;

                    if (!this.SubDivision[i].Contains(IntersectPoint))
                    {
                        vector = -vector;
                    }

                    double length = CurrentLine.Length;

                    LineSegment2D testLine = null;

                    do
                    {
                        IntersectPoint = CurrentLine.MidPoint + vector * length;
                        length        /= 2;
                        testLine       = new LineSegment2D(CurrentPoint, IntersectPoint);
                    } while (!this.SubDivision[i].Contains(testLine));

                    continue;
                }

                for (int j = 0; j < currentPolygon.VertexCount; j++)
                {
                    Point2D point = currentPolygon.GetPoint(j);

                    if (CurrentPoint == point)
                    {
                        continue;
                    }

                    LineSegment2D lineSegment = new LineSegment2D(CurrentPoint, point);

                    if (currentPolygon.Contains(lineSegment) || currentPolygon.isEdge(lineSegment))
                    {
                        if (CurrentLine.LineIntersects(lineSegment))
                        {
                            IntersectPoint = CurrentLine.ToLine().Intersects(lineSegment.ToLine());
                            if (position < 0)
                            {
                                position = CurrentLine.GetPosition(IntersectPoint);
                            }
                            else
                            {
                                position += CurrentLine.GetPosition(IntersectPoint);
                                position /= 2;
                            }
                        }
                    }
                }

                if (position < -0.5)
                {
                    CurrentPoint = IntersectPoint + IntersectLine.ToVector().Normalize();
                    i++;
                    continue;
                }

                this.LinkDivisers.Add(CurrentPoint);
                IntersectPoint = CurrentLine.GetPoint(position);
                IntersectLine  = new LineSegment2D(CurrentPoint, IntersectPoint);
                double extend = IntersectLine.Length;

                do
                {
                    extend      /= 2;
                    CurrentPoint = IntersectLine.Extend(extend);
                } while (!this.SubDivision[i].Contains(CurrentPoint));

                //CurrentPoint = CurrentLine.GetPoint(position);
            }

            CurrentLine = new LineSegment2D(CurrentPoint, this.FirstPoint);

            if (this.Parent.Contains(CurrentLine))
            {
                this.LinkDivisers.Add(CurrentPoint);
                this.LinkDivisers.Add(this.FirstPoint);
            }
            else if (IntersectLine != null)
            {
                CurrentPoint = IntersectPoint + IntersectLine.ToVector().Normalize();
                this.LinkDivisers.Add(CurrentPoint);
                this.LinkDivisers.Add(this.FirstPoint);
            }
            else
            {
                Vector2D vector = CurrentLine.ToVector().Normal().Normalize();

                IntersectPoint = CurrentLine.MidPoint + vector;

                if (!this.Parent.Contains(IntersectPoint))
                {
                    vector = -vector;
                }

                IntersectPoint = CurrentLine.MidPoint;

                double length = CurrentLine.Length;

                do
                {
                    length      /= 2;
                    CurrentPoint = IntersectPoint + vector * length;
                }while (!this.Parent.Contains(CurrentPoint));

                this.LinkDivisers.Add(this.LastPoint);
                this.LinkDivisers.Add(CurrentPoint);
                this.LinkDivisers.Add(this.FirstPoint);
            }
        }
Exemplo n.º 5
0
        private void Divide(Polygon2D poly)
        {
            for (int i = 0; i < poly.VertexCount; i++)  //Find Convex Angle
            {
                if (poly.isConvexAngle(i))
                {
                    continue;
                }
                int index = i;
                double min_dist = 10000;

                for (int j = 0; j < poly.VertexCount; j++) //Divide Polygon
                {
                    if (j == i)
                    {
                        continue;
                    }
                    LineSegment2D line = new LineSegment2D(poly.GetPoint(i), poly.GetPoint(j));

                    if (poly.isDiagonal(line))
                    {
                        //double dist = line.Length;
                        Vector2D v1 = line.ToVector();
                        LineSegment2D line1 = poly.GetEdge(i);
                        line1.SwapPoints();
                        LineSegment2D line2 = poly.GetEdge((i + 1) % poly.VertexCount);
                        Vector2D v2 = line1.ToVector();
                        Vector2D v3 = line2.ToVector();

                        double angle1 = Vector2D.Angle(v2, v1);
                        double angle2 = Vector2D.Angle(v1, v3);  //ѡȡ���ֽǶ�����ȵĵ����ָ��

                        if (angle1 < 0)
                        {
                            angle1 += Math.PI * 2;
                        }
                        if (angle2 < 0)
                        {
                            angle2 += Math.PI * 2;
                        }

                        double dist = Math.Max(angle1, angle2);
                        if (!poly.isConvexAngle(j) && dist < Math.PI)
                        {
                            dist -= Math.PI;
                        }

                        if(dist < min_dist)
                        {
                            index = j;
                            min_dist = dist;
                        }
                    }
                }
                if (index != i)
                {
                    this.DividedBy(new LineSegment2D(poly.GetPoint(i), poly.GetPoint(index)));
                    return;
                }
            }
            throw (new ArgumentException());
        }