add liskov demo
This commit is contained in:
parent
8f5b03f34a
commit
2bf6e352ec
25
src/doodles/examples/liskov.py
Normal file
25
src/doodles/examples/liskov.py
Normal 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)
|
@ -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))
|
||||
|
Loading…
Reference in New Issue
Block a user