(n, shape_points)
| 3 | |
| 4 | # This generator simulates a "chaos game" |
| 5 | def chaos_game(n, shape_points): |
| 6 | # Initialize the starting point |
| 7 | point = [random(), random()] |
| 8 | |
| 9 | for _ in range(n): |
| 10 | # Update the point position and yield the result |
| 11 | point = [(p + s) / 2 for p, s in zip(point, choice(shape_points))] |
| 12 | yield point |
| 13 | |
| 14 | # This will generate a Sierpinski triangle with a chaos game of n points for an |
| 15 | # initial triangle with three points on the vertices of an equilateral triangle: |