-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Expand file tree
/
Copy pathdistance_between_two_points.py
More file actions
37 lines (27 loc) · 955 Bytes
/
distance_between_two_points.py
File metadata and controls
37 lines (27 loc) · 955 Bytes
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
"""
Distance Between Two Points in 2D Space
Calculate the Euclidean distance between two points using the distance
formula derived from the Pythagorean theorem.
Reference: https://en.wikipedia.org/wiki/Euclidean_distance
Complexity:
Time: O(1)
Space: O(1)
"""
from __future__ import annotations
from math import sqrt
def distance_between_two_points(x1: float, y1: float, x2: float, y2: float) -> float:
"""Calculate the Euclidean distance between two points in 2D space.
Args:
x1: x-coordinate of the first point.
y1: y-coordinate of the first point.
x2: x-coordinate of the second point.
y2: y-coordinate of the second point.
Returns:
The Euclidean distance between the two points.
Examples:
>>> distance_between_two_points(0, 0, 3, 4)
5.0
>>> distance_between_two_points(-1, -1, 2, 3)
5.0
"""
return sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)