forked from AllenDowney/ThinkPython2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPoint1_soln.py
92 lines (65 loc) · 1.78 KB
/
Point1_soln.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
"""This module contains a code example related to
Think Python, 2nd Edition
by Allen Downey
http://thinkpython2.com
Copyright 2015 Allen Downey
License: http://creativecommons.org/licenses/by/4.0/
"""
from __future__ import print_function, division
import copy
import math
from Point1 import Point, Rectangle
def distance_between_points(p1, p2):
"""Computes the distance between two Point objects.
p1: Point
p2: Point
returns: float
"""
dx = p1.x - p2.x
dy = p1.y - p2.y
dist = math.sqrt(dx**2 + dy**2)
return dist
def move_rectangle(rect, dx, dy):
"""Move the Rectangle by modifying its corner object.
rect: Rectangle object.
dx: change in x coordinate (can be negative).
dy: change in y coordinate (can be negative).
"""
rect.corner.x += dx
rect.corner.y += dy
def move_rectangle_copy(rect, dx, dy):
"""Move the Rectangle and return a new Rectangle object.
rect: Rectangle object.
dx: change in x coordinate (can be negative).
dy: change in y coordinate (can be negative).
returns: new Rectangle
"""
new = copy.deepcopy(rect)
move_rectangle(new, dx, dy)
return new
def main():
blank = Point()
blank.x = 0
blank.y = 0
grosse = Point()
grosse.x = 3
grosse.y = 4
print('distance', end=' ')
print(distance_between_points(grosse, blank))
box = Rectangle()
box.width = 100.0
box.height = 200.0
box.corner = Point()
box.corner.x = 50.0
box.corner.y = 50.0
print(box.corner.x)
print(box.corner.y)
print('move')
move_rectangle(box, 50, 100)
print(box.corner.x)
print(box.corner.y)
new_box = move_rectangle_copy(box, 50, 100)
print(new_box.corner.x)
print(new_box.corner.y)
if __name__ == '__main__':
main()