-
Notifications
You must be signed in to change notification settings - Fork 0
/
worley.py
43 lines (36 loc) · 1.37 KB
/
worley.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# Worley Noise Generator
# http://en.wikipedia.org/wiki/Worley_noise
# FB36 - 20130216
import math
import random
from PIL import Image, ImageDraw
def generate(x, y, points):
imgx, imgy = x, y # image size
image = Image.new("RGB", (imgx, imgy))
ImageDraw.Draw(image)
pixels = image.load()
n = points # of seed points (100)
m = 0 # random.randint(0, n - 1) # degree (?)
seedsX = [random.randint(0, imgx - 1) for i in range(n)]
seedsY = [random.randint(0, imgy - 1) for i in range(n)]
# find max distance
maxDist = 0.0
for ky in range(imgy):
for kx in range(imgx):
# create a sorted list of distances to all seed points
dists = [math.hypot(seedsX[i] - kx, seedsY[i] - ky) for i in range(n)]
dists.sort()
if dists[m] > maxDist:
maxDist = dists[m]
# paint
for ky in range(imgy):
for kx in range(imgx):
# create a sorted list of distances to all seed points
dists = [math.hypot(seedsX[i] - kx, seedsY[i] - ky) for i in range(n)]
dists.sort()
c = int(round(255 * dists[m] / maxDist))
pixels[kx, ky] = (0, 0, c)
# label = "N = " + str(n) + " M = " + str(m)
# draw.text((0, 0), label, (0, 255, 0)) # write to top-left using green color
image.save("floor_map.png", "PNG")
generate(128, 128, 500)