10 examples of 'python distance between two points' in Python

Every line of 'python distance between two points' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
53def _py_distance(point1, point2):
54 '''
55 Calculating great-circle distance
56 (see https://en.wikipedia.org/wiki/Great-circle_distance)
57 '''
58 lon1, lat1 = (radians(coord) for coord in point1)
59 lon2, lat2 = (radians(coord) for coord in point2)
60
61 dlon = fabs(lon1 - lon2)
62 dlat = fabs(lat1 - lat2)
63
64 numerator = sqrt(
65 (cos(lat2)*sin(dlon))**2 +
66 ((cos(lat1)*sin(lat2)) - (sin(lat1)*cos(lat2)*cos(dlon)))**2)
67
68 denominator = (
69 (sin(lat1)*sin(lat2)) +
70 (cos(lat1)*cos(lat2)*cos(dlon)))
71
72 c = atan2(numerator, denominator)
73 return EARTH_MEAN_RADIUS*c
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

46def distance(x1, y1, x2, y2):
47 """Get the distance between two points."""
48 return ((x2 - x1) ** 2 + (y2 - y1) ** 2) ** 0.5
203def distances(points1, points2):
204 # Much faster than any manual calculations
205 return scipy.spatial.distance.cdist(points1, points2, metric='euclidean')
5def angle_of_two_points(p1, p2):
6 return degrees(atan2(p2[1] - p1[1], p2[0] - p1[0]))
60def distance(x1, y1, x2, y2):
61 """
62 l2 distance
63 """
64
65 return math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2))
10def distance(x1, y1, x2, y2):
11 """Return the distance between the points (x1, y1) and (x2, y2)"""
12 t1 = (x1 - x2)
13 t2 = (y1 - y2)
14 return math.sqrt(t1 * t1 + t2 * t2)
9def Distance(p1,p2):
10 dx = p2[0] - p1[0]
11 dy = p2[1] - p1[1]
12 return math.sqrt(dx*dx+dy*dy)
37def distance(p1, p2):
38 return math.sqrt(quadrance(p1, p2))
7def distance (p1,p2):
8 return (math.sqrt((p2[1]-p1[1])**2 + (p2[0]-p1[0])**2))
48def test_different_points_1(self):
49 assert math.isclose(distance(1, 1, 2, 2), math.sqrt(2))

Related snippets