1
1
# 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 :
3
3
"""
4
4
Calculates the determinant of a 2x2 matrix:
5
5
6
- | a b |
7
- | c d |
6
+ | m00 m01 |
7
+ | m10 m11 |
8
8
9
9
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.
11
14
12
15
Returns:
13
16
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
14
34
"""
15
- return a * d - c * b
35
+ return m00 * m11 - m10 * m01
16
36
17
37
18
38
# 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
35
55
36
56
# Horizontal line (y = constant)
37
57
>>> line_coefficients([0, 1], [2, 1])
38
- (0.0, -1, 1)
58
+ (0.0, -1, 1.0 )
39
59
40
60
# Diagonal line (positive slope)
41
61
>>> line_coefficients([0, 0], [1, 1])
42
- (1.0, -1, 0)
62
+ (1.0, -1, 0.0 )
43
63
44
64
# Diagonal line (negative slope)
45
65
>>> line_coefficients([0, 1], [1, 0])
46
- (-1.0, -1, 1)
66
+ (-1.0, -1, 1.0 )
47
67
"""
48
68
49
69
if p1 [0 ] == p2 [0 ]: # Vertical line
@@ -70,11 +90,13 @@ def segment_intersection(
70
90
v2 (List[float] | tuple): Second point of the first segment (x, y).
71
91
v1_prime (List[float] | tuple): First point of the second segment (x, y).
72
92
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).
74
96
75
97
Returns:
76
98
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.
78
100
79
101
References:
80
102
Cramer's rule: https://en.wikipedia.org/wiki/Cramer%27s_rule
@@ -91,13 +113,13 @@ def segment_intersection(
91
113
>>> segment_intersection([0, 0], [0, 1], [1, 0], [1, 1]) is None
92
114
True
93
115
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
-
98
116
# Intersecting infinite lines
99
117
>>> segment_intersection([0, 0], [1, 1], [1, 0], [0, 1], as_segments=False)
100
118
[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
101
123
"""
102
124
103
125
# Compute line coefficients for the two segments/lines
0 commit comments