add liskov demo

This commit is contained in:
James Turk 2024-04-30 21:23:23 -05:00
parent 8f5b03f34a
commit 2bf6e352ec
2 changed files with 26 additions and 1 deletions

View File

@ -0,0 +1,25 @@
"""
Demo of the interchangable nature of these classes.
"""
from doodles import Polygon, Line, Rectangle, Circle, Color
import random
import math
types = [Polygon, Line, Rectangle, Circle]
def rainbow(t) -> tuple[int, int, int]:
"""cycles through colors based on time"""
t = t % 1.0
r = int(255 * (1 + math.sin(2 * math.pi * (t + 0.0 / 3))) / 2)
g = int(255 * (1 + math.sin(2 * math.pi * (t + 1.0 / 3))) / 2)
b = int(255 * (1 + math.sin(2 * math.pi * (t + 2.0 / 3))) / 2)
return (r, g, b)
def create():
for _ in range(100):
DoodleType = random.choice(types)
doodle = DoodleType().random().animate("color", rainbow)

View File

@ -126,7 +126,7 @@ class Polygon(Doodle):
self._points.append(point)
return self
def random(self, n_points: int) -> Self:
def random(self, n_points: int = 5) -> Self:
super().random()
for _ in range(n_points):
self.point((random.random() * 100 - 50, random.random() * 100 - 50))