private void BuildIntersectionList(float bottom, float top)
        {
            Debug.Assert(m_intersectionList.Count == 0);

            float dy = top - bottom;
            float invDy = 1.0f / dy;

            SortedEdgeEntry entry = new SortedEdgeEntry();
            GetSortedEdgeEntry(bottom, top, dy, 0, ref entry);
            m_sortedEdgeList.Add(entry);

            for (int i = 1; i < m_activeEdgeList.Count; ++i)
            {
                GetSortedEdgeEntry(bottom, top, dy, i, ref entry);

                int j = m_sortedEdgeList.Count - 1;
                while (j >= 0)
                {
                    SortedEdgeEntry otherEntry = m_sortedEdgeList[j];

                    if (CompareSortedEdgeEntries(ref otherEntry, ref entry) == -1) break;

                    float x, y;

                    /*if (entry.DX - otherEntry.DX == 0.0f)
                    {
                        x = entry.XCoord + entry.DX * 0.5f;
                        y = (bottom + top) * 0.5f;
                    }
                    else
                    {*/
                        float invDxDiff = 1.0f / (entry.DX - otherEntry.DX);
                        y = (otherEntry.QNumerator - entry.QNumerator) * invDxDiff;
                        x = entry.DX * invDy * y + entry.QNumerator * invDy;
                        Debug.Assert(Math.Abs(otherEntry.DX * invDy * y + otherEntry.QNumerator * invDy - x) < 0.001f);
                    //}

                    IntersectionListEntry intersection = new IntersectionListEntry();
                    intersection.RIndex = entry.Index;
                    intersection.LIndex = otherEntry.Index;
                    intersection.X = x;
                    intersection.Y = y;

                    InsertIntersection(ref intersection);

                    --j;
                }

                m_sortedEdgeList.Insert(j + 1, entry);
            }

            m_sortedEdgeList.Clear();
        }
 private SortedEdgeEntry GetSortedEdgeEntry(float bottom, float top, float dy, int i, ref SortedEdgeEntry entry)
 {
     var edge = m_activeEdgeList[i];
     entry.Index = i;
     if (edge.TopY == top)
     {
         MyPolygon.Vertex vert;
         if (edge.Kind == PolygonType.SUBJECT)
         {
             m_polyA.GetVertex(edge.TopVertexIndex, out vert);
         }
         else
         {
             m_polyB.GetVertex(edge.TopVertexIndex, out vert);
         }
         entry.XCoord = vert.Coord.X;
     }
     else
     {
         entry.XCoord = edge.CalculateX(dy);
     }
     entry.DX = entry.XCoord - edge.BottomX;
     entry.Kind = edge.Kind;
     entry.QNumerator = edge.CalculateQNumerator(bottom, top, entry.XCoord);
     Debug.Assert(edge.TopY != bottom);
     return entry;
 }
        private static int CompareSortedEdgeEntries(ref SortedEdgeEntry entry1, ref SortedEdgeEntry entry2)
        {
            // Sorting rules:
            // 1.) sort by X coordinate
            // 2.) sort by type of input polygon
            // 3.) sort by DXdy value (or in this case, only DX)
            if (entry1.XCoord < entry2.XCoord)
            {
                return -1;
            }
            else if (entry1.XCoord == entry2.XCoord)
            {
                if (entry1.Kind == entry2.Kind)
                {
                    Debug.Assert(entry1.DX >= entry2.DX, "Overtaking a sorted edge entry that should be processed after us. This shouldn't happen!");
                    Debug.Assert(entry1.DX > entry2.DX, "Overlapping edge in an input polygon!");
                    return 1;
                }
                else if (entry1.Kind == PolygonType.CLIP)
                {
                    return -1;
                }
            }

            return 1;
        }