polygon-area-calculator/shape_calculator.py

54 lines
1.4 KiB
Python
Raw Permalink Normal View History

2023-04-19 07:50:31 +02:00
from __future__ import annotations
2020-09-29 19:14:39 +02:00
class Rectangle:
2023-04-19 07:50:31 +02:00
def __init__(self, width: float, height: float) -> None:
self.width = width
self.height = height
def __repr__(self) -> str:
return f"Rectangle(width={self.width}, height={self.height})"
def set_width(self, width: float) -> None:
self.width = width
def set_height(self, height: float) -> None:
self.height = height
def get_area(self) -> float:
return self.width*self.height
def get_perimeter(self) -> float:
return 2*self.width + 2*self.height
def get_diagonal(self) -> float:
return (self.width**2 + self.height**2)**.5
def get_picture(self) -> str:
if (self.width > 50 or self.height > 50):
return "Too big for picture."
return "\n".join(["*"*self.width for i in range(self.height)])+'\n'
def get_amount_inside(self, other: Rectangle) -> int:
return int(self.width/other.width)*int(self.height/other.height)
class Square(Rectangle):
def __init__(self, side: float) -> None:
super().__init__(side, side)
2020-09-29 19:14:39 +02:00
2023-04-19 07:50:31 +02:00
def __repr__(self) -> str:
return f"Square(side={self.height})"
2020-09-29 19:14:39 +02:00
2023-04-19 07:50:31 +02:00
def set_side(self, side: float) -> None:
self.height = side
self.width = side
2020-09-29 19:14:39 +02:00
2023-04-19 07:50:31 +02:00
def set_height(self, side: float) -> None:
self.set_side(side)
2020-09-29 19:14:39 +02:00
2023-04-19 07:50:31 +02:00
def set_width(self, side: float) -> None:
self.set_side(side)