Introduction It is possible to compute the intersection of two lines and the line given two points using cross-product.
In this blog post, I would like to quickly derive how to do so using homogeneous coordinate representations.
2D Point Representations Inhomogeneous Coordinates The inhomogeneous coordinates for a 2D point are just ordinary two-value Cartesian coordinates.
Augmented Coordinates The augmented coordinates for a 2D point are just the 2D inhomogeneous coordinates with an additional constant .
Homogeneous Coordinates The homogeneous coordinates are just the augmented coordinates scaled by some value .
where .
When , is called ideal point and do not have the corresponding inhomogeneous coordinates.
Intersection The 2D line could be represented using homogeneous coordinates, . It can also be normalized so that with .
Suppose is the intersection of two lines and , we must have
Because the cross product of and , , is perpendicular to both and , i.e.,
We must have
for some .
Therefore,
line_intersection.py
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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 from typing import Tuple , Optional import numpy as npdef get_2d_line_intersection ( line_1: Tuple [float , float , float ], line_2: Tuple [float , float , float ] ) -> Optional [Tuple [float , float ]]: """Get the 2D line intersection. Args: line_1 (Tuple[float, float, float]): Homogenous coordinate representation of a 2D line. line_2 (Tuple[float, float, float]): Homogenous coordinate representation of a 2D line. Returns: Tuple[float, float]: Inhomogeneous coordinate representation of the intersection. """ x_homo = np.cross(line_1, line_2) if x_homo[2 ] == 0 : return None x = x_homo / x_homo[2 ] return (x[0 ], x[1 ]) def verify_2d_line_intersection (line_1: Tuple [float , float , float ], line_2: Tuple [float , float , float ], intersection: Tuple [float , float ] ) -> bool : status = np.isclose( line_1[0 ] * intersection[0 ] + line_1[1 ] * intersection[1 ] + line_1[2 ], 0 ) and np.isclose( line_2[0 ] * intersection[0 ] + line_2[1 ] * intersection[1 ] + line_2[2 ], 0 ) return status if __name__ == "__main__" : np.random.seed(0 ) line_1 = np.random.rand(3 ) line_2 = np.random.rand(3 ) intersection = get_2d_line_intersection(line_1=line_1, line_2=line_2) print (intersection) if intersection is not None : status = verify_2d_line_intersection(line_1=line_1, line_2=line_2, intersection=intersection) assert status == True
2D Line from 2D Points Suppose the line passes two points and , similar to the intersection calculation, we must have
Because the cross product of and , , is perpendicular to both and , i.e.,
We must have
If the two points were represented using homogeneous coordinates, equivalently,
line_representation.py
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 38 39 40 41 42 43 44 45 46 47 48 from typing import Tuple , Optional import numpy as npdef get_2d_line ( point_1: Tuple [float , float ], point_2: Tuple [float , float ] ) -> Optional [Tuple [float , float , float ]]: """Get the 2D line. Args: point_1 (Tuple[float, float, float]): Inhomogeneous coordinate representation of a 2D point. point_2 (Tuple[float, float, float]): Inhomogeneous coordinate representation of a 2D point. Returns: Tuple[float, float]: Homogeneous coordinate representation of the 2D line. """ point_1_homo = (point_1[0 ], point_1[1 ], 1 ) point_2_homo = (point_2[0 ], point_2[1 ], 1 ) if point_1_homo == point_2_homo: return None line = np.cross(point_1_homo, point_2_homo) return (line[0 ], line[1 ], line[2 ]) def verify_2d_line_point (line: Tuple [float , float , float ], point: Tuple [float , float ] ) -> bool : status = np.isclose(line[0 ] * point[0 ] + line[1 ] * point[1 ] + line[2 ], 0 ) return status if __name__ == "__main__" : np.random.seed(0 ) point_1 = np.random.rand(2 ) point_2 = np.random.rand(2 ) line = get_2d_line(point_1=point_1, point_2=point_2) print (line) if line is not None : status = verify_2d_line_point(line=line, point=point_1) assert status == True status = verify_2d_line_point(line=line, point=point_2) assert status == True
References