Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
| Download

📚 The CoCalc Library - books, templates and other resources

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