Java Random Int randomPointInConvexPoly(float[] pts, int npts, float[] areas, float s, float t)

Here you can find the source of randomPointInConvexPoly(float[] pts, int npts, float[] areas, float s, float t)

Description

random Point In Convex Poly

License

Open Source License

Declaration

static float[] randomPointInConvexPoly(float[] pts, int npts,
            float[] areas, float s, float t) 

Method Source Code

//package com.java2s;

public class Main {
    static float[] randomPointInConvexPoly(float[] pts, int npts,
            float[] areas, float s, float t) {
        // Calc triangle araes
        float areasum = 0.0f;
        for (int i = 2; i < npts; i++) {
            areas[i] = triArea2D(pts, 0, (i - 1) * 3, i * 3);
            areasum += Math.max(0.001f, areas[i]);
        }//from  w ww .ja  v  a2 s .c  om
        // Find sub triangle weighted by area.
        float thr = s * areasum;
        float acc = 0.0f;
        float u = 0.0f;
        int tri = 0;
        for (int i = 2; i < npts; i++) {
            float dacc = areas[i];
            if (thr >= acc && thr < (acc + dacc)) {
                u = (thr - acc) / dacc;
                tri = i;
                break;
            }
            acc += dacc;
        }

        float v = (float) Math.sqrt(t);

        float a = 1 - v;
        float b = (1 - u) * v;
        float c = u * v;
        int pa = 0;
        int pb = (tri - 1) * 3;
        int pc = tri * 3;

        return new float[] { a * pts[pa] + b * pts[pb] + c * pts[pc],
                a * pts[pa + 1] + b * pts[pb + 1] + c * pts[pc + 1],
                a * pts[pa + 2] + b * pts[pb + 2] + c * pts[pc + 2] };
    }

    public static float triArea2D(float[] verts, int a, int b, int c) {
        float abx = verts[b] - verts[a];
        float abz = verts[b + 2] - verts[a + 2];
        float acx = verts[c] - verts[a];
        float acz = verts[c + 2] - verts[a + 2];
        return acx * abz - abx * acz;
    }

    public static float triArea2D(float[] a, float[] b, float[] c) {
        float abx = b[0] - a[0];
        float abz = b[2] - a[2];
        float acx = c[0] - a[0];
        float acz = c[2] - a[2];
        return acx * abz - abx * acz;
    }
}

Related

  1. randomNumber(int minimum, int maximum)
  2. randomNumberBetweenIntervals(int min, int max)
  3. randomNumberWithinRange(int low, int high)
  4. randomNumberWithinRange(int min, int max)
  5. randomPass(int n)
  6. randomPointsFromFigureEight(int n)
  7. randomPointsFromUnitSphere(int n)
  8. randomPointsFromUnitSquare(int n)
  9. randomQuickSort(int[] array)