python_exercises/thinkcspy/Chapter-17-18-Exercises/rectangle.py

35 lines
759 B
Python
Raw Normal View History

from points import Point
2024-02-16 16:56:28 -07:00
class Rectangle:
def __init__(self, pos, width, height):
self.pos = pos
self.width = width
self.height = height
def getWidth(self):
return self.width
def getHeight(self):
return self.height
def __str__(self):
return "Lower Left Coordinates: {}; Width: {}; Height: {}".format(self.pos, self.width, self.height)
def area(self):
return self.width * self.height
def perimeter(self):
return self.width * 2 + self.height * 2
def transpose(self):
width = self.width
self.width = self.height
self.height = width
return self
2024-02-16 16:56:28 -07:00
r = Rectangle(Point(0, 0), 10, 5)
2024-02-16 16:56:28 -07:00
print(r)
print(r.transpose())
2024-02-16 16:56:28 -07:00