One way to find the shortest distance between a series of surface (x, y) points is to let Python module itertools find the non-repeating combinations of point pairs. Now you can apply the Pythagoras' theorem to find all the distances and the minimum distance.

```
''' distance_shortest101.py
use Python module itertools to get non-repeating combinations
of two points on a surface
calculate the shortest distance between the given surface points
tested with Python27 and Python33 by vegaseat 30oct2013
'''
import itertools as it
import pprint
def distance_points(two_point_list):
'''
calculate distance between two points from the list
of two point tuples
return a list of (distance, (point1, point2)) tuples
'''
distance_p_list = []
for p in two_point_list:
#print(p) # test
px1 = p[0][0]
px2 = p[1][0]
py1 = p[0][1]
py2 = p[1][1]
#print(px1, py1, px2, py2) # test
# use Pythagoras' theorem
distance = ((px1-px2)**2 + (py1-py2)**2)**0.5
distance_p_list.append((distance, p))
return distance_p_list
# create a series of surface (x, y) points
point_list = [
(1, 2),
(3, 5),
(4, 6),
(1.5, 7)
]
# make sets of 2 non-repeating points
two_point_list = list(it.combinations(point_list, 2))
print("Non-repeating point combinations:")
pprint.pprint(two_point_list)
print('-'*40)
distance_p_list = distance_points(two_point_list)
print("List of (distance, (point1, point2)) tuples:")
pprint.pprint(distance_p_list)
print('-'*40)
shortest_distance = min(distance_p_list)
# show result
sf = "The shortest distance is {:f} between points {}"
print(sf.format(shortest_distance[0],shortest_distance[1]))
''' result ...
Non-repeating point combinations:
[((1, 2), (3, 5)),
((1, 2), (4, 6)),
((1, 2), (1.5, 7)),
((3, 5), (4, 6)),
((3, 5), (1.5, 7)),
((4, 6), (1.5, 7))]
----------------------------------------
List of (distance, (point1, point2)) tuples:
[(3.605551275463989, ((1, 2), (3, 5))),
(5.0, ((1, 2), (4, 6))),
(5.024937810560445, ((1, 2), (1.5, 7))),
(1.4142135623730951, ((3, 5), (4, 6))),
(2.5, ((3, 5), (1.5, 7))),
(2.692582403567252, ((4, 6), (1.5, 7)))]
----------------------------------------
The shortest distance is 1.414214 between points ((3, 5), (4, 6))
'''
```

Gribouillis
1,391
Programming Explorer
Team Colleague

gerisam
0
Newbie Poster

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.