예제 #1
0
        public static PolyChains Sort(IList <Point2d> points, bool robust = true, double epsilon = MathUtils.EPSILON)
        {
            PolyChains pchains = new PolyChains(points);

            // NOTE: it is not necessary to find the leftmost point. It is enough with finding the start of a chain.
            int leftMost = FindLeftMost(points, epsilon);

            pchains.LeftMost = leftMost;

            IPolyEnumerator <Point2d> eFirst = NewEnumerator(points, leftMost, robust, epsilon);
            IPolyEnumerator <Point2d> e      = eFirst.Clone();

            IPolyEnumerator <Point2d> eNext = e.Clone();

            eNext.Next();
            if (eNext.Equals(eFirst))
            {
            }

            IPolyEnumerator <Point2d> first = e.Clone();
            bool?      leftRight            = null;
            bool?      bottomTop            = null;
            List <int> inflectionYPoints    = new List <int>();

            do
            {
                Point2d p     = e.Point;
                Point2d pNext = eNext.Point;
                {
                    bool?nextBT = null;
                    if (pNext.Y.EpsilonG(p.Y))
                    {
                        nextBT = true;
                    }
                    else if (pNext.Y.EpsilonL(p.Y))
                    {
                        nextBT = false;
                    }

                    if (bottomTop == null)
                    {
                        bottomTop = nextBT;
                    }
                    else
                    {
                        if ((nextBT != null) && (bottomTop != nextBT))
                        {
                            inflectionYPoints.Add(e.Index);
                        }
                    }
                }

                {
                    bool?nextLR = null;
                    if (pNext.X.EpsilonG(p.X))
                    {
                        nextLR = true;
                    }
                    else if (pNext.X.EpsilonL(p.X))
                    {
                        nextLR = false;
                    }

                    if (leftRight == null)
                    {
                        leftRight = nextLR;
                    }
                    else
                    {
                        if ((nextLR != null) && (leftRight != nextLR))
                        {
                            pchains.AddChain((bool)leftRight, first, e, inflectionYPoints);

                            first     = e.Clone();
                            leftRight = null;
                            bottomTop = null;
                            inflectionYPoints.Clear();
                        }
                    }
                }

                e.Next();
                eNext.Next();
            }while (!e.Equals(eFirst));

            if (!first.Equals(e))
            {
                pchains.AddChain((leftRight ?? true), first, e, inflectionYPoints);
            }
            return(pchains);

            /*for (int i = 1; i < points.Count + 1; i++)
             * {
             *  Point2d pprev = points[(leftMost + i - 1) % points.Count];
             *  Point2d p = points[(leftMost + i) % points.Count];
             *
             *  {
             *      bool? nextBT = null;
             *      if (p.Y.EpsilonG(pprev.Y))
             *      {
             *          nextBT = true;
             *      }
             *      else if (p.Y.EpsilonL(pprev.Y))
             *      {
             *          nextBT = false;
             *      }
             *
             *      if (bottomTop == null)
             *      {
             *          bottomTop = nextBT;
             *      }
             *      else
             *      {
             *          if ((nextBT != null) && (bottomTop != nextBT))
             *          {
             *              inflectionYPoints.Add(leftMost + i);
             *          }
             *      }
             *  }
             *
             *  {
             *      bool? nextLR = null;
             *      if (p.X.EpsilonG(pprev.X))
             *      {
             *          nextLR = true;
             *      }
             *      else if (p.X.EpsilonL(pprev.X))
             *      {
             *          nextLR = false;
             *      }
             *
             *      if (leftRight == null)
             *      {
             *          leftRight = nextLR;
             *      }
             *      else
             *      {
             *          if ((nextLR != null) && (leftRight != nextLR))
             *          {
             *              pchains.AddChain((bool)leftRight, first, (leftMost + i) - first, inflectionYPoints);
             *
             *              // Rollback
             *              i--;
             *
             *              first = leftMost + i;
             *              leftRight = null;
             *              bottomTop = null;
             *              inflectionYPoints.Clear();
             *          }
             *      }
             *  }
             * }
             *
             * int c = (leftMost + points.Count + 1) - first;
             * if (c > 1)
             * {
             *  pchains.AddChain((leftRight ?? true), first, c, inflectionYPoints);
             * }
             * return pchains;*/
        }
예제 #2
0
        public static IList <IList <Point2d> > Sort2(IList <Point2d> points)
        {
            PolyChains pchains = Sort(points);

            return(pchains.GetMonotoneChains());
        }