Skip to content

Instantly share code, notes, and snippets.

@yammesicka
Last active June 4, 2020 04:15
Show Gist options
  • Save yammesicka/acdf7bc9bddc543526ee7afeaf027d2c to your computer and use it in GitHub Desktop.
Save yammesicka/acdf7bc9bddc543526ee7afeaf027d2c to your computer and use it in GitHub Desktop.
Yam's Chaos game
import random
import matplotlib.pyplot as plt
import numpy as np
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def __add__(self, other):
return Point(self.x + other.x, self.y + other.y)
def __sub__(self, other):
return Point(self.x - other.x, self.y - other.y)
def __mul__(self, number):
return Point(self.x * number, self.y * number)
def __truediv__(self, number):
return Point(self.x / number, self.y / number)
def chaos_game(n: int = 0):
bounds = (Point(0, 0), Point(2, 2), Point(4, 0))
current_point = Point(2, 2)
for _ in range(1, n + 1):
plt.scatter(current_point.x, current_point.y)
next_point = random.choice(bounds)
current_point = (current_point + next_point) / 2
plt.draw()
chaos_game(n=5000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment