Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from shared import Shape
- class Square(Shape):
- def __init__(self, x, y, side_length):
- self.x = x
- self.y = y
- self.side_length = side_length
- def area(self):
- return self.side_length ** 2
- def to_dict(self):
- return {
- "type": "Square",
- "attributes": {
- "x": self.x,
- "y": self.y,
- "side_length": self.side_length
- }
- }
- def __str__(self):
- return f"Square at ({self.x}, {self.y}) with side length {self.side_length}"
- class Circle(Shape):
- def __init__(self, x, y, radius):
- self.x = x
- self.y = y
- self.radius = radius
- def area(self):
- import math
- return math.pi * (self.radius ** 2)
- def to_dict(self):
- return {
- "type": "Circle",
- "attributes": {
- "x": self.x,
- "y": self.y,
- "radius": self.radius
- }
- }
- def __str__(self):
- return f"Circle at ({self.x}, {self.y}) with radius {self.radius}"
- class Ellipse(Shape):
- def __init__(self, x, y, width, height):
- self.x = x
- self.y = y
- self.width = width
- self.height = height
- def area(self):
- import math
- return math.pi * (self.width / 2) * (self.height / 2)
- def to_dict(self):
- return {
- "type": "Ellipse",
- "attributes": {
- "x": self.x,
- "y": self.y,
- "width": self.width,
- "height": self.height
- }
- }
- def __str__(self):
- return f"Ellipse at ({self.x}, {self.y}) with width {self.width} and height {self.height}"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement