Skip to content

Commit d1cbb01

Browse files
fix: use descriptive parameter names
1 parent ca948ee commit d1cbb01

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

geometry/segment_intersection.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ class Point(NamedTuple):
2727
y: float
2828

2929

30-
def direction(a: Point, b: Point, c: Point) -> float:
31-
"""Return the cross product of vectors (a→c) and (a→b).
30+
def direction(pivot: Point, target: Point, query: Point) -> float:
31+
"""Return the cross product of vectors (pivot->query) and (pivot->target).
3232
3333
The sign of the result encodes the orientation of the ordered triple
34-
(a, b, c):
35-
- Negative counter-clockwise (left turn)
36-
- Positive clockwise (right turn)
37-
- Zero collinear
34+
(pivot, target, query):
35+
- Negative -> counter-clockwise (left turn)
36+
- Positive -> clockwise (right turn)
37+
- Zero -> collinear
3838
3939
>>> direction(Point(0, 0), Point(1, 0), Point(0, 1))
4040
-1
@@ -43,11 +43,13 @@ def direction(a: Point, b: Point, c: Point) -> float:
4343
>>> direction(Point(0, 0), Point(1, 1), Point(2, 2))
4444
0
4545
"""
46-
return (c.x - a.x) * (b.y - a.y) - (b.x - a.x) * (c.y - a.y)
46+
return (query.x - pivot.x) * (target.y - pivot.y) - (target.x - pivot.x) * (
47+
query.y - pivot.y
48+
)
4749

4850

49-
def on_segment(a: Point, b: Point, p: Point) -> bool:
50-
"""Check whether point *p*, known to be collinear with segment ab, lies on it.
51+
def on_segment(seg_start: Point, seg_end: Point, point: Point) -> bool:
52+
"""Check whether *point*, known to be collinear with the segment, lies on it.
5153
5254
>>> on_segment(Point(0, 0), Point(4, 4), Point(2, 2))
5355
True
@@ -56,9 +58,9 @@ def on_segment(a: Point, b: Point, p: Point) -> bool:
5658
>>> on_segment(Point(0, 0), Point(4, 0), Point(2, 0))
5759
True
5860
"""
59-
return min(a.x, b.x) <= p.x <= max(a.x, b.x) and min(a.y, b.y) <= p.y <= max(
60-
a.y, b.y
61-
)
61+
return min(seg_start.x, seg_end.x) <= point.x <= max(
62+
seg_start.x, seg_end.x
63+
) and min(seg_start.y, seg_end.y) <= point.y <= max(seg_start.y, seg_end.y)
6264

6365

6466
def segments_intersect(p1: Point, p2: Point, p3: Point, p4: Point) -> bool:

0 commit comments

Comments
 (0)