private void AddBoundPairsToActiveEdges(float bottomY)
        {
            int i = m_localMinimaList.Count - 1;
            while (i >= 0)
            {
                if (m_localMinimaList[i].MinimumCoordinate != bottomY)
                    break;

                int boundPairIndex = m_usedBoundPairs.Count;
                m_usedBoundPairs.Add(m_localMinimaList[i]);

                var parentPoly = m_localMinimaList[i].Parent;
                MyPolygon.Vertex minimumVertex, prevVertex, nextVertex;
                parentPoly.GetVertex(m_localMinimaList[i].Minimum, out minimumVertex);
                parentPoly.GetVertex(minimumVertex.Prev, out prevVertex);
                parentPoly.GetVertex(minimumVertex.Next, out nextVertex);
                Debug.Assert(minimumVertex.Coord.Y == bottomY);

                PolygonType polyType = parentPoly == m_polyA ? PolygonType.SUBJECT : PolygonType.CLIP;

                Edge leftEdge = PrepareActiveEdge(boundPairIndex, ref minimumVertex, ref nextVertex, polyType, Side.LEFT);
                Edge rightEdge = PrepareActiveEdge(boundPairIndex, ref minimumVertex, ref prevVertex, polyType, Side.RIGHT);

                int insertPosition = SortInMinimum(ref leftEdge, ref rightEdge, polyType);
                if (leftEdge.Contributing)
                {
                    Debug.Assert(rightEdge.Contributing, "Contributing status of minimum edges was not consistent!");
                    var newPoly = new PartialPolygon();
                    newPoly.Append(minimumVertex.Coord);
                    leftEdge.AssociatedPolygon = newPoly;
                    rightEdge.AssociatedPolygon = newPoly;
                }
                else
                {
                    Debug.Assert(!rightEdge.Contributing, "Contributing status of minimum edges was not consistent!");
                }

                if (leftEdge.DXdy > rightEdge.DXdy)
                {
                    /*leftEdge.OutputSide = Side.RIGHT;
                    rightEdge.OutputSide = Side.LEFT;*/
                    m_activeEdgeList.Insert(insertPosition, rightEdge);
                    m_activeEdgeList.Insert(insertPosition + 1, leftEdge);
                }
                else
                {
                    Debug.Assert(leftEdge.DXdy < rightEdge.DXdy, "Overlapping edges in an input polygon!");
                    m_activeEdgeList.Insert(insertPosition, leftEdge);
                    m_activeEdgeList.Insert(insertPosition + 1, rightEdge);
                }

                --i;
            }

            int removeCount = m_localMinimaList.Count - 1 - i;
            m_localMinimaList.RemoveRange(i + 1, removeCount);
        }
        private void PerformIntersection(int leftEdgeIndex, int rightEdgeIndex, ref Edge e1, ref Edge e2, ref Vector3 intersectionPosition, IntersectionClassification intersectionClassification)
        {
            Debug.Assert(intersectionClassification != IntersectionClassification.INVALID);
            switch (intersectionClassification)
            {
                case IntersectionClassification.LIKE_INTERSECTION:
                    Debug.Assert((e1.Contributing && e2.Contributing) || (!e1.Contributing && !e2.Contributing));
                    Debug.Assert((e1.OutputSide == Side.LEFT && e2.OutputSide == Side.RIGHT) || (e1.OutputSide == Side.RIGHT && e2.OutputSide == Side.LEFT));

                    Side swapSide = e1.OutputSide;
                    e1.OutputSide = e2.OutputSide;
                    e2.OutputSide = swapSide;

                    if (e1.Contributing)
                    {
                        if (e1.OutputSide == Side.RIGHT)
                        {
                            e1.AssociatedPolygon.Append(intersectionPosition);
                            e2.AssociatedPolygon.Prepend(intersectionPosition);
                        }
                        else
                        {
                            e1.AssociatedPolygon.Prepend(intersectionPosition);
                            e2.AssociatedPolygon.Append(intersectionPosition);
                        }
                    }
                    break;
                case IntersectionClassification.LEFT_E1_INTERMEDIATE:
                    e1.AssociatedPolygon.Append(intersectionPosition);
                    break;
                case IntersectionClassification.RIGHT_E1_INTERMEDIATE:
                    e1.AssociatedPolygon.Prepend(intersectionPosition);
                    break;
                case IntersectionClassification.LEFT_E2_INTERMEDIATE:
                    e2.AssociatedPolygon.Append(intersectionPosition);
                    break;
                case IntersectionClassification.RIGHT_E2_INTERMEDIATE:
                    e2.AssociatedPolygon.Prepend(intersectionPosition);
                    break;
                case IntersectionClassification.LOCAL_MINIMUM:
                    var newPoly = new PartialPolygon();
                    newPoly.Append(intersectionPosition);
                    e1.AssociatedPolygon = newPoly;
                    e2.AssociatedPolygon = newPoly;
                    break;
                case IntersectionClassification.LOCAL_MAXIMUM:
                    AddLocalMaximum(leftEdgeIndex, rightEdgeIndex, ref e1, ref e2, intersectionPosition);
                    break;
            }

            PartialPolygon swap = e1.AssociatedPolygon;
            e1.AssociatedPolygon = e2.AssociatedPolygon;
            e2.AssociatedPolygon = swap;

            if (intersectionClassification != IntersectionClassification.LIKE_INTERSECTION)
            {
                e1.Contributing = !e1.Contributing;
                e2.Contributing = !e2.Contributing;
            }

            m_activeEdgeList[leftEdgeIndex] = e2;
            m_activeEdgeList[rightEdgeIndex] = e1;
        }
        private Edge FindOtherPolygonEdge(PartialPolygon polygon, int thisEdgeIndex, out int otherEdgeIndex)
        {
            Debug.Assert(m_activeEdgeList.Where(edge => edge.AssociatedPolygon == polygon).Count() == 2);
            for (int i = 0; i < m_activeEdgeList.Count; ++i)
            {
                if (i == thisEdgeIndex) continue;

                if (m_activeEdgeList[i].AssociatedPolygon == polygon)
                {
                    otherEdgeIndex = i;
                    return m_activeEdgeList[i];
                }
            }

            Debug.Assert(false);
            otherEdgeIndex = -1;
            return default(Edge);
        }
 public void Add(PartialPolygon other)
 {
     if (other.m_vertices.Count == 0) return;
     // Append the first vertex with the check and the rest without
     Append(other.m_vertices[0]);
     for (int i = 1; i < other.m_vertices.Count; ++i)
     {
         m_vertices.Add(other.m_vertices[i]);
     }
 }