private LineSegment2D ChooseDiviser(LineSegment2D lineSegment)
        {
            for (int i = 0; i < CurrentPolygon.VertexCount; i++)
            {
                LineSegment2D Edge = CurrentPolygon.GetEdge(i);

                if (Edge == lineSegment || Edge.Intersects(lineSegment) || !Edge.LineIntersects(lineSegment))
                {
                    continue;
                }

                Point2D divPoint = lineSegment.ToLine().Intersects(Edge.ToLine());

                if (lineSegment.Contains(divPoint))
                {
                    continue;
                }

                LineSegment2D divLine = new LineSegment2D(lineSegment.LastPoint, divPoint);

                if (divLine.Contains(lineSegment.FirstPoint))
                {
                    //divLine = new LineSegment2D(lineSegment.FirstPoint, divPoint);
                    continue;
                }

                /*if (CurrentPolygon.InflectsEdge(divLine))
                 * {
                 *      int ssi = 0;
                 * }*/
                if (!CurrentPolygon.Contains(divLine))
                {
                    continue;
                }

                if (divPoint == this.LastPoint)
                {
                    ///need trace here
                    return(divLine);
                }

                if (this.isApart(divLine, this.CurrentPointIndex, this.LastPointIndex))
                {
                    return(divLine);
                }
            }

            return(null);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Get the intersect point of the two line segment.
        /// </summary>
        /// <param name="lineSegment">the ohter line segment.</param>
        /// <returns>the intersect point.</returns>
        public Point2D GetIntersectPoint(LineSegment2D lineSegment)
        {
            if (this.Intersects(lineSegment))
            {
                try
                {
                    Point2D point = this.ToLine().Intersects(lineSegment.ToLine());

                    return(point);
                }
                catch (ArgumentException e)
                {
                    string emsg = e.Message;

                    return(new Point2D(double.NaN, double.NaN));
                }
            }
            else
            {
                return(new Point2D(double.NaN, double.NaN));
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Get the intersect point of the two line segment.
        /// </summary>
        /// <param name="lineSegment">the ohter line segment.</param>
        /// <returns>the intersect point.</returns>
        public Point2D GetIntersectPoint(LineSegment2D lineSegment)
        {
            if (this.Intersects(lineSegment))
            {
                try
                {
                    Point2D point = this.ToLine().Intersects(lineSegment.ToLine());

                    return point;
                }
                catch (ArgumentException e)
                {
                    string emsg = e.Message;

                    return new Point2D(double.NaN, double.NaN);
                }
            }
            else
            {
                return new Point2D(double.NaN, double.NaN);
            }
        }
        /// <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);
            }
        }
        private LineSegment2D ChooseDiviser(LineSegment2D lineSegment)
        {
            for (int i = 0; i < CurrentPolygon.VertexCount; i++)
            {
                LineSegment2D Edge = CurrentPolygon.GetEdge(i);

                if (Edge == lineSegment || Edge.Intersects(lineSegment) || !Edge.LineIntersects(lineSegment))
                {
                    continue;
                }

                Point2D divPoint = lineSegment.ToLine().Intersects(Edge.ToLine());

                if (lineSegment.Contains(divPoint))
                {
                    continue;
                }

                LineSegment2D divLine = new LineSegment2D(lineSegment.LastPoint, divPoint);

                if (divLine.Contains(lineSegment.FirstPoint))
                {
                    //divLine = new LineSegment2D(lineSegment.FirstPoint, divPoint);
                    continue;
                }

                /*if (CurrentPolygon.InflectsEdge(divLine))
                {
                    int ssi = 0;
                }*/
                if (!CurrentPolygon.Contains(divLine))
                {
                    continue;
                }

                if (divPoint == this.LastPoint)
                {
                    ///need trace here
                    return divLine;
                }

                if (this.isApart(divLine, this.CurrentPointIndex, this.LastPointIndex))
                {
                    return divLine;
                }
            }

            return null;
        }
        /// <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);
            }
        }