-
Notifications
You must be signed in to change notification settings - Fork 0
/
hexamids.py
182 lines (138 loc) · 6.96 KB
/
hexamids.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
175
176
177
178
179
180
181
182
from math import cos, pi, sin, sqrt
import random
from geometer import *
class ContextFiller:
def __init__(self, canvas, fill_color):
self.canvas = canvas
self.original_fill_color = canvas.fill_color
self.fill_color = fill_color
def __enter__(self):
self.canvas.set_fill_color(self.fill_color)
return self.canvas
def __exit__(self, exc_type, exc_val, exc_tb):
self.canvas.set_fill_color(self.original_fill_color)
class ContextStroker:
def __init__(self, canvas, stroke_width, stroke_color):
self.canvas = canvas
self.original_stroke_width = canvas.stroke_width
self.original_stroke_color = canvas.stroke_color
self.stroke_width = stroke_width
self.stroke_color = stroke_color
def __enter__(self):
self.canvas.set_stroke_width(self.stroke_width)
self.canvas.set_stroke_color(self.stroke_color)
return self.canvas
def __exit__(self, exc_type, exc_val, exc_tb):
self.canvas.set_stroke_width(self.original_stroke_width)
self.canvas.set_stroke_color(self.original_stroke_color)
def random_point_within(min_x, max_x, min_y, max_y):
rand_x = random.random() * (max_x - min_x) + min_x
rand_y = random.random() * (max_y - min_y) + min_y
return Point(rand_x, rand_y)
def random_point_within_circle(center, radius):
t = 2 * pi * random.random()
r = sqrt(random.random() * radius *radius)
return Point(r * cos(t) + center.x, r * sin(t) + center.y)
def color_for_point(point, canvas):
"""
:param point: Point
:param canvas: CoreGraphicsCanvas
:return: Color
"""
line = Line.from_origin_with_slope(Point(canvas.width, 0), 2.0/5.0)
distance = line.distance_to(point)
return band(fills, distance, canvas.diagonal, fuzz=True)
def draw(canvas):
hex_size = 128
hex_grid = VerticalHexagonGrid(canvas.center, 256, 10, 10)
for p in hex_grid.points:
if p.distance_to(canvas.center) < 1050:
hex = HorizontalHexagon(hex_size, p)
hex.draw(canvas)
hex_center = random_point_within_circle(p, hex.step * 0.8)
northeast, east, southeast, southwest, west, northwest = hex.points
for point in hex.points:
l = Line(hex_center, point)
l.draw(canvas)
north_t = ArbitraryTriangle([northwest, northeast, hex_center])
northeast_t = ArbitraryTriangle([northeast, east, hex_center])
southeast_t = ArbitraryTriangle([east, southeast, hex_center])
south_t = ArbitraryTriangle([southeast, southwest, hex_center])
southwest_t = ArbitraryTriangle([southwest, west, hex_center])
northwest_t = ArbitraryTriangle([west, northwest, hex_center])
color = base00 # color_for_point(Point(min_x, min_y), canvas)
with ContextStroker(canvas, 0, clear) as fill_only_canvas:
with ContextFiller(fill_only_canvas, color) as north_canvas:
north_t.draw(north_canvas)
with ContextFiller(fill_only_canvas, color.alpha(0.66)) as northeast_canvas:
northeast_t.draw(northeast_canvas)
with ContextFiller(fill_only_canvas, color.alpha(0.33)) as southeast_canvas:
southeast_t.draw(southeast_canvas)
with ContextFiller(fill_only_canvas, color.alpha(0)) as south_canvas:
south_t.draw(south_canvas)
with ContextFiller(fill_only_canvas, color.alpha(0.33)) as southwest_canvas:
southwest_t.draw(southwest_canvas)
with ContextFiller(fill_only_canvas, color.alpha(0.66)) as northwest_canvas:
northwest_t.draw(northwest_canvas)
"""
blocks = 5
inner_margin = 75
bounds_w = canvas.width / (blocks + 2)
bounds_h = canvas.height / (blocks + 2)
for plot_w_idx in range(blocks):
for plot_h_idx in range(blocks):
min_x = (plot_w_idx * bounds_w + bounds_w) + 10
max_x = (plot_w_idx * bounds_w + bounds_w * 2) - 10
min_y = (plot_h_idx * bounds_h + bounds_h) + 10
max_y = (plot_h_idx * bounds_h + bounds_h * 2) - 10
square_center = random_point_within(min_x + inner_margin, max_x - inner_margin, min_y + inner_margin, max_y - inner_margin)
s = Shape(0)
southwest = Point(min_x, min_y)
northwest = Point(min_x, max_y)
northeast = Point(max_x, max_y)
southeast = Point(max_x, min_y)
s.add_point(southwest)
s.add_point(northwest)
s.add_point(northeast)
s.add_point(southeast)
s.draw(canvas)
west = ArbitraryTriangle([southwest, northwest, square_center])
north = ArbitraryTriangle([northwest, northeast, square_center])
east = ArbitraryTriangle([northeast, southeast, square_center])
south = ArbitraryTriangle([southeast, southwest, square_center])
for point in s.points:
l = Line(square_center, point)
l.draw(canvas)
sides = [west, north, east, south]
color = base00 # color_for_point(Point(min_x, min_y), canvas)
with ContextStroker(canvas, 0, clear) as fill_only_canvas:
with ContextFiller(fill_only_canvas, color.alpha(0.5)) as west_canvas:
west.draw(west_canvas)
with ContextFiller(fill_only_canvas, color) as north_canvas:
north.draw(north_canvas)
with ContextFiller(fill_only_canvas, color.alpha(0.75)) as east_canvas:
east.draw(east_canvas)
with ContextFiller(fill_only_canvas, color.alpha(0.25)) as south_canvas:
south.draw(south_canvas)
#
# fill_one = range(4) # Either fill one triangle
# fill_two = zip(range(4), range(1, 4) + [0]) # Or two, adjacent triangles
#
# fill_chance = random.random() * (plot_h_idx + 1)
#
# # fill_chance += Point(min_x, min_y).distance_to(origin) / canvas.diagonal
#
# if fill_chance < 1:
# pair = random.choice(fill_two)
# triangles = sides[pair[0]], sides[pair[1]]
# with ContextStroker(canvas, 0, clear) as fill_only_canvas:
# with ContextFiller(fill_only_canvas, base1) as filled_canvas:
# triangles[0].draw(filled_canvas)
# with ContextFiller(fill_only_canvas, base00) as filled_canvas:
# triangles[1].draw(filled_canvas)
# elif 1 < fill_chance < 1.75:
# triangle = sides[random.choice(fill_one)]
# with ContextStroker(canvas, 0, clear) as fill_only_canvas:
# with ContextFiller(fill_only_canvas, base1) as filled_canvas:
# triangle.draw(filled_canvas)
"""