Source for: lesson2.py [raw]

 1class Box():
 2
 3    def __init__(self, x=0, y=0, w=1, h=1):
 4        """Accept arguments to define our box, and store them."""
 5        self.x = x
 6        self.y = y
 7        self.w = w
 8        self.h = h
 9
10    def __repr__(self):
11        return "Box(%s, %s, %s, %s)" % (self.x, self.y, self.w, self.h)
12
13
14many_boxes = [Box() for i in range(5000)]
15
16# We add a "separation" constant so you can see the boxes individually
17separation = .2
18
19
20def layout(boxes):
21    for i, box in enumerate(boxes):
22        box.x = i * (1 + separation)
23
24
25layout(many_boxes)
26
27import svgwrite
28
29
30def draw_boxes(boxes, fname, size):
31    dwg = svgwrite.Drawing(fname, profile="full", size=size)
32    # Draw all the boxes
33    for box in boxes:
34        dwg.add(
35            dwg.rect(
36                insert=(f"{box.x}cm", f"{box.y}cm"),
37                size=(f"{box.w}cm", f"{box.h}cm"),
38                fill="red",
39            )
40        )
41    dwg.save()
42
43
44draw_boxes(many_boxes, "lesson2.svg", ("5cm", "2cm"))
45