#### https://www.freecodecamp.org/learn/scientific-computing-with-python/scientific-computing-with-python-projects/polygon-area-calculator ########## shape_calculator.py ######
class Rectangle:
def __init__(self, width, height):
self.width = width
self.height = height
def set_width(self, width):
self.width = width
def set_height(self, height):
self.height = height
def get_area(self):
return self.width * self.height
def get_perimeter(self):
return 2 * self.width + 2 * self.height
def get_diagonal(self):
return (self.width ** 2 + self.height ** 2) ** .5
def get_picture(self):
plist =[]
if self.width < 50 or self.height < 50:
for i in range(0, self.height):
plist.append(self.width * '*')
return '\n'.join(plist)
else:
return "Too big for picture."
def get_amount_inside(self, shape):
return self.get_area() // (shape.width * shape.height)
def __str__(self):
return f"Rectangle(width = {self.width}, height = {self.height})"
class Square(Rectangle):
def __init__(self, side):
self.width = side
self.height = side
def __str__(self):
return f"Square(side = {self.width})"
def set_width(self, width):
self.width = side
def set_height(self, height):
self.height = side
def set_side(self, side):
self.width = side
self.height = side
# This entrypoint file to be used in development. Start by reading README.md
import os
os.chdir('./shape_calc')
import shape_calculator
rect = shape_calculator.Rectangle(5, 10)
print(rect.get_area())
rect.set_width(3)
print(rect.get_perimeter())
print(rect)
sq = shape_calculator.Square(9)
print(sq.get_area())
sq.set_side(4)
print(sq.get_diagonal())
print(sq)
50 26 Rectangle(width = 3, height = 10) 81 5.656854249492381 Square(side = 4)
rect.set_width(51)
rect.set_height(3)
print(rect.get_picture())
*************************************************** *************************************************** ***************************************************
rect = shape_calculator.Rectangle(10, 5)
print(rect.get_area())
rect.set_height(3)
print(rect.get_perimeter())
print(rect)
print(rect.get_picture())
sq = shape_calculator.Square(9)
print(sq.get_area())
sq.set_side(4)
print(sq.get_diagonal())
print(sq)
print(sq.get_picture())
rect.set_height(8)
rect.set_width(16)
print(rect.get_amount_inside(sq))
50 26 Rectangle(width = 10, height = 3) ********** ********** ********** 81 5.656854249492381 Square(side = 4) **** **** **** **** 8