public static com.esri.core.geometry.QuadTree buildQuadTree(com.esri.core.geometry.MultiPath
                                                                    multipath)
        {
            com.esri.core.geometry.Envelope2D extent = new com.esri.core.geometry.Envelope2D(
                );
            multipath.queryEnvelope2D(extent);
            com.esri.core.geometry.QuadTree quadTree = new com.esri.core.geometry.QuadTree(extent
                                                                                           , 8);
            int hint_index = -1;

            com.esri.core.geometry.SegmentIterator seg_iter = multipath.querySegmentIterator(
                );
            while (seg_iter.nextPath())
            {
                while (seg_iter.hasNextSegment())
                {
                    com.esri.core.geometry.Segment segment = seg_iter.nextSegment();
                    int index = seg_iter.getStartPointIndex();
                    com.esri.core.geometry.Envelope2D boundingbox = new com.esri.core.geometry.Envelope2D
                                                                        ();
                    segment.queryEnvelope2D(boundingbox);
                    hint_index = quadTree.insert(index, boundingbox, hint_index);
                }
            }
            return(quadTree);
        }
        public static void testIntervalTreeRandomConstruction()
        {
            int pointcount = 0;
            int passcount  = 1000;
            int figureSize = 50;

            com.esri.core.geometry.Envelope env = new com.esri.core.geometry.Envelope();
            env.setCoords(-10000, -10000, 10000, 10000);
            com.esri.core.geometry.RandomCoordinateGenerator generator = new com.esri.core.geometry.RandomCoordinateGenerator
                                                                             (System.Math.max(figureSize, 10000), env, 0.001);
            java.util.Random random = new java.util.Random(2013);
            int rand_max            = 98765;

            System.Collections.Generic.List <com.esri.core.geometry.Envelope1D> intervals = new
                                                                                            System.Collections.Generic.List <com.esri.core.geometry.Envelope1D>();
            com.esri.core.geometry.AttributeStreamOfInt8 intervalsFound = new com.esri.core.geometry.AttributeStreamOfInt8
                                                                              (0);
            for (int i = 0; i < passcount; i++)
            {
                int r = figureSize;
                if (r < 3)
                {
                    continue;
                }
                com.esri.core.geometry.Polygon poly = new com.esri.core.geometry.Polygon();
                com.esri.core.geometry.Point   pt;
                for (int j = 0; j < r; j++)
                {
                    int  rand       = random.nextInt(rand_max);
                    bool bRandomNew = (r > 10) && ((1.0 * rand) / rand_max > 0.95);
                    pt = generator.GetRandomCoord();
                    if (j == 0 || bRandomNew)
                    {
                        poly.startPath(pt);
                    }
                    else
                    {
                        poly.lineTo(pt);
                    }
                }
                {
                    intervals.clear();
                    com.esri.core.geometry.SegmentIterator seg_iter = poly.querySegmentIterator();
                    com.esri.core.geometry.Envelope1D      interval;
                    com.esri.core.geometry.Envelope1D      range = poly.queryInterval(com.esri.core.geometry.VertexDescription.Semantics
                                                                                      .POSITION, 0);
                    range.vmin -= 0.01;
                    range.vmax += 0.01;
                    while (seg_iter.nextPath())
                    {
                        while (seg_iter.hasNextSegment())
                        {
                            com.esri.core.geometry.Segment segment = seg_iter.nextSegment();
                            interval = segment.queryInterval(com.esri.core.geometry.VertexDescription.Semantics
                                                             .POSITION, 0);
                            intervals.add(interval);
                        }
                    }
                    intervalsFound.resize(intervals.Count, 0);
                    // Just test construction for assertions
                    com.esri.core.geometry.IntervalTreeImpl intervalTree = new com.esri.core.geometry.IntervalTreeImpl
                                                                               (true);
                    construct(intervalTree, intervals);
                    for (int j_1 = 0; j_1 < intervals.Count; j_1++)
                    {
                        intervalTree.insert(j_1);
                    }
                    com.esri.core.geometry.IntervalTreeImpl.IntervalTreeIteratorImpl iterator = intervalTree
                                                                                                .getIterator(range, 0.0);
                    int count = 0;
                    int handle;
                    while ((handle = iterator.next()) != -1)
                    {
                        count++;
                        intervalsFound.write(handle, unchecked ((byte)1));
                    }
                    NUnit.Framework.Assert.IsTrue(count == intervals.Count);
                    for (int j_2 = 0; j_2 < intervalsFound.size(); j_2++)
                    {
                        interval = intervals[j_2];
                        NUnit.Framework.Assert.IsTrue(intervalsFound.read(j_2) == 1);
                    }
                    for (int j_3 = 0; j_3 < intervals.Count >> 1; j_3++)
                    {
                        intervalTree.remove(j_3);
                    }
                    iterator.resetIterator(range, 0.0);
                    count = 0;
                    while ((handle = iterator.next()) != -1)
                    {
                        count++;
                        intervalsFound.write(handle, unchecked ((byte)1));
                    }
                    NUnit.Framework.Assert.IsTrue(count == intervals.Count - (intervals.Count >> 1));
                    for (int j_4 = (intervals.Count >> 1); j_4 < intervals.Count; j_4++)
                    {
                        intervalTree.remove(j_4);
                    }
                }
            }
        }
        public static void testRandom()
        {
            int passcount   = 10;
            int figureSize  = 100;
            int figureSize2 = 100;

            com.esri.core.geometry.Envelope extent1 = new com.esri.core.geometry.Envelope();
            com.esri.core.geometry.Envelope extent2 = new com.esri.core.geometry.Envelope();
            com.esri.core.geometry.Envelope extent3 = new com.esri.core.geometry.Envelope();
            com.esri.core.geometry.Envelope extent4 = new com.esri.core.geometry.Envelope();
            extent1.setCoords(-10000, 5000, 10000, 25000);
            // red
            extent2.setCoords(-10000, 2000, 10000, 8000);
            // blue
            extent3.setCoords(-10000, -8000, 10000, -2000);
            // blue
            extent4.setCoords(-10000, -25000, 10000, -5000);
            // red
            com.esri.core.geometry.RandomCoordinateGenerator generator1 = new com.esri.core.geometry.RandomCoordinateGenerator
                                                                              (System.Math.max(figureSize, 10000), extent1, 0.001);
            com.esri.core.geometry.RandomCoordinateGenerator generator2 = new com.esri.core.geometry.RandomCoordinateGenerator
                                                                              (System.Math.max(figureSize, 10000), extent2, 0.001);
            com.esri.core.geometry.RandomCoordinateGenerator generator3 = new com.esri.core.geometry.RandomCoordinateGenerator
                                                                              (System.Math.max(figureSize, 10000), extent3, 0.001);
            com.esri.core.geometry.RandomCoordinateGenerator generator4 = new com.esri.core.geometry.RandomCoordinateGenerator
                                                                              (System.Math.max(figureSize, 10000), extent4, 0.001);
            java.util.Random random = new java.util.Random(1982);
            int rand_max            = 511;
            int qCount = 0;
            int eCount = 0;
            int bCount = 0;

            for (int c = 0; c < passcount; c++)
            {
                com.esri.core.geometry.Polygon polyRed  = new com.esri.core.geometry.Polygon();
                com.esri.core.geometry.Polygon polyBlue = new com.esri.core.geometry.Polygon();
                int r = figureSize;
                if (r < 3)
                {
                    continue;
                }
                com.esri.core.geometry.Point pt;
                for (int j = 0; j < r; j++)
                {
                    int  rand       = random.nextInt(rand_max);
                    bool bRandomNew = (r > 10) && ((1.0 * rand) / rand_max > 0.95);
                    pt = generator1.GetRandomCoord();
                    if (j == 0 || bRandomNew)
                    {
                        polyRed.startPath(pt);
                    }
                    else
                    {
                        polyRed.lineTo(pt);
                    }
                }
                for (int j_1 = 0; j_1 < r; j_1++)
                {
                    int  rand       = random.nextInt(rand_max);
                    bool bRandomNew = (r > 10) && ((1.0 * rand) / rand_max > 0.95);
                    pt = generator4.GetRandomCoord();
                    if (j_1 == 0 || bRandomNew)
                    {
                        polyRed.startPath(pt);
                    }
                    else
                    {
                        polyRed.lineTo(pt);
                    }
                }
                r = figureSize2;
                if (r < 3)
                {
                    continue;
                }
                for (int j_2 = 0; j_2 < r; j_2++)
                {
                    int  rand       = random.nextInt(rand_max);
                    bool bRandomNew = (r > 10) && ((1.0 * rand) / rand_max > 0.95);
                    pt = generator2.GetRandomCoord();
                    if (j_2 == 0 || bRandomNew)
                    {
                        polyBlue.startPath(pt);
                    }
                    else
                    {
                        polyBlue.lineTo(pt);
                    }
                }
                for (int j_3 = 0; j_3 < r; j_3++)
                {
                    int  rand       = random.nextInt(rand_max);
                    bool bRandomNew = (r > 10) && ((1.0 * rand) / rand_max > 0.95);
                    pt = generator3.GetRandomCoord();
                    if (j_3 == 0 || bRandomNew)
                    {
                        polyBlue.startPath(pt);
                    }
                    else
                    {
                        polyBlue.lineTo(pt);
                    }
                }
                com.esri.core.geometry.Envelope2D env = new com.esri.core.geometry.Envelope2D();
                // Quad_tree
                com.esri.core.geometry.QuadTree quadTree = buildQuadTree(polyBlue);
                com.esri.core.geometry.QuadTree.QuadTreeIterator iterator = quadTree.getIterator(
                    );
                com.esri.core.geometry.SegmentIteratorImpl _segIterRed = ((com.esri.core.geometry.MultiPathImpl
                                                                           )polyRed._getImpl()).querySegmentIterator();
                while (_segIterRed.nextPath())
                {
                    while (_segIterRed.hasNextSegment())
                    {
                        com.esri.core.geometry.Segment segmentRed = _segIterRed.nextSegment();
                        segmentRed.queryEnvelope2D(env);
                        iterator.resetIterator(env, 0.001);
                        while (iterator.next() != -1)
                        {
                            qCount++;
                        }
                    }
                }
                // Envelope_2D_intersector
                System.Collections.Generic.List <com.esri.core.geometry.Envelope2D> envelopes_red =
                    new System.Collections.Generic.List <com.esri.core.geometry.Envelope2D>();
                System.Collections.Generic.List <com.esri.core.geometry.Envelope2D> envelopes_blue
                    = new System.Collections.Generic.List <com.esri.core.geometry.Envelope2D>();
                com.esri.core.geometry.SegmentIterator segIterRed = polyRed.querySegmentIterator(
                    );
                while (segIterRed.nextPath())
                {
                    while (segIterRed.hasNextSegment())
                    {
                        com.esri.core.geometry.Segment segment = segIterRed.nextSegment();
                        env = new com.esri.core.geometry.Envelope2D();
                        segment.queryEnvelope2D(env);
                        envelopes_red.add(env);
                    }
                }
                com.esri.core.geometry.SegmentIterator segIterBlue = polyBlue.querySegmentIterator
                                                                         ();
                while (segIterBlue.nextPath())
                {
                    while (segIterBlue.hasNextSegment())
                    {
                        com.esri.core.geometry.Segment segment = segIterBlue.nextSegment();
                        env = new com.esri.core.geometry.Envelope2D();
                        segment.queryEnvelope2D(env);
                        envelopes_blue.add(env);
                    }
                }
                com.esri.core.geometry.Envelope2DIntersectorImpl intersector = new com.esri.core.geometry.Envelope2DIntersectorImpl
                                                                                   ();
                intersector.setTolerance(0.001);
                intersector.startRedConstruction();
                for (int i = 0; i < envelopes_red.Count; i++)
                {
                    intersector.addRedEnvelope(i, envelopes_red[i]);
                }
                intersector.endRedConstruction();
                intersector.startBlueConstruction();
                for (int i_1 = 0; i_1 < envelopes_blue.Count; i_1++)
                {
                    intersector.addBlueEnvelope(i_1, envelopes_blue[i_1]);
                }
                intersector.endBlueConstruction();
                while (intersector.next())
                {
                    eCount++;
                }
                NUnit.Framework.Assert.IsTrue(qCount == eCount);
            }
        }
        internal virtual bool rgHelper(com.esri.core.geometry.RasterizedGeometry2D rg, com.esri.core.geometry.MultiPath
                                       mp)
        {
            com.esri.core.geometry.SegmentIterator iter = mp.querySegmentIterator();
            while (iter.nextPath())
            {
                while (iter.hasNextSegment())
                {
                    com.esri.core.geometry.Segment seg = iter.nextSegment();
                    int count = 20;
                    for (int i = 0; i < count; i++)
                    {
                        double t = (1.0 * i / count);
                        com.esri.core.geometry.Point2D pt = seg.getCoord2D(t);
                        com.esri.core.geometry.RasterizedGeometry2D.HitType hit = rg.queryPointInGeometry
                                                                                      (pt.x, pt.y);
                        if (hit != com.esri.core.geometry.RasterizedGeometry2D.HitType.Border)
                        {
                            return(false);
                        }
                    }
                }
            }
            if (mp.getType() != com.esri.core.geometry.Geometry.Type.Polygon)
            {
                return(true);
            }
            com.esri.core.geometry.Polygon    poly = (com.esri.core.geometry.Polygon)mp;
            com.esri.core.geometry.Envelope2D env  = new com.esri.core.geometry.Envelope2D();
            poly.queryEnvelope2D(env);
            int count_1 = 100;

            for (int iy = 0; iy < count_1; iy++)
            {
                double ty = 1.0 * iy / count_1;
                double y  = env.ymin * (1.0 - ty) + ty * env.ymax;
                for (int ix = 0; ix < count_1; ix++)
                {
                    double tx = 1.0 * ix / count_1;
                    double x  = env.xmin * (1.0 - tx) + tx * env.xmax;
                    com.esri.core.geometry.RasterizedGeometry2D.HitType hit = rg.queryPointInGeometry
                                                                                  (x, y);
                    com.esri.core.geometry.PolygonUtils.PiPResult res = com.esri.core.geometry.PolygonUtils
                                                                        .isPointInPolygon2D(poly, new com.esri.core.geometry.Point2D(x, y), 0);
                    if (res == com.esri.core.geometry.PolygonUtils.PiPResult.PiPInside)
                    {
                        bool bgood = (hit == com.esri.core.geometry.RasterizedGeometry2D.HitType.Border ||
                                      hit == com.esri.core.geometry.RasterizedGeometry2D.HitType.Inside);
                        if (!bgood)
                        {
                            return(false);
                        }
                    }
                    else
                    {
                        if (res == com.esri.core.geometry.PolygonUtils.PiPResult.PiPOutside)
                        {
                            bool bgood = (hit == com.esri.core.geometry.RasterizedGeometry2D.HitType.Border ||
                                          hit == com.esri.core.geometry.RasterizedGeometry2D.HitType.Outside);
                            if (!bgood)
                            {
                                return(false);
                            }
                        }
                        else
                        {
                            bool bgood = (hit == com.esri.core.geometry.RasterizedGeometry2D.HitType.Border);
                            if (!bgood)
                            {
                                return(false);
                            }
                        }
                    }
                }
            }
            return(true);
        }