Noise in 2D

For anyone wondering, how to set something up, here is some code for you :slight_smile: (Recursive algorithm)
I haven’t added comments, nor I will add any explanation. If you don’t know, what Perlin’s Noise is, that code is not for you :stuck_out_tongue: Google it :smiley:
Also, I’ts posted in Shared Code, so I think, thats okey :slight_smile:

package /*TOP-SECRET*/

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;

public class PerlinNoise2D {
	
	private float[][] values;
	private float roughness;
	private float varyation;
	private Random rand;

	public PerlinNoise2D(int size, float roughness, float varyation, float seed, Random rand) {
		this.roughness = roughness;
		this.varyation = varyation;
		this.rand = rand;
		
		int realsize = 1;
		while(realsize < size) {
			realsize *= 2;
		}
		realsize += 1;
		values = new float[realsize][realsize];
		
		int firststep = realsize/2;
		values[0][0] = seed;
		values[realsize-1][0] = seed;
		values[0][realsize-1] = seed;
		values[realsize-1][realsize-1] = seed;
		values[0][firststep] = seed;
		values[firststep][0] = seed;
		values[firststep][firststep] = seed;
		values[firststep][realsize-1] = seed;
		values[realsize-1][firststep] = seed;
		
		noise(0, 0, realsize-1);
	}
	
	private void noise(int x, int y, int step) {
		if (step > 1) {
			int halfStep = step/2;
			float vary = (step*varyation)*roughness;
			
			values[x+halfStep][y] = values[x+halfStep][y] == 0f ? 
					vary(mid(values[x][y], values[x+step][y]), vary) : values[x+halfStep][y];
					
			values[x][y+halfStep] = values[x][y+halfStep] == 0f ? 
					vary(mid(values[x][y], values[x][y+step]), vary) : values[x][y+halfStep];
			
			values[x+step][y+halfStep] = values[x+step][y+halfStep] == 0f ? 
					vary(mid(values[x+step][y], values[x+step][y+step]), vary) : values[x+step][y+halfStep];
					
			values[x+halfStep][y+step] = values[x+halfStep][y+step] == 0f ? 
					vary(mid(values[x][y+step], values[x+step][y+step]), vary) : values[x+halfStep][y+step];
			
			values[x+halfStep][y+halfStep] = vary(mid(
					values[x][y],
					values[x+halfStep][y],
					values[x+step][y],
					values[x][y+halfStep],
					values[x+step][y+halfStep],
					values[x][y+step],
					values[x+halfStep][y+step],
					values[x+step][y+step]), vary);
			
			noise(x, y, halfStep);
			noise(x+halfStep, y, halfStep);
			noise(x, y+halfStep, halfStep);
			noise(x+halfStep, y+halfStep, halfStep);
		}
	}
	
	public float randRange(float range, Random rand) {
		return rand.nextBoolean() ? -rand.nextFloat()*range : rand.nextFloat()*range;
	}
	
	public float vary(float val, float vary) {
		return val+randRange(vary, rand);
	}
	
	public float mid(float... vals) {
		float sum = 0;
		for (int i = 0; i < vals.length; i++) {
			sum += vals[i];
		}
		return sum / vals.length;
	}
	
	public float[][] get() {
		return values;
	}
	
	public static void main(String[] args) {
		System.out.println("Started.");
		int size = 512;
		PerlinNoise2D pn2d = new PerlinNoise2D(size, 0.4f, 1, 20000f, new Random());
		float[][] vals = pn2d.get();
		BufferedImage img = new BufferedImage(size+1, size+1, BufferedImage.TYPE_INT_ARGB);
		for (int x = 0; x < vals.length; x++) {
			for (int y = 0; y < vals[x].length; y++) {
				img.setRGB(x, y, ((int)vals[x][y]) | 0xFF000000);
			}
		}
		try {
			saveImg(new File("Heightmap.png"), img);
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println("Finished.");
	}
	
	public static void saveImg(File f, BufferedImage img) throws IOException {
		f.createNewFile();
		ImageIO.write(img, "PNG", f);
	}
	
}

Hmmm. Might come in handy one day. Care to post a screenshot of a produced image? You also misspelled variation. =S

Oops… sorry, english is not my native language :smiley:

Acctually, these produced images are just for showing, that it works, and to visualize them.

The “beautiness” of these Images TOTALLY depends on your settings in the constructor of PerlinNoise2D.
Also, I just say “int color on that pixel is just the value of the perlin noise at the position of that pixel”.

But anyways:

The green and blue colors aren’t ment to be like that, that was by accident :smiley:
The hard edges are there due to wrapping of that nice int-generic-type… (wrapping to negative values…)

This is not PerlinNoise. :clue:

? :persecutioncomplex:

This is perlin noise:

Not everything that looks noisy is actually perlin noise.

Mr. Perlin invented a very specific algorithm, and yours is simply something else. :slight_smile:

Comparisons! Comparisons!

Perlin actually created a few different algorithms that all have his name, and there’s certainly a lot out there with his name that aren’t his algorithms. Perhaps that’s value noise?

Yes indeed: it’s a value noise variant.

This kind of looks like Diamond square. I didn’t look too closely, so I might be wrong.

That’s right.

That describes how you’re combining the base noise. The base noise is value noise.

… okey… I’ve just heard of “Diamond-Square”-Algorithms. And guys told me, that would be Perlin’s Noise… That’s all I know :slight_smile:
Awesome Article: http://www.gameprogrammer.com/fractal.html
He/She in the Article talks about the mid-displacement algorithm.

depending on your need, alternativly you may use this one ^^ :

http://www.java-gaming.org/topics/fastest-perlinnoise-improved-version-bicubic-amp-bilinear-grad-amp-value-noise/23771/view.html

Yeah… realized it after that posting :smiley:

But, I don’t need it anyways. It’s just a fixed version of the algorithm, I created for my Ludum Dare game… ;D