📚 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, division1213import copy14import math1516from Point1 import Point, Rectangle171819def distance_between_points(p1, p2):20"""Computes the distance between two Point objects.2122p1: Point23p2: Point2425returns: float26"""27dx = p1.x - p2.x28dy = p1.y - p2.y29dist = math.sqrt(dx**2 + dy**2)30return dist313233def move_rectangle(rect, dx, dy):34"""Move the Rectangle by modifying its corner object.3536rect: Rectangle object.37dx: change in x coordinate (can be negative).38dy: change in y coordinate (can be negative).39"""40rect.corner.x += dx41rect.corner.y += dy424344def move_rectangle_copy(rect, dx, dy):45"""Move the Rectangle and return a new Rectangle object.4647rect: Rectangle object.48dx: change in x coordinate (can be negative).49dy: change in y coordinate (can be negative).5051returns: new Rectangle52"""53new = copy.deepcopy(rect)54move_rectangle(new, dx, dy)55return new565758def main():59blank = Point()60blank.x = 061blank.y = 06263grosse = Point()64grosse.x = 365grosse.y = 46667print('distance', end=' ')68print(distance_between_points(grosse, blank))6970box = Rectangle()71box.width = 100.072box.height = 200.073box.corner = Point()74box.corner.x = 50.075box.corner.y = 50.07677print(box.corner.x)78print(box.corner.y)79print('move')80move_rectangle(box, 50, 100)81print(box.corner.x)82print(box.corner.y)8384new_box = move_rectangle_copy(box, 50, 100)85print(new_box.corner.x)86print(new_box.corner.y)878889if __name__ == '__main__':90main()91929394