|
28 | 28 | # --------------------------------------------------------------------------- |
29 | 29 |
|
30 | 30 |
|
31 | | -def _euclidean_distance(a: tuple[float, float], b: tuple[float, float]) -> float: |
| 31 | +def _euclidean_distance( |
| 32 | + point_1: tuple[float, float], point_2: tuple[float, float] |
| 33 | +) -> float: |
32 | 34 | """Return the Euclidean distance between two 2-D points. |
33 | 35 |
|
34 | 36 | >>> _euclidean_distance((0.0, 0.0), (3.0, 4.0)) |
35 | 37 | 5.0 |
36 | 38 | >>> _euclidean_distance((1.0, 1.0), (1.0, 1.0)) |
37 | 39 | 0.0 |
38 | 40 | """ |
39 | | - return math.hypot(b[0] - a[0], b[1] - a[1]) |
| 41 | + return math.hypot(point_2[0] - point_1[0], point_2[1] - point_1[1]) |
40 | 42 |
|
41 | 43 |
|
42 | 44 | def _perpendicular_distance( |
43 | | - p: tuple[float, float], |
44 | | - a: tuple[float, float], |
45 | | - b: tuple[float, float], |
| 45 | + point: tuple[float, float], |
| 46 | + line_start: tuple[float, float], |
| 47 | + line_end: tuple[float, float], |
46 | 48 | ) -> float: |
47 | | - """Return the perpendicular distance from point *p* to the line through *a* and *b*. |
| 49 | + """Return the perpendicular distance from *point* to the line through |
| 50 | + *line_start* and *line_end*. |
48 | 51 |
|
49 | | - The result is the absolute value of the signed area of the triangle (a, b, p) |
50 | | - divided by the length of segment ab, which equals the altitude of that triangle |
51 | | - from p. |
| 52 | + The result is the absolute value of the signed area of the triangle |
| 53 | + (line_start, line_end, point) divided by the length of the segment, which |
| 54 | + equals the altitude of that triangle from point. |
52 | 55 |
|
53 | 56 | >>> _perpendicular_distance((4.0, 0.0), (0.0, 0.0), (0.0, 3.0)) |
54 | 57 | 4.0 |
55 | | - >>> _perpendicular_distance((4.0, 0.0), (0.0, 0.0), (0.0, 3.0)) |
56 | | - 4.0 |
57 | | - >>> # order of a and b does not affect the result |
| 58 | + >>> # order of line_start and line_end does not affect the result |
58 | 59 | >>> _perpendicular_distance((4.0, 0.0), (0.0, 3.0), (0.0, 0.0)) |
59 | 60 | 4.0 |
60 | 61 | >>> _perpendicular_distance((4.0, 1.0), (0.0, 1.0), (0.0, 4.0)) |
61 | 62 | 4.0 |
62 | 63 | >>> _perpendicular_distance((2.0, 1.0), (-2.0, 1.0), (-2.0, 4.0)) |
63 | 64 | 4.0 |
64 | 65 | """ |
65 | | - px, py = p |
66 | | - ax, ay = a |
67 | | - bx, by = b |
| 66 | + px, py = point |
| 67 | + ax, ay = line_start |
| 68 | + bx, by = line_end |
68 | 69 | numerator = abs((by - ay) * px - (bx - ax) * py + bx * ay - by * ax) |
69 | | - denominator = _euclidean_distance(a, b) |
| 70 | + denominator = _euclidean_distance(line_start, line_end) |
70 | 71 | if denominator == 0.0: |
71 | | - # a and b coincide; fall back to point-to-point distance |
72 | | - return _euclidean_distance(p, a) |
| 72 | + # line_start and line_end coincide; fall back to point-to-point distance |
| 73 | + return _euclidean_distance(point, line_start) |
73 | 74 | return numerator / denominator |
74 | 75 |
|
75 | 76 |
|
|
0 commit comments