#! /usr/bin/env python # def lines_imp_int_2d ( a1, b1, c1, a2, b2, c2 ): #*****************************************************************************80 # ## LINES_IMP_INT_2D determines where two implicit lines intersect in 2D. # # Discussion: # # The implicit form of a line in 2D is: # # A * X + B * Y + C = 0 # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 04 December 2010 # # Author: # # John Burkardt # # Parameters: # # Input, real A1, B1, C1, define the first line. # At least one of A1 and B1 must be nonzero. # # Input, real A2, B2, C2, define the second line. # At least one of A2 and B2 must be nonzero. # # Output, integer IVAL, reports on the intersection. # -1, both A1 and B1 were zero. # -2, both A2 and B2 were zero. # 0, no intersection, the lines are parallel. # 1, one intersection point, returned in X, Y. # 2, infinitely many intersections, the lines are identical. # # Output, real P(2,1), if IVAL = 1, then P is # the intersection point. if IVAL = 2, then P is one of the # points of intersection. Otherwise, P = []. # import numpy as np from r8mat_solve import r8mat_solve p = np.zeros ( 2 ) # # Refuse to handle degenerate lines. # if ( a1 == 0.0 and b1 == 0.0 ): ival = -1 return ival, p elif ( a2 == 0.0 and b2 == 0.0 ): ival = -2 return ival, p # # Set up and solve a linear system. # a = np.zeros ( [ 2, 3 ] ) a[0,0] = a1 a[0,1] = b1 a[0,2] = - c1 a[1,0] = a2 a[1,1] = b2 a[1,2] = - c2 a, info = r8mat_solve ( 2, 1, a ) # # If the inverse exists, then the lines intersect at the solution point. # if ( info == 0 ): ival = 1; p[0] = a[0,2] p[1] = a[1,2] # # If the inverse does not exist, then the lines are parallel # or coincident. Check for parallelism by seeing if the # C entries are in the same ratio as the A or B entries. # else: ival = 0 if ( a1 == 0.0 ): if ( b2 * c1 == c2 * b1 ): ival = 2 p[0] = 0.0 p[1] = - c1 / b1 else: if ( a2 * c1 == c2 * a1 ): ival = 2 if ( abs ( a1 ) < abs ( b1 ) ): p[0] = 0.0 p[1] = - c1 / b1 else: p[0] = - c1 / a1 p[1] = 0.0 return ival, p