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);
        }
Example #2
0
 /// <summary>
 /// Whether the line segment is inside the polygon.
 /// </summary>
 /// <param name="lineSegment">the line segment.</param>
 /// <returns>true if contains.</returns>
 public bool Contains(LineSegment2D lineSegment)
 {
     /*for (int i = 0; i < this.VertexCount; i++)
      * {
      *      if (lineSegment.Contains(this.GetEdge(i)))
      *      {
      *              return false;
      *      }
      * }*/
     if (this.InflectsEdge(lineSegment))
     {
         return(false);
     }
     return(!lineSegment.Intersects(this) && this.Contains(lineSegment.MidPoint) && !this.isOnEdge(lineSegment.MidPoint));
 }
        /// <summary>
        /// Whether ths line segment intersects a polygon.
        /// </summary>
        /// <param name="polygon">the polygon.</param>
        /// <returns>true if intersects.</returns>
        public bool Intersects(Polygon2D polygon)
        {
            bool bResult = false;

            for (int i = 0; i < polygon.VertexCount; i++)
            {
                LineSegment2D lineSegment = polygon.GetEdge(i);

                //bResult |= this.TrulyIntersects(polygon.GetEdge(i));
                bResult |= (this.TrulyLineIntersects(lineSegment) && lineSegment.Intersects(this));
                if (bResult)
                {
                    return(true);
                }
            }

            return(bResult);
        }
        /// <summary>
        /// Divide a sub polygon by a line segment.
        /// </summary>
        /// <param name="lineSegment">the line segment.</param>
        /// <param name="poly">the polygon to divide.</param>
        /// <returns>one of the sub polygon divided from the polygon.</returns>
        internal Polygon2D DividedBy(LineSegment2D lineSegment, Polygon2D poly)
        {
            if (poly.isDiagonal(lineSegment))
            {
                int a = poly.HasVertex(lineSegment.FirstPoint);
                int b = poly.HasVertex(lineSegment.LastPoint);

                if (a > b)
                {
                    int tmp = a;
                    a = b;
                    b = tmp;
                }

                Point2DCollection p2 = new Point2DCollection(b - a + 1);
                Point2DCollection p1 = new Point2DCollection(poly.VertexCount - p2.Size + 2);

                for (int i = 0; i < poly.VertexCount; i++)
                {
                    if (i <= a || i >= b)
                    {
                        p1.Add(poly.GetPoint(i));
                    }
                    if (i >= a && i <= b)
                    {
                        p2.Add(poly.GetPoint(i));
                    }
                }

                if (p1.Count > p2.Count)
                {
                    SubDivision.Add(new Polygon2D(p1));
                    return new Polygon2D(p2);
                }
                else
                {
                    SubDivision.Add(new Polygon2D(p2));
                    return new Polygon2D(p1);
                }
            }
            else if (lineSegment.Intersects(poly))
            {
                Point2DCollection Points = new Point2DCollection(2);

                for (int i = 0; i < poly.VertexCount; i++)
                {
                    LineSegment2D border = poly.GetEdge(i);

                    Point2D p = lineSegment.GetIntersectPoint(border);

                    if (p.isRegular)
                    {
                        Points.DistinctAdd(p);
                    }
                }

                Debug.Assert(Points.Count == 2);

                if (poly.HasVertex(Points[0]) == Polygon2D.NoSuchPoint)
                {
                    poly.Add(Points[0], poly.OnEdge(Points[0]));
                    Parent.AddInner(Points[0]);
                    this.InnerPoints.DistinctAdd(Points[0]);
                }

                if (poly.HasVertex(Points[1]) == Polygon2D.NoSuchPoint)
                {
                    poly.Add(Points[1], poly.OnEdge(Points[1]));
                    Parent.AddInner(Points[1]);
                    this.InnerPoints.DistinctAdd(Points[1]);
                }

                LineSegment2D line = new LineSegment2D(Points[0], Points[1]);

                return DividedBy(line, poly);
            }
            else
            {
                return poly;
            }
        }
        /// <summary>
        /// Divide a sub polygon by a line segment.
        /// </summary>
        /// <param name="lineSegment">the line segment.</param>
        /// <param name="poly">the polygon to divide.</param>
        /// <returns>one of the sub polygon divided from the polygon.</returns>
        internal Polygon2D DividedBy(LineSegment2D lineSegment, Polygon2D poly)
        {
            if (poly.isDiagonal(lineSegment))
            {
                int a = poly.HasVertex(lineSegment.FirstPoint);
                int b = poly.HasVertex(lineSegment.LastPoint);

                if (a > b)
                {
                    int tmp = a;
                    a = b;
                    b = tmp;
                }

                Point2DCollection p2 = new Point2DCollection(b - a + 1);
                Point2DCollection p1 = new Point2DCollection(poly.VertexCount - p2.Size + 2);

                for (int i = 0; i < poly.VertexCount; i++)
                {
                    if (i <= a || i >= b)
                    {
                        p1.Add(poly.GetPoint(i));
                    }
                    if (i >= a && i <= b)
                    {
                        p2.Add(poly.GetPoint(i));
                    }
                }

                if (p1.Count > p2.Count)
                {
                    SubDivision.Add(new Polygon2D(p1));
                    return(new Polygon2D(p2));
                }
                else
                {
                    SubDivision.Add(new Polygon2D(p2));
                    return(new Polygon2D(p1));
                }
            }
            else if (lineSegment.Intersects(poly))
            {
                Point2DCollection Points = new Point2DCollection(2);

                for (int i = 0; i < poly.VertexCount; i++)
                {
                    LineSegment2D border = poly.GetEdge(i);

                    Point2D p = lineSegment.GetIntersectPoint(border);

                    if (p.isRegular)
                    {
                        Points.DistinctAdd(p);
                    }
                }

                Debug.Assert(Points.Count == 2);

                if (poly.HasVertex(Points[0]) == Polygon2D.NoSuchPoint)
                {
                    poly.Add(Points[0], poly.OnEdge(Points[0]));
                    Parent.AddInner(Points[0]);
                    this.InnerPoints.DistinctAdd(Points[0]);
                }

                if (poly.HasVertex(Points[1]) == Polygon2D.NoSuchPoint)
                {
                    poly.Add(Points[1], poly.OnEdge(Points[1]));
                    Parent.AddInner(Points[1]);
                    this.InnerPoints.DistinctAdd(Points[1]);
                }

                LineSegment2D line = new LineSegment2D(Points[0], Points[1]);

                return(DividedBy(line, poly));
            }
            else
            {
                return(poly);
            }
        }
Example #6
0
 /// <summary>
 /// Whether the line segment is inside the polygon.
 /// </summary>
 /// <param name="lineSegment">the line segment.</param>
 /// <returns>true if contains.</returns>
 public bool Contains(LineSegment2D lineSegment)
 {
     /*for (int i = 0; i < this.VertexCount; i++)
     {
         if (lineSegment.Contains(this.GetEdge(i)))
         {
             return false;
         }
     }*/
     if (this.InflectsEdge(lineSegment))
     {
         return false;
     }
     return !lineSegment.Intersects(this) && this.Contains(lineSegment.MidPoint) && !this.isOnEdge(lineSegment.MidPoint);
 }