Java interpolate interpolatedNoise(int x, int z, int reciprocal)

Here you can find the source of interpolatedNoise(int x, int z, int reciprocal)

Description

Gets interpolated noise for the specified coordinate pair, using the specified frequency reciprocal.

License

Open Source License

Parameter

Parameter Description
x The x coordinate.
z The z coordinate.
reciprocal The frequency reciprocal.

Return

The interpolated noise.

Declaration

private static int interpolatedNoise(int x, int z, int reciprocal) 

Method Source Code

//package com.java2s;

public class Main {
    /**/*from  ww w. j  a  va  2s  . c om*/
     * The cosine table used for interpolation.
     */
    private static final int[] COSINE = new int[2048];

    /**
     * Gets interpolated noise for the specified coordinate pair, using the specified frequency reciprocal.
     *
     * @param x The x coordinate.
     * @param z The z coordinate.
     * @param reciprocal The frequency reciprocal.
     * @return The interpolated noise.
     */
    private static int interpolatedNoise(int x, int z, int reciprocal) {
        int xt = x % reciprocal;
        int zt = z % reciprocal;

        x /= reciprocal;
        z /= reciprocal;

        int c = smoothNoise(x, z);
        int e = smoothNoise(x + 1, z);
        int ce = interpolate(c, e, xt, reciprocal);

        int n = smoothNoise(x, z + 1);
        int ne = smoothNoise(x + 1, z + 1);
        int u = interpolate(n, ne, xt, reciprocal);

        return interpolate(ce, u, zt, reciprocal);
    }

    /**
     * Computes smooth noise for the specified coordinate pair.
     *
     * @param x The x coordinate.
     * @param z The z coordinate.
     * @return The smooth noise.
     */
    private static int smoothNoise(int x, int z) {
        int corners = noise(x - 1, z - 1) + noise(x + 1, z - 1) + noise(x - 1, z + 1) + noise(x + 1, z + 1);
        int sides = noise(x - 1, z) + noise(x + 1, z) + noise(x, z - 1) + noise(x, z + 1);
        int center = noise(x, z);

        return corners / 16 + sides / 8 + center / 4;
    }

    /**
     * Interpolates two smooth noise values.
     *
     * @param a The first smooth noise value.
     * @param b The second smooth noise value.
     * @param theta The angle.
     * @param reciprocal The frequency reciprocal.
     * @return The interpolated value.
     */
    private static int interpolate(int a, int b, int theta, int reciprocal) {
        int cosine = 65536 - COSINE[theta * COSINE.length / (2 * reciprocal)] / 2;
        return (a * (65536 - cosine)) / 65536 + (b * cosine) / 65536;
    }

    /**
     * Computes noise for the specified coordinate pair.
     *
     * @param x The x coordinate.
     * @param z The z coordinate.
     * @return The noise.
     */
    private static int noise(int x, int z) {
        int n = x + z * 57;
        n = (n << 13) ^ n;
        n = (n * (n * n * 15731 + 789221) + 1376312589) & Integer.MAX_VALUE;
        return (n >> 19) & 0xff;
    }
}

Related

  1. interpolateColor(int c1, int c2, int st, int sts)
  2. interpolateColor(int rgba1, int rgba2, float percent)
  3. interpolateColors(int a, int b, float lerp)
  4. interpolateColors(int c0, int c1, float w)
  5. interpolateCubic(int x0, int x1, int x2, int x3, double t)
  6. interpolatedSample(double xStart, double xVal, double xEnd, double yStart, double yEnd)
  7. interpolatefinal(double a, final double b, final double d)
  8. interpolateFloat(float f0, float f1, double mixer)
  9. interpolateFloat(float t, float a, float b)