From e3e3a32a3b0bc1fd3ba38fb880455c17735c66fa Mon Sep 17 00:00:00 2001 From: Gabriel Le Breton Date: Wed, 31 May 2017 17:31:09 -0400 Subject: [PATCH 1/4] Adds python2 note in ReadMe.md as requested in #4 --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index a0503f3..05129c3 100644 --- a/README.md +++ b/README.md @@ -15,12 +15,17 @@ 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 2. You may use a [virtualenv](http://python-guide-pt-br.readthedocs.io/en/latest/dev/virtualenvs/) + ### How to Run The script will generate several random maps and save them as PNG files. git clone https://github.com/fogleman/PirateMap.git cd PirateMap + # make sure you run python2 then pip install -r requirements.txt python main.py From 0328f0a68d7893dd816b5f98a52319c275ab9513 Mon Sep 17 00:00:00 2001 From: Gabriel Le Breton Date: Wed, 31 May 2017 17:53:37 -0400 Subject: [PATCH 2/4] Converts code to python3 <3 solving #4 --- .gitignore | 2 +- graph.py | 2 +- main.py | 27 +++++++++++++++++---------- poisson_disc.py | 6 +++--- xkcd.py | 2 +- 5 files changed, 23 insertions(+), 16 deletions(-) diff --git a/.gitignore b/.gitignore index 86bd36d..6736760 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ env *.png - +*.pyc diff --git a/graph.py b/graph.py index 31956d5..985fc25 100644 --- a/graph.py +++ b/graph.py @@ -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): diff --git a/main.py b/main.py index 75b5263..ef401ef 100644 --- a/main.py +++ b/main.py @@ -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() @@ -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: @@ -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) diff --git a/poisson_disc.py b/poisson_disc.py index dd6966c..023bb7e 100644 --- a/poisson_disc.py +++ b/poisson_disc.py @@ -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 @@ -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 diff --git a/xkcd.py b/xkcd.py index 2db6d24..d1e5ea2 100644 --- a/xkcd.py +++ b/xkcd.py @@ -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) From 026c3c4d6deda435207ee8c3c3b4778aab44dd4a Mon Sep 17 00:00:00 2001 From: Gabriel Le Breton Date: Wed, 31 May 2017 17:54:19 -0400 Subject: [PATCH 3/4] Updates readme for spreading the Python3 support :tada: --- README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index 05129c3..39bdb40 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ I used several excellent third party libraries... ### Requirements -This project currently uses Python 2. You may use a [virtualenv](http://python-guide-pt-br.readthedocs.io/en/latest/dev/virtualenvs/) +This project currently uses *Python 3*. You may use a [virtualenv](http://python-guide-pt-br.readthedocs.io/en/latest/dev/virtualenvs/) ### How to Run @@ -25,7 +25,6 @@ The script will generate several random maps and save them as PNG files. git clone https://github.com/fogleman/PirateMap.git cd PirateMap - # make sure you run python2 then pip install -r requirements.txt python main.py From 2a9e294f204bf2ca0c09e6b0ad73582dd9d10965 Mon Sep 17 00:00:00 2001 From: Gabriel Le Breton Date: Wed, 31 May 2017 17:56:36 -0400 Subject: [PATCH 4/4] Updates ReadMe to official virtualenv documentation #4 --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 39bdb40..7bcbc1f 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ I used several excellent third party libraries... ### Requirements -This project currently uses *Python 3*. You may use a [virtualenv](http://python-guide-pt-br.readthedocs.io/en/latest/dev/virtualenvs/) +This project currently uses *Python 3*. You may use a [virtualenv](https://virtualenv.pypa.io/) ### How to Run