|
| 1 | +import java.util.Random; |
| 2 | +import java.util.Scanner; |
1 | 3 |
|
| 4 | +public class PerlinNoise { |
| 5 | + static float[][] generatePerlinNoise(int width, int height, int octaveCount, float persistence, long seed) { |
| 6 | + final float[][] base = new float[width][height]; |
| 7 | + final float[][] perlinNoise = new float[width][height]; |
| 8 | + final float[][][] noiseLayers = new float[octaveCount][][]; |
| 9 | + |
| 10 | + Random random = new Random(seed); |
| 11 | + for(int x = 0; x < width; x++) { |
| 12 | + for(int y = 0; y < height; y++) { |
| 13 | + base[x][y] = random.nextFloat(); |
| 14 | + } |
| 15 | + } |
| 16 | + |
| 17 | + //calculate octaves |
| 18 | + for(int octave = 0; octave < octaveCount; octave++) { |
| 19 | + noiseLayers[octave] = generatePerlinNoiseLayer(base, width, height, octave); |
| 20 | + } |
| 21 | + |
| 22 | + float amplitude = 1f; |
| 23 | + float totalAmplitude = 0f; |
| 24 | + |
| 25 | + //calculate perlin noise |
| 26 | + for(int octave = octaveCount - 1; octave >= 0; octave--) { |
| 27 | + amplitude *= persistence; |
| 28 | + totalAmplitude += amplitude; |
| 29 | + |
| 30 | + for(int x = 0; x < width; x++) { |
| 31 | + for(int y = 0; y < height; y++) { |
| 32 | + perlinNoise[x][y] += noiseLayers[octave][x][y] * amplitude; |
| 33 | + } |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + //normalize |
| 38 | + for(int x = 0; x < width; x++) { |
| 39 | + for (int y = 0; y < height; y++) { |
| 40 | + perlinNoise[x][y] /= totalAmplitude; |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + return perlinNoise; |
| 45 | + } |
| 46 | + |
| 47 | + static float[][] generatePerlinNoiseLayer(float[][] base, int width, int height, int octave) { |
| 48 | + float[][] perlinNoiseLayer = new float[width][height]; |
| 49 | + |
| 50 | + int period = 1 << octave; |
| 51 | + float frequency = 1f / period; |
| 52 | + |
| 53 | + for(int x = 0; x < width; x++) { |
| 54 | + int x0 = (x / period) * period; |
| 55 | + int x1 = (x0 + period) % width; |
| 56 | + float horizintalBlend = (x - x0) * frequency; |
| 57 | + |
| 58 | + for(int y = 0; y < height; y++) { |
| 59 | + int y0 = (y / period) * period; |
| 60 | + int y1 = (y0 + period) % height; |
| 61 | + float verticalBlend = (y - y0) * frequency; |
| 62 | + |
| 63 | + float top = interpolate(base[x0][y0], base[x1][y0], horizintalBlend); |
| 64 | + float bottom = interpolate(base[x0][y1], base[x1][y1], horizintalBlend); |
| 65 | + |
| 66 | + perlinNoiseLayer[x][y] = interpolate(top, bottom, verticalBlend); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + return perlinNoiseLayer; |
| 71 | + } |
| 72 | + |
| 73 | + static float interpolate(float a, float b, float alpha) { |
| 74 | + return a * (1 - alpha) + alpha * b; |
| 75 | + } |
| 76 | + |
| 77 | + public static void main(String[] args) { |
| 78 | + Scanner in = new Scanner(System.in); |
| 79 | + |
| 80 | + final int width; |
| 81 | + final int height; |
| 82 | + final int octaveCount; |
| 83 | + final float persistence; |
| 84 | + final long seed; |
| 85 | + final String charset; |
| 86 | + final float[][] perlinNoise; |
| 87 | + |
| 88 | + System.out.println("Width (int): "); |
| 89 | + width = in.nextInt(); |
| 90 | + |
| 91 | + System.out.println("Height (int): "); |
| 92 | + height = in.nextInt(); |
| 93 | + |
| 94 | + System.out.println("Octave count (int): "); |
| 95 | + octaveCount = in.nextInt(); |
| 96 | + |
| 97 | + System.out.println("Persistence (float): "); |
| 98 | + persistence = in.nextFloat(); |
| 99 | + |
| 100 | + System.out.println("Seed (long): "); |
| 101 | + seed = in.nextLong(); |
| 102 | + |
| 103 | + System.out.println("Charset (String): "); |
| 104 | + charset = in.next(); |
| 105 | + |
| 106 | + |
| 107 | + perlinNoise = generatePerlinNoise(width, height, octaveCount, persistence, seed); |
| 108 | + final char[] chars = charset.toCharArray(); |
| 109 | + final int length = chars.length; |
| 110 | + final float step = 1f / length; |
| 111 | + //output based on charset |
| 112 | + for(int x = 0; x < width; x++) { |
| 113 | + for(int y = 0; y < height; y++) { |
| 114 | + float value = step; |
| 115 | + float noiseValue = perlinNoise[x][y]; |
| 116 | + |
| 117 | + for (char c : chars) { |
| 118 | + if (noiseValue <= value) { |
| 119 | + System.out.print(c); |
| 120 | + break; |
| 121 | + } |
| 122 | + |
| 123 | + value += step; |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + System.out.println(); |
| 128 | + } |
| 129 | + } |
| 130 | +} |
0 commit comments