def render(self, chromosome, filepath):
"""Render a chromosome to an SVG file."""
import svgwrite
margin = 100
unit = 200
radius = 50
pad = 10
width = (self.width + 1) * unit + margin * 2
height = (self.height + 1) * unit + margin * 2
doc = svgwrite.Drawing(filename=filepath, size=(width, height))
# Color theme to match the talk...
colors = ["#ff9999", "#9999ff", "#99ff99", "#ffffff"]
# Fill colors at random
def channel():
return int(random.triangular(0, 255, 175))
while len(colors) < len(self.roles):
colors.append("#%02x%02x%02x" % (channel(), channel(), channel()))
# Map row, col to pixels
def origin(row, col):
x = row * unit + margin
y = col * unit + margin
return (x, y)
def color_of_group(group):
idx = self.roles.index(group)
return colors[idx]
def color_of_person(person_id):
group = self.people[person_id][1]
return color_of_group(group)
# Render seating assignments
for seat, person in enumerate(chromosome):
row, col = self.map.point_at(seat)
x, y = origin(row, col)
x, y = (x + radius, y + radius)
doc.add(doc.circle(
center=(x, y),
r=radius,
stroke_width=8,
stroke="#000",
fill=color_of_person(person)
))
doc.save()
# pylint: disable=too-many-locals
评论列表
文章目录