Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
env
*.png

*.pyc
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ I used several excellent third party libraries...
- `pyhull` for delaunay triangulation
- `Shapely` for all kinds of 2D geometry operations

### Requirements

This project currently uses *Python 3*. You may use a [virtualenv](https://virtualenv.pypa.io/)

### How to Run

The script will generate several random maps and save them as PNG files.
Expand Down
2 changes: 1 addition & 1 deletion graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def shortest_path(graph, start, end):
seen.add(v)
if v == end:
return path
for (neighbor, c) in graph[v].iteritems():
for (neighbor, c) in graph[v].items():
heapq.heappush(queue, (cost + c, neighbor, path))

def make_graph(points, threshold, layer=None):
Expand Down
27 changes: 17 additions & 10 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
from alpha_shape import alpha_shape
import math
import random

import cairocffi as cairo
from colour import Color
from poisson_disc import poisson_disc
from shapely.geometry import Polygon, MultiPolygon, Point
from xkcd import xkcdify
import cairocffi as cairo

import graph
import layers
import math
import noise
import random
from poisson_disc import poisson_disc
from xkcd import xkcdify


def make_layer():
x = layers.Noise(8).add(layers.Constant(0.6)).clamp()
Expand Down Expand Up @@ -78,13 +79,19 @@ def render_curve(dc, points, alpha):
dc.curve_to(cx, cy, dx, dy, x3, y3)
# dc.line_to(*points[-1])


# https://stackoverflow.com/questions/21892989/what-is-the-good-python3-equivalent-for-auto-tuple-unpacking-in-lambda
def star(f):
return lambda args: f(*args)


def find_path(layer, points, threshold):
x = layers.Noise(4).add(layers.Constant(0.6)).clamp()
x = x.translate(random.random() * 1000, random.random() * 1000)
x = x.scale(0.01, 0.01)
g = graph.make_graph(points, threshold, x)
end = max(points, key=lambda (x, y): layer.get(x, y))
points.sort(key=lambda (x, y): math.hypot(x - end[0], y - end[1]))
end = max(points, key=star(lambda x,y: layer.get(x, y)))
points.sort(key=star(lambda x, y: math.hypot(x - end[0], y - end[1])))
for start in reversed(points):
path = graph.shortest_path(g, end, start)
if path:
Expand Down Expand Up @@ -169,6 +176,6 @@ def render(seed=None):

if __name__ == '__main__':
for seed in range(100):
print seed
print(seed)
surface = render(seed)
surface.write_to_png('out%04d.png' % seed)
6 changes: 3 additions & 3 deletions poisson_disc.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ def normalize(self, x, y):
def nearby(self, x, y):
result = []
i, j = self.normalize(x, y)
for p in xrange(i - 2, i + 3):
for q in xrange(j - 2, j + 3):
for p in range(i - 2, i + 3):
for q in range(j - 2, j + 3):
if (p, q) in self.cells:
result.append(self.cells[(p, q)])
return result
Expand All @@ -36,7 +36,7 @@ def poisson_disc(x1, y1, x2, y2, r, n):
grid.insert(x, y)
while active:
ax, ay = random.choice(active)
for i in xrange(n):
for i in range(n):
a = random.random() * 2 * pi
d = random.random() * r + r
x = ax + cos(a) * d
Expand Down
2 changes: 1 addition & 1 deletion xkcd.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def evenly_spaced(points, spacing):
def perturbed(points, spacing, intensity):
result = []
points = evenly_spaced(points, spacing)
noises = [random.random() * 2 - 1 for _ in range(len(points)/2)]
noises = [random.random() * 2 - 1 for _ in range(int(len(points)/2))]
for _ in range(3):
noises = low_pass(noises, 0.3)
noises = normalize(noises, -1, 1)
Expand Down