|
7 | 7 | * Utility for generating 2D value-noise blended across octaves (commonly known |
8 | 8 | * as Perlin-like noise). |
9 | 9 | * |
10 | | - * <p>The implementation follows the classic approach of: |
| 10 | + * <p> |
| 11 | + * The implementation follows the classic approach of: |
11 | 12 | * <ol> |
12 | | - * <li>Generate a base grid of random values in [0, 1).</li> |
13 | | - * <li>For each octave k, compute a layer by bilinear interpolation of the base grid |
14 | | - * at period 2^k.</li> |
15 | | - * <li>Blend all layers from coarse to fine using a geometric series of amplitudes |
16 | | - * controlled by {@code persistence}, then normalize to [0, 1].</li> |
| 13 | + * <li>Generate a base grid of random values in [0, 1).</li> |
| 14 | + * <li>For each octave k, compute a layer by bilinear interpolation of the base |
| 15 | + * grid |
| 16 | + * at period 2^k.</li> |
| 17 | + * <li>Blend all layers from coarse to fine using a geometric series of |
| 18 | + * amplitudes |
| 19 | + * controlled by {@code persistence}, then normalize to [0, 1].</li> |
17 | 20 | * </ol> |
18 | 21 | * |
19 | | - * <p>For background see: <a href="http://devmag.org.za/2009/04/25/perlin-noise/">Perlin Noise</a>. |
| 22 | + * <p> |
| 23 | + * For background see: |
| 24 | + * <a href="http://devmag.org.za/2009/04/25/perlin-noise/">Perlin Noise</a>. |
20 | 25 | * |
21 | | - * <p>Constraints and notes: |
| 26 | + * <p> |
| 27 | + * Constraints and notes: |
22 | 28 | * <ul> |
23 | | - * <li>{@code width} and {@code height} should be positive.</li> |
24 | | - * <li>{@code octaveCount} must be at least 1 (0 would lead to a division by zero).</li> |
25 | | - * <li>{@code persistence} should be in (0, 1], typical values around 0.5–0.8.</li> |
26 | | - * <li>Given the same seed and parameters, results are deterministic.</li> |
| 29 | + * <li>{@code width} and {@code height} should be positive.</li> |
| 30 | + * <li>{@code octaveCount} must be at least 1 (0 would lead to a division by |
| 31 | + * zero).</li> |
| 32 | + * <li>{@code persistence} should be in (0, 1], typical values around |
| 33 | + * 0.5–0.8.</li> |
| 34 | + * <li>Given the same seed and parameters, results are deterministic.</li> |
27 | 35 | * </ul> |
28 | 36 | */ |
| 37 | + |
29 | 38 | public final class PerlinNoise { |
30 | | - private PerlinNoise() {} |
| 39 | + private PerlinNoise() { |
| 40 | + } |
31 | 41 |
|
32 | 42 | /** |
33 | 43 | * Generate a 2D array of blended noise values normalized to [0, 1]. |
34 | 44 | * |
35 | | - * @param width width of the noise array (columns) |
36 | | - * @param height height of the noise array (rows) |
| 45 | + * @param width width of the noise array (columns) |
| 46 | + * @param height height of the noise array (rows) |
37 | 47 | * @param octaveCount number of octaves (layers) to blend; must be >= 1 |
38 | 48 | * @param persistence per-octave amplitude multiplier in (0, 1] |
39 | | - * @param seed seed for the random base grid |
40 | | - * @return a {@code width x height} array containing blended noise values in [0, 1] |
| 49 | + * @param seed seed for the random base grid |
| 50 | + * @return a {@code width x height} array containing blended noise values in [0, |
| 51 | + * 1] |
41 | 52 | */ |
42 | 53 | static float[][] generatePerlinNoise(int width, int height, int octaveCount, float persistence, long seed) { |
43 | 54 | if (width <= 0 || height <= 0) { |
44 | 55 | throw new IllegalArgumentException("width and height must be > 0"); |
45 | 56 | } |
| 57 | + |
46 | 58 | if (octaveCount < 1) { |
47 | 59 | throw new IllegalArgumentException("octaveCount must be >= 1"); |
48 | 60 | } |
@@ -110,8 +122,8 @@ static float[][] blendAndNormalize(float[][][] layers, int width, int height, fl |
110 | 122 | * Generate a single octave layer by bilinear interpolation of a base grid at a |
111 | 123 | * given octave (period = 2^octave). |
112 | 124 | * |
113 | | - * @param base base random float array of size {@code width x height} |
114 | | - * @param width width of noise array |
| 125 | + * @param base base random float array of size {@code width x height} |
| 126 | + * @param width width of noise array |
115 | 127 | * @param height height of noise array |
116 | 128 | * @param octave current octave (0 for period 1, 1 for period 2, ...) |
117 | 129 | * @return float array containing the octave's interpolated values |
@@ -152,8 +164,8 @@ static float[][] generatePerlinNoiseLayer(float[][] base, int width, int height, |
152 | 164 | /** |
153 | 165 | * Linear interpolation between two values. |
154 | 166 | * |
155 | | - * @param a value at alpha = 0 |
156 | | - * @param b value at alpha = 1 |
| 167 | + * @param a value at alpha = 0 |
| 168 | + * @param b value at alpha = 1 |
157 | 169 | * @param alpha interpolation factor in [0, 1] |
158 | 170 | * @return interpolated value {@code (1 - alpha) * a + alpha * b} |
159 | 171 | */ |
|
0 commit comments