private int EventCompare(bool p1IsStart, Point p11, Point p12, bool p2IsStart, Point p21, Point p22)
        {
            // compare the selected points first
            int comp = PointUtils.PointsCompare(p11, p21);

            if (comp != 0)
            {
                return(comp);
            }
            // the selected points are the same

            if (PointUtils.PointsSame(p12, p22)) // if the non-selected points are the same too...
            {
                return(0);                       // then the segments are equal
            }

            if (p1IsStart != p2IsStart)     // if one is a start and the other isn"t...
            {
                return(p1IsStart ? 1 : -1); // favor the one that isn"t the start
            }

            // otherwise, we"ll have to calculate which one is below the other manually
            return(PointUtils.PointAboveOrOnLine(p12,
                                                 p2IsStart ? p21 : p22, // order matters
                                                 p2IsStart ? p22 : p21
                                                 )
                ? 1
                : -1);
        }
            public void AddRegion(Region region)
            {
                // regions are a list of points:
                //  [ [0, 0], [100, 0], [50, 100] ]
                // you can add multiple regions before running calculate
                Point pt1;
                Point pt2 = region.Points[region.Points.Count - 1];

                for (int i = 0; i < region.Points.Count; i++)
                {
                    pt1 = pt2;
                    pt2 = region.Points[i];

                    int forward = PointUtils.PointsCompare(pt1, pt2);
                    if (forward == 0) // points are equal, so we have a zero-length segment
                    {
                        continue;     // just skip it
                    }

                    EventAddSegment(
                        SegmentNew(
                            forward < 0 ? pt1 : pt2,
                            forward < 0 ? pt2 : pt1
                            ),
                        true
                        );
                }
            }