This repository has been archived by the owner on Sep 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarching_squares.py
187 lines (138 loc) · 6.4 KB
/
marching_squares.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
183
184
185
186
187
import pygame
from vector import Vector2D, randint
def Default_Cellular_Automata_Rule(current, num_neighbours):
birth_requirement = 4
death_requirement = 2
if current:
if num_neighbours < death_requirement:
return False
else:
return True
else:
if num_neighbours > birth_requirement:
return True
else:
return False
def Generate_Cellular_Automata_Grid(marchin_squares_object, iterations=10, fill=60, rule=Default_Cellular_Automata_Rule):
grid = []
for y in range(marchin_squares_object.h+1):
temp = []
for x in range(marchin_squares_object.w+1):
if x == 0 or y == 0 or x == marchin_squares_object.w or y == marchin_squares_object.h:
state = 0
elif randint(0, 100) > fill:
state = 1
else:
state = 0
temp.append(state)
grid.append(temp)
for _ in range(iterations):
temp = grid
for y in range(1, marchin_squares_object.h - 1):
for x in range(1, marchin_squares_object.w - 1):
state = grid[y][x]
n = [
grid[y-1][x],
grid[y+1][x],
grid[y][x-1],
grid[y][x+1],
grid[y-1][x-1],
grid[y+1][x+1],
grid[y-1][x+1],
grid[y+1][x-1],
]
temp[y][x] = rule(state, n.count(True))
grid = temp
return grid
def Generate_Surface_from_Grid(grid):
w, h = len(grid[0]), len(grid)
surface = pygame.Surface((w, h))
for y in range(h):
for x in range(w):
state = grid[y][x]
color = [state * 255 for _ in range(3)]
surface.set_at((x, y), color)
return surface
def Draw_Basic_algorithm(screen, rect, resolution, wall_colour, line_width):
rect.sort(key=lambda elem: Vector2D(0, -1000).dist(elem, use_sqrt=False))
rect[2], rect[3] = rect[3], rect[2]
nibble = ''.join([str(int(i.data["state"])) for i in rect])
x, y = rect[0].get_xy(int)
hr, r = resolution/2, resolution
#region cases
if nibble == '0000' : pass
if nibble == '0001' or nibble == '1110' : pygame.draw.line(screen, wall_colour, (x, y + hr), (x + hr, y + r), line_width)
if nibble == '0010' or nibble == '1101' : pygame.draw.line(screen, wall_colour, (x + r, y + hr), (x + hr, y + r), line_width)
if nibble == '1011' or nibble == '0100' : pygame.draw.line(screen, wall_colour, (x + hr, y), (x + r, y + hr), line_width)
if nibble == '0111' or nibble == '1000' : pygame.draw.line(screen, wall_colour, (x + hr, y), (x, y + hr), line_width)
if nibble == '1100' or nibble == '0011' : pygame.draw.line(screen, wall_colour, (x, y + hr), (x + r, y + hr), line_width)
if nibble == '1001' or nibble == '0110' : pygame.draw.line(screen, wall_colour, (x + hr, y), (x + hr, y + r), line_width)
if nibble == '1010':
pygame.draw.line(screen, wall_colour, (x + hr, y), (x, y + hr), line_width)
pygame.draw.line(screen, wall_colour, (x + r, y + hr), (x + hr, y + r), line_width)
if nibble == '0101':
pygame.draw.line(screen, wall_colour, (x + hr, y), (x + r, y + hr), line_width)
pygame.draw.line(screen, wall_colour, (x, y + hr), (x + hr, y + r), line_width)
#endregion
class Marching_Squares():
def __init__(self, size, resolution=25, celular_automata_iterations=10):
self.resolution = resolution
self.size = size
self.points = []
self.grid = []
self.w = int(self.size[1] / self.resolution)
self.h = int(self.size[0] / self.resolution)
def draw(self, screen, display_points=False, wall_colour=[120, 60, 0], line_width=3):
if self.grid != [] and self.points != []:
for rect in self.grid:
Draw_Basic_algorithm(screen, rect, self.resolution, wall_colour, line_width)
if not display_points : return
for point in self.points:
color = [(point.data["state"]) * 255 for _ in range(3)]
pygame.draw.circle(screen, color, point.get_xy(int), 4)
else:
raise Exception("Please first run either from_grid or randomise")
def randomise(self):
# create points
self.points = []
for y in range(self.h + 1):
for x in range(self.w + 1):
vec = Vector2D((x * self.resolution), (y * self.resolution))
vec.data["state"] = randint(0, 1)
self.points.append(vec)
# create squares
self.grid = []
for y in range(self.h + 1):
for x in range(self.w + 1):
vec = Vector2D((x * self.resolution) + self.resolution/2, (y * self.resolution) + self.resolution/2)
dist = []
for point in self.points:
dist.append(int(point.dist(vec)))
dist = min(dist)
temp = []
for point in self.points:
if int(point.dist(vec)) == dist : temp.append(point)
if len(temp) == 4:
self.grid.append(temp)
def from_grid(self, grid):
# create points
self.points = []
for y in range(self.h + 1):
for x in range(self.w + 1):
vec = Vector2D((x * self.resolution), (y * self.resolution))
vec.data["state"] = not grid[y][x]
self.points.append(vec)
# create squares
self.grid = []
for y in range(self.h + 1):
for x in range(self.w + 1):
vec = Vector2D((x * self.resolution) + self.resolution/2, (y * self.resolution) + self.resolution/2)
dist = []
for point in self.points:
dist.append(int(point.dist(vec, use_sqrt=False)))
dist = min(dist)
temp = []
for point in self.points:
if int(point.dist(vec, use_sqrt=False)) == dist : temp.append(point)
if len(temp) == 4:
self.grid.append(temp)