-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtriangle.py
More file actions
54 lines (43 loc) · 1.96 KB
/
triangle.py
File metadata and controls
54 lines (43 loc) · 1.96 KB
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
import random
class Triangle:
def __init__(self, img_width, img_height):
x = random.randint(0, int(img_width))
y = random.randint(0, int(img_height))
self.points = [
(x + random.randint(-50, 50), y + random.randint(-50, 50)),
(x + random.randint(-50, 50), y + random.randint(-50, 50)),
(x + random.randint(-50, 50), y + random.randint(-50, 50))]
self.color = (
random.randint(0, 256),
random.randint(0, 256),
random.randint(0, 256),
random.randint(0, 256)
)
self._img_width = img_width
self._img_height = img_height
def __repr__(self):
return "Trangle: %s in color %s" % (','.join([str(p) for p in self.points]), str(self.color))
def mutate(self, sigma=1.0):
mutations = ['shift', 'point', 'color', 'reset']
weights = [30, 35, 30, 5]
mutation_type = random.choices(mutations, weights=weights, k=1)[0]
if mutation_type == 'shift':
x_shift = int(random.randint(-50, 50)*sigma)
y_shift = int(random.randint(-50, 50)*sigma)
self.points = [(x + x_shift, y + y_shift) for x, y in self.points]
elif mutation_type == 'point':
index = random.choice(list(range(len(self.points))))
self.points[index] = (self.points[index][0] + int(random.randint(-50, 50)*sigma),
self.points[index][1] + int(random.randint(-50, 50)*sigma),)
elif mutation_type == 'color':
self.color = tuple(
c + int(random.randint(-50, 50)*sigma) for c in self.color
)
# Ensure color is within correct range
self.color = tuple(
min(max(c, 0), 255) for c in self.color
)
else:
new_triangle = Triangle(self._img_width, self._img_height)
self.points = new_triangle.points
self.color = new_triangle.color