#! /usr/bin/env python # def lines_exp_int_2d ( p1, p2, q1, q2 ): #*****************************************************************************80 # ## LINES_EXP_INT_2D determines where two explicit lines intersect in 2D. # # Discussion: # # The explicit form of a line in 2D is: # # the line through the points P1, P2. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 25 October 2015 # # Author: # # John Burkardt # # Parameters: # # Input, real P1(2,1), P2(2,1), two points on the first line. # # Input, real Q1(2,1), Q2(2,1), two points on the second line. # # Output, integer IVAL, reports on the intersection: # 0, no intersection, the lines may be parallel or degenerate. # 1, one intersection point, returned in P. # 2, infinitely many intersections, the lines are identical. # # Output, real P(2,1), if IVAl = 1, P is # the intersection point. Otherwise, P = 0. # import numpy as np from line_exp2imp_2d import line_exp2imp_2d from lines_imp_int_2d import lines_imp_int_2d ival = False p = np.zeros ( 2 ) # # Check whether either line is a point. # if ( p1[0] == p2[0] and p1[1] == p2[1] ): point_1 = True else: point_1 = False if ( q1[0] == q2[0] and q1[1] == q2[1] ): point_2 = True else: point_2 = False # # Convert the lines to ABC format. # if ( not point_1 ): a1, b1, c1 = line_exp2imp_2d ( p1, p2 ) if ( not point_2 ): a2, b2, c2 = line_exp2imp_2d ( q1, q2 ) # # Search for intersection of the lines. # if ( point_1 and point_2 ): if ( p1[0] == q1[0] and p1[1] == q1[1] ): ival = True p[0] = p1[0] p[1] = p1[1] elif ( point_1 ): if ( a2 * p1[0] + b2 * p1[1] == c2 ): ival = True p[0] = p1[0] p[1] = p1[1] elif ( point_2 ): if ( a1 * q1[0] + b1 * q1[1] == c1 ): ival = True p[0] = q1[0] p[1] = q1[1] else: ival, p = lines_imp_int_2d ( a1, b1, c1, a2, b2, c2 ) return ival, p