forked from AllenDowney/ThinkPython
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Point1_soln.py
81 lines (59 loc) · 1.66 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
"""This module contains code from
Think Python by Allen B. Downey
http://thinkpython.com
Copyright 2012 Allen B. Downey
License: GNU GPLv3 http://www.gnu.org/licenses/gpl.html
"""
import copy
import math
# to avoid duplicating code, I'm importing everything from Point1
from Point1 import *
def distance_between_points(p1, p2):
"""Computes the distance between two Point objects."""
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).
"""
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',
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 box.corner.x
print box.corner.y
if __name__ == '__main__':
main()