a6_1a.py

class Point(object):
    """Represents a point in 2-D space."""

class Rectangle(object):
    """Represents a rectangle

    attributes: width, height, corner.
    """

def create_rectangle(x, y, width, height):
    pt = Point()
    pt.x = x
    pt.y = y
    r = Rectangle()
    r.width = width
    r.height = height
    r.corner = pt
    return r

def str_rectangle(rect):
    return "(%.3f, %.3f, %.3f, %.3f)" % (rect.corner.x, rect.corner.y,
                                         rect.width, rect.height)

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

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

def rectangle_area(rect):
    return rect.width * rect.height

if __name__ == "__main__":
    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))
    print(str_rectangle(r2))