Module #5 Assignment

 class Point:

    def __init__(self, x, y):
self.x = x
self.y = y

def __str__(self):
return f"({self.x}, {self.y})"

class Rectangle:
""" A class to manufacture rectangle objects """

def __init__(self, posn, w, h):
""" Initialize rectangle at posn, with width w, height h """
self.corner = posn
self.width = w
self.height = h

def __str__(self):
return f"{self.corner}, {self.width}, {self.height}"


def create_rectangle(x, y, w, h):
return Rectangle(Point(x, y), w, h)

def str_rectangle(rect):
return str(rect)

def shift_rectangle(rect, dx, dy):
mx = rect.corner.x
my = rect.corner.y
rect.corner.x = dx + mx
rect.corner.y = dy + my

def offset_rectangle(rect, dx, dy):
mx = rect.corner.x
my = rect.corner.y
return create_rectangle(mx + dx, my + dy, rect.width, rect.height)

r1 = create_rectangle(10, 20, 30, 40)
print(str_rectangle(r1))
shift_rectangle(r1, -10, -20)
print(str_rectangle(r1))
r2 = offset_rectangle(r1, 100, 100)
print(str_rectangle(r1)) # should be same as previous
print(str_rectangle(r2))

Output
(10, 20), 30, 40
(0, 0), 30, 40
(0, 0), 30, 40
(100, 100), 30, 40

Comments