class Point(object):
"""Represents a point in 2-D space."""
class Rectangle(object):
"""Represents a rectangle
attributes: width, height, corner.
"""
def __init__(self, x, y, width, height):
pt = Point()
pt.x = x
pt.y = y
self.width = width
self.height = height
self.corner = pt
def __str__(self):
return "(%.3f, %.3f, %.3f, %.3f)" % (
self.corner.x, self.corner.y, self.width, self.height)
def shift(self, dx, dy):
self.corner.x += dx
self.corner.y += dy
def offset(self, dx, dy):
x = self.corner.x + dx
y = self.corner.y + dy
return Rectangle(x, y, self.width, self.height)
def __add__(self, other):
x = min(self.corner.x, other.corner.x)
y = min(self.corner.y, other.corner.y)
mx = max(self.corner.x + self.width, other.corner.x + other.width)
my = max(self.corner.y + self.height, other.corner.y + other.height)
width = mx - x
height = my - y
return Rectangle(x, y, width, height)
r1 = Rectangle(10, 20, 10, 10)
r2 = Rectangle(20, 50, 15, 20)
print(r1 + r2)