Bukkit-API  1.7.9-R0.2
The inofficial Bukkit-API
NoiseGenerator.java
1 package org.bukkit.util.noise;
2 
3 /**
4  * Base class for all noise generators
5  */
6 public abstract class NoiseGenerator {
7  protected final int perm[] = new int[512];
8  protected double offsetX;
9  protected double offsetY;
10  protected double offsetZ;
11 
12  /**
13  * Speedy floor, faster than (int)Math.floor(x)
14  *
15  * @param x Value to floor
16  * @return Floored value
17  */
18  public static int floor(double x) {
19  return x >= 0 ? (int) x : (int) x - 1;
20  }
21 
22  protected static double fade(double x) {
23  return x * x * x * (x * (x * 6 - 15) + 10);
24  }
25 
26  protected static double lerp(double x, double y, double z) {
27  return y + x * (z - y);
28  }
29 
30  protected static double grad(int hash, double x, double y, double z) {
31  hash &= 15;
32  double u = hash < 8 ? x : y;
33  double v = hash < 4 ? y : hash == 12 || hash == 14 ? x : z;
34  return ((hash & 1) == 0 ? u : -u) + ((hash & 2) == 0 ? v : -v);
35  }
36 
37  /**
38  * Computes and returns the 1D noise for the given coordinate in 1D space
39  *
40  * @param x X coordinate
41  * @return Noise at given location, from range -1 to 1
42  */
43  public double noise(double x) {
44  return noise(x, 0, 0);
45  }
46 
47  /**
48  * Computes and returns the 2D noise for the given coordinates in 2D space
49  *
50  * @param x X coordinate
51  * @param y Y coordinate
52  * @return Noise at given location, from range -1 to 1
53  */
54  public double noise(double x, double y) {
55  return noise(x, y, 0);
56  }
57 
58  /**
59  * Computes and returns the 3D noise for the given coordinates in 3D space
60  *
61  * @param x X coordinate
62  * @param y Y coordinate
63  * @param z Z coordinate
64  * @return Noise at given location, from range -1 to 1
65  */
66  public abstract double noise(double x, double y, double z);
67 
68  /**
69  * Generates noise for the 1D coordinates using the specified number of
70  * octaves and parameters
71  *
72  * @param x X-coordinate
73  * @param octaves Number of octaves to use
74  * @param frequency How much to alter the frequency by each octave
75  * @param amplitude How much to alter the amplitude by each octave
76  * @return Resulting noise
77  */
78  public double noise(double x, int octaves, double frequency, double amplitude) {
79  return noise(x, 0, 0, octaves, frequency, amplitude);
80  }
81 
82  /**
83  * Generates noise for the 1D coordinates using the specified number of
84  * octaves and parameters
85  *
86  * @param x X-coordinate
87  * @param octaves Number of octaves to use
88  * @param frequency How much to alter the frequency by each octave
89  * @param amplitude How much to alter the amplitude by each octave
90  * @param normalized If true, normalize the value to [-1, 1]
91  * @return Resulting noise
92  */
93  public double noise(double x, int octaves, double frequency, double amplitude, boolean normalized) {
94  return noise(x, 0, 0, octaves, frequency, amplitude, normalized);
95  }
96 
97  /**
98  * Generates noise for the 2D coordinates using the specified number of
99  * octaves and parameters
100  *
101  * @param x X-coordinate
102  * @param y Y-coordinate
103  * @param octaves Number of octaves to use
104  * @param frequency How much to alter the frequency by each octave
105  * @param amplitude How much to alter the amplitude by each octave
106  * @return Resulting noise
107  */
108  public double noise(double x, double y, int octaves, double frequency, double amplitude) {
109  return noise(x, y, 0, octaves, frequency, amplitude);
110  }
111 
112  /**
113  * Generates noise for the 2D coordinates using the specified number of
114  * octaves and parameters
115  *
116  * @param x X-coordinate
117  * @param y Y-coordinate
118  * @param octaves Number of octaves to use
119  * @param frequency How much to alter the frequency by each octave
120  * @param amplitude How much to alter the amplitude by each octave
121  * @param normalized If true, normalize the value to [-1, 1]
122  * @return Resulting noise
123  */
124  public double noise(double x, double y, int octaves, double frequency, double amplitude, boolean normalized) {
125  return noise(x, y, 0, octaves, frequency, amplitude, normalized);
126  }
127 
128  /**
129  * Generates noise for the 3D coordinates using the specified number of
130  * octaves and parameters
131  *
132  * @param x X-coordinate
133  * @param y Y-coordinate
134  * @param z Z-coordinate
135  * @param octaves Number of octaves to use
136  * @param frequency How much to alter the frequency by each octave
137  * @param amplitude How much to alter the amplitude by each octave
138  * @return Resulting noise
139  */
140  public double noise(double x, double y, double z, int octaves, double frequency, double amplitude) {
141  return noise(x, y, z, octaves, frequency, amplitude, false);
142  }
143 
144  /**
145  * Generates noise for the 3D coordinates using the specified number of
146  * octaves and parameters
147  *
148  * @param x X-coordinate
149  * @param y Y-coordinate
150  * @param z Z-coordinate
151  * @param octaves Number of octaves to use
152  * @param frequency How much to alter the frequency by each octave
153  * @param amplitude How much to alter the amplitude by each octave
154  * @param normalized If true, normalize the value to [-1, 1]
155  * @return Resulting noise
156  */
157  public double noise(double x, double y, double z, int octaves, double frequency, double amplitude, boolean normalized) {
158  double result = 0;
159  double amp = 1;
160  double freq = 1;
161  double max = 0;
162 
163  for (int i = 0; i < octaves; i++) {
164  result += noise(x * freq, y * freq, z * freq) * amp;
165  max += amp;
166  freq *= frequency;
167  amp *= amplitude;
168  }
169 
170  if (normalized) {
171  result /= max;
172  }
173 
174  return result;
175  }
176 }
double noise(double x, double y)
double noise(double x, double y, double z, int octaves, double frequency, double amplitude)
double noise(double x, double y, double z, int octaves, double frequency, double amplitude, boolean normalized)
double noise(double x, int octaves, double frequency, double amplitude, boolean normalized)
double noise(double x, double y, int octaves, double frequency, double amplitude, boolean normalized)
double noise(double x, int octaves, double frequency, double amplitude)
double noise(double x, double y, int octaves, double frequency, double amplitude)