|
| 1 | +import random |
| 2 | +import matplotlib.pyplot as plt |
| 3 | + |
| 4 | +def draw_line(canvas): |
| 5 | + x1 = random.randint(0, canvas_width) |
| 6 | + y1 = random.randint(0, canvas_height) |
| 7 | + x2 = random.randint(0, canvas_width) |
| 8 | + y2 = random.randint(0, canvas_height) |
| 9 | + plt.plot([x1, x2], [y1, y2], color=random_color()) |
| 10 | + |
| 11 | +def draw_circle(canvas): |
| 12 | + x = random.randint(0, canvas_width) |
| 13 | + y = random.randint(0, canvas_height) |
| 14 | + radius = random.randint(5, 50) |
| 15 | + circle = plt.Circle((x, y), radius, color=random_color(), fill=False) |
| 16 | + canvas.add_artist(circle) |
| 17 | + |
| 18 | +def draw_square(canvas): |
| 19 | + x = random.randint(0, canvas_width) |
| 20 | + y = random.randint(0, canvas_height) |
| 21 | + side_length = random.randint(10, 50) |
| 22 | + square = plt.Rectangle((x, y), side_length, side_length, color=random_color(), fill=False) |
| 23 | + canvas.add_artist(square) |
| 24 | + |
| 25 | +def random_color(): |
| 26 | + return (random.random(), random.random(), random.random()) |
| 27 | + |
| 28 | +#canva size |
| 29 | +canvas_width = 200 |
| 30 | +canvas_height = 200 |
| 31 | + |
| 32 | +#generate size |
| 33 | + |
| 34 | +fig, ax = plt.subplots() |
| 35 | +ax.set_xlim(1, canvas_width) |
| 36 | +ax.set_ylim(2, canvas_height) |
| 37 | +ax.set_aspect('equal', adjustable='box') |
| 38 | + |
| 39 | +#create shapes |
| 40 | + |
| 41 | +num_shapes = random.randint(1, 30) |
| 42 | +for _ in range(num_shapes): |
| 43 | + shape_type = random.choice([draw_line, draw_circle, draw_square]) |
| 44 | + shape_type(ax) |
| 45 | + |
| 46 | +plt.show() |
0 commit comments