diff --git a/lessons/python/shapes/circle.py b/lessons/python/shapes/circle.py new file mode 100644 index 0000000..42a6851 --- /dev/null +++ b/lessons/python/shapes/circle.py @@ -0,0 +1,18 @@ +import numpy as np + +from .shape import Shape + +MANY_POINTS = 100 + + +class Circle(Shape): + def __init__(self, center: tuple = (1.0, 1.0), radius: float = 1.0) -> None: + self.c = center + self.r = radius + theta = np.linspace(0.0, 2 * np.pi, MANY_POINTS) + x = self.c[0] + self.r * np.cos(theta) + y = self.c[1] + self.r * np.sin(theta) + super().__init__(x, y) + + def calculate_area(self) -> None: + self.area = np.pi * self.r**2 diff --git a/lessons/python/shapes/rectangle.py b/lessons/python/shapes/rectangle.py new file mode 100644 index 0000000..4781dda --- /dev/null +++ b/lessons/python/shapes/rectangle.py @@ -0,0 +1,19 @@ +import numpy as np + +from .shape import Shape + + +class Rectangle(Shape): + def __init__( + self, lower_left: tuple = (1.0, 1.0), upper_right: tuple = (3.0, 2.0) + ) -> None: + self.ll = lower_left + self.ur = upper_right + x = [self.ll[0], self.ur[0], self.ur[0], self.ll[0]] + y = [self.ll[1], self.ll[1], self.ur[1], self.ur[1]] + super().__init__(x, y) + + def calculate_area(self) -> None: + width = self.ur[0] - self.ll[0] + height = self.ur[1] - self.ll[1] + self.area = width * height diff --git a/lessons/python/shapes/shape.py b/lessons/python/shapes/shape.py new file mode 100644 index 0000000..0d69868 --- /dev/null +++ b/lessons/python/shapes/shape.py @@ -0,0 +1,16 @@ +from abc import ABC, abstractmethod + +import numpy as np + + +class Shape(ABC): + @abstractmethod + def __init__(self, x: np.ndarray, y: np.ndarray) -> None: + self.x = x + self.y = y + self.n_sides = len(x) + self.area = None + + @abstractmethod + def calculate_area(self) -> None: + ... diff --git a/lessons/python/shapes/square.py b/lessons/python/shapes/square.py new file mode 100644 index 0000000..a26727c --- /dev/null +++ b/lessons/python/shapes/square.py @@ -0,0 +1,11 @@ +import numpy as np + +from .rectangle import Rectangle + + +class Square(Rectangle): + def __init__( + self, lower_left: tuple = (1.0, 1.0), side_length: float = 2.0 + ) -> None: + upper_right = (lower_left[0] + side_length, lower_left[1] + side_length) + super().__init__(lower_left, upper_right)