Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Java Perlin Noise seed addition

  1. #1
    Member
    Join Date
    Sep 2013
    Posts
    51
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Java Perlin Noise seed addition

    I got a working perlin noise 1D code and it always outputs the same result. I want to add a seed to it, that it only gives the same when using the same seed, but I don't know where I should put it.

    Here's the class with the Perlin noise:

    public class ClassNoise
    {
    	public static List<Integer[]> primes = new ArrayList<Integer[]>();
    	static
    	{
    		int prime1 = 0;
    		int prime2 = 0;
    		int prime3 = 0;
    		for (int i = 1; i < 10000000; i+=2)
    		{
     
    			if (isPrime(i))
    			{
    				prime1 = i;
    				for (; i < 10000000; i+=2)
    				{
    					if (isPrime(i))
    					{
    						//primes.add(i);
    						prime2 = i;
    						for (; i < 10000000; i+=2)
    						{
    							if (isPrime(i))
    							{
    								prime3 = i;
     
    								primes.add(new Integer[]{prime1,prime2,prime3});
    							}
    						}
    					}
    				}
    			}
    		}
    	}
     
    	public static float Noise(int x, int octave)
    	{
    		x = (x << 13) ^ x;
    		return (float) (1.0 - ((x
    				* (x * x * primes.get(octave)[0] + primes.get(octave)[1]) + primes.get(octave)[2]) & Integer.MAX_VALUE) / 1073741824f);
    	}
     
    	public static float PerlinNoise1D(float x, float persistence, int octaves)
    	{
    		float total = 0;
    		float p = persistence;
    		int n = octaves - 1;
     
    		float[] octavePostMutliply = { 1, 1, 1, 1, 1, 1, 1, 1 };
     
    		for (int i = 0; i <= n; i++)
    		{
     
    			float frequency = (float) Math.pow(2, i);
    			double amplitude = Math.pow(p, octaves - i);
    			total += InterpolatedNoise(x * frequency, i) * amplitude
    					* octavePostMutliply[i];
    		}
     
    		return total;
     
    	}
     
    	private static float InterpolatedNoise(float x, int octave)
    	{
    		int integer_X = (int) x;
    		float fractional_X = x - integer_X;
     
    		float v1 = SmoothNoise1D(integer_X, octave);
    		float v2 = SmoothNoise1D(integer_X + 1, octave);
     
    		return CosineInterpolate(v1, v2, fractional_X);
     
    	}
     
    	public static float CosineInterpolate(float a, float b, float x)
    	{
    		float ft = (float) (x * Math.PI);
    		float f = (float) ((1 - Math.cos(ft)) * 0.5);
     
    		return a * (1 - f) + b * f;
    	}
     
    	public static float SmoothNoise1D(int x, int octave)
    	{
    		return Noise(x, octave) / 2 + Noise(x - 1, octave) / 4 + Noise(x + 1, octave) / 4;
    	}
     
    	private static boolean isPrime(int n)
    	{
    		if (n % 2 == 0)
    			return false;
    		for (int i = 3; i * i <= n; i += 2)
    		{
    			if (n % i == 0)
    				return false;
    		}
    		return true;
    	}
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Java Perlin Noise seed addition

    I'm not sure what Perlin is, but if a seed is permitted, it would usually be fed to a constructor, similar to how the Random() constructor can be fed with a long value as a seed. Check the Perlin API to see if there is a constructor that allows a seed.

Similar Threads

  1. Java Perlin Noise 1D terrain generation problem
    By Bingo90 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: September 12th, 2013, 11:59 AM
  2. Generate random numbers between 1 and 52 by passing a seed to Random.
    By Shareefuddin in forum Object Oriented Programming
    Replies: 2
    Last Post: April 22nd, 2013, 09:48 AM
  3. Game of Life 2-D matrix random seed boolean array PLEASE
    By Eriosblood in forum Collections and Generics
    Replies: 20
    Last Post: September 3rd, 2012, 06:10 PM
  4. Addition
    By ruffu054 in forum What's Wrong With My Code?
    Replies: 17
    Last Post: August 14th, 2011, 10:49 PM
  5. Perlin Noise (and other Java 2d graphics questions)
    By helloworld922 in forum AWT / Java Swing
    Replies: 3
    Last Post: June 11th, 2010, 07:42 PM