Question

I've looked around and it seems most use it for textures and I find myself stuck trying to figure out how to implement Perlin/Simplex Noise to a set of 2 dimensional coordinates x and y.

All I'm trying to do is input 2 random coordinates, x & y and then it returns an altered x & y, or is this not possible?

I'm using pythons noise to allow me to create the coordinates, but I find myself that perlin noise is quite complex to understand and cannot fully comprehend how it works.

Was it helpful?

Solution

It is possible to use Perlin Noise for adding some uncertainty to a collection of 2D coordinates (if the collection is a single pair, that is fine too). You can consider the returned noise value (in the range [-1, 1] for the pointed library) together with some factor to determine how much it affects your input coordinates. The larger the factor, the greater impact the noise has over your data. Here is one of the simplest possible examples:

from noise import snoise2 # Simplex noise for 2D points

x, y = 0.5, 0.3
factor = 0.1
n = snoise2(x, y)
print x + n * factor, y + n * factor

We can also consider a much larger factor and apply the same idea to images. Considering factor = 15 and rounding the resulting coordinates to the nearest neighbor, we go from the image at left to the image at right:

enter image description here enter image description here

The complete code for obtaining the image follows. The factors n1 and n2 were used to obtain a "less boring" image.

import sys
from noise import snoise2
from PIL import Image

img = Image.open(sys.argv[1]).convert('L')
result = Image.new('L', img.size)
width, height = img.size

factor = 15
res = result.load()
im = img.load()
for x in xrange(width):
    for y in xrange(height):
        n1 = snoise2(x, y)
        n2 = snoise2(y, x)
        pt = [int(round(x + n1 * factor)), int(round(y + n2 * factor))]
        pt[0] = min(max(0, pt[0]), width - 1)
        pt[1] = min(max(0, pt[1]), height - 1)
        res[x, y] = im[tuple(pt)]
result.save(sys.argv[2])

Of course this isn't even scratching how Perlin Noise can be used. As another example, given a certain function, you can "noisify" the inputs and combine with the mentioned factor to create different outputs. For instance, here is the result of doing that on some function based on cosine:

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top