-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathflower.py
More file actions
73 lines (62 loc) · 2.44 KB
/
flower.py
File metadata and controls
73 lines (62 loc) · 2.44 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# PY5 IMPORTED MODE CODE
from dna import DNA
from rectangle import Rectangle
class Flower:
"""Flower phenotype."""
def __init__(self, dna: DNA, x: float, y: float):
self.dna = dna # Flower DNA.
self.fitness = 1.0 # How fit is the flower?
self.rollover_on = False # Is user rolling over this flower?
self.x, self.y = x, y # Position on screen.
self.w, self.h = 70, 140 # Size of square enclosing flower.
self.bounding_box = Rectangle(
self.x - self.w / 2, self.y - self.h / 2, self.w, self.h
)
def show(self) -> None:
"""Display the flower."""
# DNA values such as petal color, petal size, and number of petals.
genes = self.dna.genes
# Set RGB range from 0 to 1 with color_mode() and use map() as needed.
petal_color = color(genes[0], genes[1], genes[2], genes[3])
petal_size = remap(genes[4], 0, 1, 4, 24)
petal_count = floor(remap(genes[5], 0, 1, 2, 16))
center_color = color(genes[6], genes[7], genes[8])
center_size = remap(genes[9], 0, 1, 24, 48)
stem_color = color(genes[10], genes[11], genes[12])
stem_length = remap(genes[13], 0, 1, 50, 100)
push()
translate(self.x, self.y)
# Draw the bounding box.
fill(0, 0.25) if self.rollover_on else no_fill()
stroke(0)
stroke_weight(0.5)
rect_mode(CENTER)
rect(0, 0, self.w, self.h)
# Draw the stem.
translate(0, self.h / 2 - stem_length)
stroke(stem_color)
stroke_weight(4)
line(0, 0, 0, stem_length)
no_stroke()
# Draw the petals.
fill(petal_color)
for i in range(petal_count):
angle = remap(i, 0, petal_count, 0, TAU)
x = petal_size * cos(angle)
y = petal_size * sin(angle)
circle(x, y, petal_size)
# Draw the center.
fill(center_color)
circle(0, 0, center_size)
pop()
# Display fitness value.
text_align(CENTER)
fill(0) if self.rollover_on else fill(0.25)
text(str(floor(self.fitness)), self.x, self.y + 90)
def rollover(self, mx: float, my: float) -> None:
"""Increment fitness if mouse is rolling over flower."""
if self.bounding_box.contains(mx, my):
self.rollover_on = True
self.fitness += 0.25
else:
self.rollover_on = False