Skip to content

Commit

Permalink
Add shape demonstration classes
Browse files Browse the repository at this point in the history
  • Loading branch information
mdpiper committed Jul 25, 2023
1 parent 221be8c commit 11dd9e8
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
18 changes: 18 additions & 0 deletions lessons/python/shapes/circle.py
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions lessons/python/shapes/rectangle.py
Original file line number Diff line number Diff line change
@@ -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
16 changes: 16 additions & 0 deletions lessons/python/shapes/shape.py
Original file line number Diff line number Diff line change
@@ -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:
...
11 changes: 11 additions & 0 deletions lessons/python/shapes/square.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 11dd9e8

Please sign in to comment.