forked from fogleman/PirateMap
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
174 lines (166 loc) · 5.02 KB
/
main.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
from alpha_shape import alpha_shape
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
def make_layer():
x = layers.Noise(8).add(layers.Constant(0.6)).clamp()
x = x.translate(random.random() * 1000, random.random() * 1000)
x = x.scale(0.005, 0.005)
x = x.subtract(layers.Distance(256, 256, 256))
return x
def render_shape(dc, shape):
if shape.is_empty:
return
if isinstance(shape, MultiPolygon):
for child in shape.geoms:
render_shape(dc, child)
if isinstance(shape, Polygon):
dc.new_sub_path()
for x, y in shape.exterior.coords:
dc.line_to(x, y)
dc.close_path()
def render_mark(dc, x, y):
n = 8
dc.move_to(x - n, y - n)
dc.line_to(x + n, y + n)
dc.move_to(x - n, y + n)
dc.line_to(x + n, y - n)
def render_compass(dc):
w, h = 4, 32
dc.line_to(-w, 0)
dc.line_to(0, h)
dc.line_to(w, 0)
dc.line_to(0, -h)
dc.close_path()
dc.set_source_rgb(*Color('#FFFFFF').rgb)
dc.set_line_width(4)
dc.stroke_preserve()
dc.fill()
dc.line_to(-w, 0)
dc.line_to(w, 0)
dc.line_to(0, -h)
dc.close_path()
dc.set_source_rgb(*Color('#DC3522').rgb)
dc.fill()
dc.save()
dc.translate(0, -h * 3 / 2 - 8)
w, h = 5, 15
dc.line_to(-w, h)
dc.line_to(-w, 0)
dc.line_to(w, h)
dc.line_to(w, 0)
dc.set_source_rgb(*Color('#FFFFFF').rgb)
dc.stroke()
dc.restore()
def render_curve(dc, points, alpha):
items = zip(points, points[1:], points[2:], points[3:])
# dc.line_to(*points[0])
# dc.line_to(*points[1])
for (x1, y1), (x2, y2), (x3, y3), (x4, y4) in items:
a1 = math.atan2(y2 - y1, x2 - x1)
a2 = math.atan2(y4 - y3, x4 - x3)
cx = x2 + math.cos(a1) * alpha
cy = y2 + math.sin(a1) * alpha
dx = x3 - math.cos(a2) * alpha
dy = y3 - math.sin(a2) * alpha
dc.curve_to(cx, cy, dx, dy, x3, y3)
# dc.line_to(*points[-1])
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]))
for start in reversed(points):
path = graph.shortest_path(g, end, start)
if path:
return path
def render(seed=None):
random.seed(seed)
width = height = 512
scale = 2
surface = cairo.ImageSurface(cairo.FORMAT_RGB24,
width * scale, height * scale)
dc = cairo.Context(surface)
dc.set_line_cap(cairo.LINE_CAP_ROUND)
dc.set_line_join(cairo.LINE_JOIN_ROUND)
dc.scale(scale, scale)
layer = make_layer()
# layer.save('layer.png', 0, 0, width, height)
points = poisson_disc(0, 0, width, height, 8, 16)
shape1 = layer.alpha_shape(points, 0.1, 1, 0.1).buffer(-4).buffer(4)
shape2 = layer.alpha_shape(points, 0.3, 1, 0.1).buffer(-8).buffer(4)
shape3 = layer.alpha_shape(points, 0.12, 0.28, 0.1).buffer(-12).buffer(4)
points = [x for x in points
if shape1.contains(Point(*x)) and layer.get(*x) >= 0.25]
path = find_path(layer, points, 16)
mark = path[0]
# water background
dc.set_source_rgb(*Color('#2185C5').rgb)
dc.paint()
# shallow water
n = 5
shape = shape1.simplify(8).buffer(32).buffer(-16)
shapes = [shape]
for _ in range(n):
shape = shape.simplify(8).buffer(64).buffer(-32)
shape = xkcdify(shape, 2, 8)
shapes.append(shape)
shapes.reverse()
c1 = Color('#4FA9E1')
c2 = Color('#2185C5')
for c, shape in zip(c2.range_to(c1, n), shapes):
dc.set_source_rgb(*c.rgb)
render_shape(dc, shape)
dc.fill()
# height
dc.save()
dc.set_source_rgb(*Color('#CFC291').rgb)
for _ in range(5):
render_shape(dc, shape1)
dc.fill()
dc.translate(0, 1)
dc.restore()
# sandy land
dc.set_source_rgb(*Color('#FFFFA6').rgb)
render_shape(dc, shape1)
dc.fill()
# grassy land
dc.set_source_rgb(*Color('#BDF271').rgb)
render_shape(dc, shape2)
dc.fill()
# dark sand
dc.set_source_rgb(*Color('#CFC291').rgb)
render_shape(dc, shape3)
dc.fill()
# path
dc.set_source_rgb(*Color('#DC3522').rgb)
render_curve(dc, path, 4)
dc.set_dash([4])
dc.stroke()
dc.set_dash([])
# mark
dc.set_source_rgb(*Color('#DC3522').rgb)
render_mark(dc, *mark)
dc.set_line_width(4)
dc.stroke()
# compass
dc.save()
dc.translate(48, height - 64)
dc.rotate(random.random() * math.pi / 4 - math.pi / 8)
render_compass(dc)
dc.restore()
return surface
if __name__ == '__main__':
for seed in range(100):
print seed
surface = render(seed)
surface.write_to_png('out%04d.png' % seed)