Example #1
0
    public FloatArray computePolygon(float[] points, int offset, int count, bool sorted)
    {
        int end = offset + count;

        if (!sorted)
        {
            if (sortedPoints == null || sortedPoints.Length < count)
            {
                sortedPoints = new float[count];
            }
            Array.Copy(points, offset, sortedPoints, 0, count);
            points = sortedPoints;
            offset = 0;
            sort(points, count);
        }

        FloatArray hull = this.hull;

        hull.clear();

        // Lower hull.
        for (int i = offset; i < end; i += 2)
        {
            float x = points[i];
            float y = points[i + 1];
            while (hull.size >= 4 && ccw(x, y) <= 0)
            {
                hull.size -= 2;
            }
            hull.Add(x);
            hull.Add(y);
        }
        List <float> l1 = new List <float>();
        List <float> l2 = new List <float>();

        float[] xx = new float[hull.size];
        float[] yy = new float[hull.size];
        l1 = hull.toArray().ToList();
        l2 = hull.toArray().ToList();
        for (int i = 0; i < l1.Count; i++)
        {
            if (i % 2 == 0)
            {
                xx[i / 2] = l1.ElementAt(i);
            }
            else
            {
                yy[(i - 1) / 2] = l1.ElementAt(i);
            }
        }
        abajo = Punto.getArreglo(xx, yy);

        FloatArray hullAUX = new FloatArray();

        // Upper hull.
        for (int i = end - 4, t = hull.size + 2; i >= offset; i -= 2)
        {
            float x = points[i];
            float y = points[i + 1];
            while (hull.size >= t && ccw(x, y) <= 0)
            {
                hull.size -= 2;
            }
            hull.Add(x);
            hull.Add(y);
        }

        for (int i = end - 4, t = hullAUX.size + 2; i >= offset; i -= 2)
        {
            float x = points[i];
            float y = points[i + 1];
            while (hullAUX.size >= t && ccw(x, y) <= 0)
            {
                hullAUX.size -= 2;
            }
            hullAUX.Add(x);
            hullAUX.Add(y);
        }

        l1 = hullAUX.toArray().ToList();
        l2 = hullAUX.toArray().ToList();
        for (int i = 0; i < l1.Count; i++)
        {
            if (i % 2 == 0)
            {
                xx[i / 2] = l1.ElementAt(i);
            }
            else
            {
                yy[(i - 1) / 2] = l1.ElementAt(i);
            }
        }
        arriba = Punto.getArreglo(xx, yy);

        Array.Sort(arriba, ConvexHull.compararPorAbsisa());
        return(hull);
    }
Example #2
0
    /** Computes a hull the same as {@link #computePolygon(float[], int, int, bool)} but returns indices of the specified points. */
    public IntArray computeIndices(float[] points, int offset, int count, bool sorted, bool yDown)
    {
        int end = offset + count;

        if (!sorted)
        {
            if (sortedPoints == null || sortedPoints.Length < count)
            {
                sortedPoints = new float[count];
            }
            Array.Copy(points, offset, sortedPoints, 0, count);
            points = sortedPoints;
            offset = 0;
            sortWithIndices(points, count, yDown);
        }

        IntArray indices = this.indices;

        indices.clear();

        FloatArray hull = this.hull;

        hull.clear();

        // Lower hull.
        for (int i = offset, index = i / 2; i < end; i += 2, index++)
        {
            float x = points[i];
            float y = points[i + 1];
            while (hull.size >= 4 && ccw(x, y) <= 0)
            {
                hull.size -= 2;
                indices.size--;
            }
            hull.Add(x);
            hull.Add(y);
            indices.Add(index);
        }

        // Upper hull.
        for (int i = end - 4, index = i / 2, t = hull.size + 2; i >= offset; i -= 2, index--)
        {
            float x = points[i];
            float y = points[i + 1];
            while (hull.size >= t && ccw(x, y) <= 0)
            {
                hull.size -= 2;
                indices.size--;
            }
            hull.Add(x);
            hull.Add(y);
            indices.Add(index);
        }

        // Convert sorted to unsorted indices.
        if (!sorted)
        {
            short[] originalIndicesArray = originalIndices.items;
            int[]   indicesArray         = indices.items;
            for (int i = 0, n = indices.size; i < n; i++)
            {
                indicesArray[i] = originalIndicesArray[indicesArray[i]];
            }
        }

        return(indices);
    }