diff --git a/src/doodles/examples/liskov.py b/src/doodles/examples/liskov.py new file mode 100644 index 0000000..6d2ecab --- /dev/null +++ b/src/doodles/examples/liskov.py @@ -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) diff --git a/src/doodles/shapes.py b/src/doodles/shapes.py index 775e8be..3fb6e96 100644 --- a/src/doodles/shapes.py +++ b/src/doodles/shapes.py @@ -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))