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)
if __name__ == "__main__":
r1 = Rectangle(10, 20, 30, 40)
print(r1)
r1.shift(-10, -20)
print(r1)
r2 = r1.offset(100, 100)
print(r1)
print(r2)