📚 The CoCalc Library - books, templates and other resources
License: OTHER
"""This module contains a code example related to12Think Python, 2nd Edition3by Allen Downey4http://thinkpython2.com56Copyright 2015 Allen Downey78License: http://creativecommons.org/licenses/by/4.0/9"""1011from __future__ import print_function, division121314class Point:15"""Represents a point in 2-D space.1617attributes: x, y18"""192021def print_point(p):22"""Print a Point object in human-readable format."""23print('(%g, %g)' % (p.x, p.y))242526class Rectangle:27"""Represents a rectangle.2829attributes: width, height, corner.30"""313233def find_center(rect):34"""Returns a Point at the center of a Rectangle.3536rect: Rectangle3738returns: new Point39"""40p = Point()41p.x = rect.corner.x + rect.width/2.042p.y = rect.corner.y + rect.height/2.043return p444546def grow_rectangle(rect, dwidth, dheight):47"""Modifies the Rectangle by adding to its width and height.4849rect: Rectangle object.50dwidth: change in width (can be negative).51dheight: change in height (can be negative).52"""53rect.width += dwidth54rect.height += dheight555657def main():58blank = Point()59blank.x = 360blank.y = 461print('blank', end=' ')62print_point(blank)6364box = Rectangle()65box.width = 100.066box.height = 200.067box.corner = Point()68box.corner.x = 0.069box.corner.y = 0.07071center = find_center(box)72print('center', end=' ')73print_point(center)7475print(box.width)76print(box.height)77print('grow')78grow_rectangle(box, 50, 100)79print(box.width)80print(box.height)818283if __name__ == '__main__':84main()85868788