-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdistance.py
More file actions
67 lines (54 loc) · 1.71 KB
/
distance.py
File metadata and controls
67 lines (54 loc) · 1.71 KB
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
"""
File for computing the minimal distance between points
"""
import math
def compute_distance(point1, point2):
"""
Compute the distance between two points.
The points should be given as a tuple with x and y co-ordinates.
The function returns the distance which is computed using Pythagoras.
"""
return math.sqrt((point1[0] - point2[0]) ** 2 +
(point1[1] - point2[1]) **2)
def compute_minimum_distance(points):
"""
Compute the minimum distance between several points.
Given a list of points, compute the distance between all pairs of points
and return the minimum of these distances.
"""
distances = []
for i in range(len(points)):
for j in range(len(points)):
if i == j:
continue
distance = compute_distance(points[i],
points[j])
distances.append(distance)
return min(distances)
"""
Can also phrase this as:
distances = []
for point1 in points:
for point2 in points:
if point1 == point2:
continue
distance = compute_distance(points1, points2)
distances.append(distance)
return min(distances)
"""
"""
Can also phrase this as:
distances = [compute_distance(point1),(point2)
for point1 in points
for point2 in points
if point1 != points2]
return min(distances)
"""
"""
Can also phrase this as:
distances = []
return min(compute_distance(point1, point2)
for point1 in points
for point2 in points
if point1 != points2)
"""