Skip to content

Commit f65fc4d

Browse files
authored
Merge pull request TheAlgorithms#511 from RalleYTN/Development
Added SimplexNoise
2 parents d814728 + 0128a01 commit f65fc4d

File tree

4 files changed

+574
-0
lines changed

4 files changed

+574
-0
lines changed
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
package src.main.java.com.generation;
2+
3+
import java.util.Random;
4+
5+
/**
6+
* Implementation of the simplex noise algorithm.
7+
*/
8+
public class SimplexNoise {
9+
10+
private SimplexNoiseOctave[] octaves;
11+
private double[] frequencys;
12+
private double[] amplitudes;
13+
private int largestFeature;
14+
private double persistance;
15+
private long seed;
16+
17+
/**
18+
* @param largestFeature the diameter of the largest possible "cloud".
19+
* @param persistence the persistence. a low persistence causes smoother transition between the features while a high one makes the transition hard. (range = {@code 0.0F} - {@code 1.0F})
20+
* @param seed the seed this algorithm will use to generate pseudo random numbers. The generation will always look the same if the seed and the other parameters have the same value as in a previous generation
21+
*/
22+
public SimplexNoise(int largestFeature, double persistence, long seed) {
23+
24+
this.largestFeature = largestFeature;
25+
this.persistance = persistence;
26+
this.seed = seed;
27+
28+
int octaveCount = (int)Math.ceil(Math.log10(largestFeature) / Math.log10(2.0D));
29+
this.octaves = new SimplexNoiseOctave[octaveCount];
30+
this.frequencys = new double[octaveCount];
31+
this.amplitudes = new double[octaveCount];
32+
33+
Random random = new Random(seed);
34+
35+
for(int index = 0; index < octaveCount; index++) {
36+
37+
this.octaves[index] = new SimplexNoiseOctave(random.nextInt());
38+
this.frequencys[index] = Math.pow(2, index);
39+
this.amplitudes[index] = Math.pow(persistence, octaveCount - index);
40+
}
41+
}
42+
43+
/**
44+
* Generates a height map.
45+
* @param x X coordinate
46+
* @param y Y coordinate
47+
* @param width width
48+
* @param height height
49+
* @return the generated height map
50+
*/
51+
public float[][] generateHeightMap(int x, int y, int width, int height) {
52+
53+
int xEnd = x + width;
54+
int yEnd = y + height;
55+
56+
float[][] result = new float[width][height];
57+
58+
for(int i = 0; i < width; i++) {
59+
60+
for(int j = 0; j < height; j++) {
61+
62+
int posX = x + i * ((xEnd - x) / width);
63+
int posY = y + j * ((yEnd - y) / height);
64+
result[i][j] = Math.min(1.0F, Math.max(0.0F, (float)(0.5D * (1 + this.getNoise(posX, posY)))));
65+
}
66+
}
67+
68+
return result;
69+
}
70+
71+
/**
72+
* Generates a two dimensional noise.
73+
* @param x X coordinate
74+
* @param y Y coordinate
75+
* @return the generated noise
76+
*/
77+
public double getNoise(int x, int y) {
78+
79+
double result = 0;
80+
81+
for(int index = 0; index < this.octaves.length; index++) {
82+
83+
result += this.octaves[index].noise(x / this.frequencys[index], y / this.frequencys[index]) * this.amplitudes[index];
84+
}
85+
86+
return result;
87+
}
88+
89+
/**
90+
* Generates a three dimensional noise.
91+
* @param x X coordinate
92+
* @param y Y coordinate
93+
* @param z Z coordinate
94+
* @return the generated noise
95+
*/
96+
public double getNoise(int x, int y, int z) {
97+
98+
double result = 0;
99+
100+
for(int index = 0; index < this.octaves.length; index++) {
101+
102+
double frequency = Math.pow(2, index);
103+
double amplitude = Math.pow(this.persistance, this.octaves.length - index);
104+
105+
result += this.octaves[index].noise(x / frequency, y / frequency, z / frequency) * amplitude;
106+
}
107+
108+
return result;
109+
}
110+
111+
/**
112+
* @return the largest possible feature
113+
*/
114+
public int getLargestFeature() {
115+
116+
return this.largestFeature;
117+
}
118+
119+
/**
120+
* @return the persistence
121+
*/
122+
public double getPersistance() {
123+
124+
return this.persistance;
125+
}
126+
127+
/**
128+
* @return the seed
129+
*/
130+
public long getSeed() {
131+
132+
return this.seed;
133+
}
134+
}

0 commit comments

Comments
 (0)