Skip to content

Commit 924b445

Browse files
committed
fixed ruff issues
1 parent f6348d7 commit 924b445

File tree

1 file changed

+36
-14
lines changed

1 file changed

+36
-14
lines changed

maths/line_intersection.py

+36-14
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,38 @@
11
# Function to calculate determinant of a 2x2 matrix
2-
def determinant(a: float, b: float, c: float, d: float) -> float:
2+
def determinant(m00: float, m01: float, m10: float, m11: float) -> float:
33
"""
44
Calculates the determinant of a 2x2 matrix:
55
6-
| a b |
7-
| c d |
6+
| m00 m01 |
7+
| m10 m11 |
88
99
Args:
10-
a, b, c, d (float): Elements of the 2x2 matrix.
10+
m00 (float): Element in the first row, first column.
11+
m01 (float): Element in the first row, second column.
12+
m10 (float): Element in the second row, first column.
13+
m11 (float): Element in the second row, second column.
1114
1215
Returns:
1316
float: The determinant of the matrix.
17+
18+
Examples:
19+
# Determinant of the identity matrix (should be 1)
20+
>>> determinant(1, 0, 0, 1)
21+
1
22+
23+
# Determinant of a matrix with two equal rows (should be 0)
24+
>>> determinant(1, 2, 1, 2)
25+
0
26+
27+
# Determinant of a matrix with a negative determinant
28+
>>> determinant(1, 2, 3, 4)
29+
-2
30+
31+
# Determinant of a matrix with larger numbers
32+
>>> determinant(10, 20, 30, 40)
33+
-200
1434
"""
15-
return a * d - c * b
35+
return m00 * m11 - m10 * m01
1636

1737

1838
# Function to compute the line equation coefficients from two points
@@ -35,15 +55,15 @@ def line_coefficients(p1: list[float] | tuple, p2: list[float] | tuple) -> tuple
3555
3656
# Horizontal line (y = constant)
3757
>>> line_coefficients([0, 1], [2, 1])
38-
(0.0, -1, 1)
58+
(0.0, -1, 1.0)
3959
4060
# Diagonal line (positive slope)
4161
>>> line_coefficients([0, 0], [1, 1])
42-
(1.0, -1, 0)
62+
(1.0, -1, 0.0)
4363
4464
# Diagonal line (negative slope)
4565
>>> line_coefficients([0, 1], [1, 0])
46-
(-1.0, -1, 1)
66+
(-1.0, -1, 1.0)
4767
"""
4868

4969
if p1[0] == p2[0]: # Vertical line
@@ -70,11 +90,13 @@ def segment_intersection(
7090
v2 (List[float] | tuple): Second point of the first segment (x, y).
7191
v1_prime (List[float] | tuple): First point of the second segment (x, y).
7292
v2_prime (List[float] | tuple): Second point of the second segment (x, y).
73-
as_segments (bool): treat the inputs as line segments (True) or as infinite lines (False).
93+
as_segments (bool):
94+
treat the inputs as line segments (True)
95+
or as infinite lines (False).
7496
7597
Returns:
7698
List[float] | None:
77-
Returns the intersection point [x, y] if the segments/lines intersect, otherwise None.
99+
Returns the intersection point [x, y] if existent, otherwise None.
78100
79101
References:
80102
Cramer's rule: https://en.wikipedia.org/wiki/Cramer%27s_rule
@@ -91,13 +113,13 @@ def segment_intersection(
91113
>>> segment_intersection([0, 0], [0, 1], [1, 0], [1, 1]) is None
92114
True
93115
94-
# Parallel infinite lines (ignoring segment boundaries)
95-
>>> segment_intersection([0, 0], [1, 1], [2, 2], [3, 3], as_segments=False) is None
96-
True
97-
98116
# Intersecting infinite lines
99117
>>> segment_intersection([0, 0], [1, 1], [1, 0], [0, 1], as_segments=False)
100118
[0.5, 0.5]
119+
120+
# Parallel infinite lines (ignoring segment boundaries)
121+
>>> segment_intersection([0, 0], [1, 1], [2, 2], [3, 3], False) is None
122+
True
101123
"""
102124

103125
# Compute line coefficients for the two segments/lines

0 commit comments

Comments
 (0)