-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGrammar.py
170 lines (146 loc) · 4.55 KB
/
Grammar.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
import random
import Rule
import File
import main
import matplotlib.pyplot as plt
from shapely.geometry import Polygon
from shapely.geometry import Point
from shapely.geometry import LineString
from shapely.ops import cascaded_union
class Grammar:
def __init__(self):
self.final_shape = None
self.polygons = []
self.rooms = []
self.lines = []
@staticmethod
def lateral():
rand = random.randint(0,1)
if rand > 0:
return True
return False
@staticmethod
def choose_origin(x_val,y_val):
x_seed = random.randint(0,1)
y_seed = random.randint(0,1)
if x_seed: origin_x = max(x_val)
else: origin_x = min(x_val)
if y_seed: origin_y = max(y_val)
else: origin_y = min(y_val)
return origin_x,origin_y
def get_collision(self, child = None):
for shape in self.rooms:
if child.intersects(shape):
return True
return False
def display(self):
try:
x,y = self.final_shape.exterior.xy
except AttributeError:
print "Trying again..."
main.main()
plt.plot(x,y,color='black')
for polygon in self.rooms:
#if polygon.within(self.final_shape):
x,y = polygon.exterior.xy
plt.plot(x,y,color='black')
plt.axis('off')
plt.savefig("a_house.png")
file = File.ImageOps("a_house.png")
file.add_label("The Ulysses House")
plt.show()
def generate_parent(self):
orientation = self.lateral()
if orientation:
polygon = Polygon(
[
(0,0),
(100,0),
(100,50),
(0,50)
]
)
else:
polygon = Polygon(
[
(0,0),
(0,100),
(50,100),
(50,0)
]
)
self.generate_rooms(polygon,1)
self.polygons.append(polygon)
def generate_child(self, count, previous = None):
orientation = self.lateral()
rule = Rule.Rule()
if count == 0:
return
if previous:
latest = previous
else:
latest = self.polygons[len(self.polygons) - 1]
x_val, y_val = latest.exterior.xy
x, y = self.choose_origin(x_val,y_val)
if orientation:
polygon = Polygon(
[
(x,y),
(x+100*rule.scale_factor,y),
(x+100*rule.scale_factor,y+50*rule.scale_factor),
(x,y+50*rule.scale_factor)
]
)
else:
polygon = Polygon(
[
(x,y),
(x,y+100*rule.scale_factor),
(x+50*rule.scale_factor,y+100*rule.scale_factor),
(x+50*rule.scale_factor,y)
]
)
self.generate_rooms(polygon,random.randint(1,5))
self.polygons.append(polygon)
count -= 1
self.generate_child(count, polygon)
def generate_rooms(self, polygon, count):
room = None
if count == 0: return
x_vals, y_vals = polygon.exterior.xy
_x = min(x_vals)
_y = max(y_vals)
orig_x, orig_y = _x,_y
origin = (_x,_y)
#TOP RIGHT
while Point(_x+.1,_y-.1).within(polygon):
_x += .1
topright = (_x,_y)
#BOTTOM LEFT
_x = orig_x
while Point(_x+.1,_y-.1).within(polygon):
_y -= .1
bottomleft = (_x,_y)
#BOTTOM RIGHT
_x = orig_x
_y = orig_y
while Point(_x-.1,_y-.1).within(polygon):
_y -= .1
bottomright = (_x,_y)
room = Polygon(
[
origin,
topright,
bottomright,
bottomleft
]
)
print room
if self.get_collision(room):
print "ACCIDENT!"
return
self.rooms.append(room)
count -= 1
self.generate_rooms(polygon, count)
def generate_outline(self):
self.final_shape = cascaded_union(self.polygons)