function [x_i,y_i] = line_intersect_c(A1,B1,C1,A2,B2,C2) % function [x_i,y_i] = line_intersect_c(A1,B1,C1,A2,B2,C2) % This function checks to see if two lines, % both in the plane z = 0, intersect. % If they do, it returns the coordinates of the % point of intersection and plots the point. % If the lines don't intersect, it returns values of % Inf (infinity) for x and y, and no plot is produced. % Input parameters % A1, B1, C1 = coefficients of equation for line 1 % (A1)(x) + (B1)(y) = C1 % A2, B2, C2 = coefficients of equation for line 2 % (A2)(x) + (B2)(y) = C2 % The equations of the lines are % A1.*x + B1.*y = C1 % A2.*x + B2.*y = C2 % or, in matrix form [A][X] = [B] % |A1 B1| |x| = |C1| % |A2 B2| |y| = |C2| % Example % [x_i,y_i] = line_intersect_c(1,2,3,4,5,6) % Name: Steve Martel % Date: 9/15/04 % Set up matrices for simultaneous linear equations A = [A1 B1;A2 B2]; B = [C1;C2] % Find the solution for the point of intersection [X] X = A\B; x_i = X(1); y_i = X(2); % Plot the intersection, extending the lines a bit from that point % Prepare two subplots, the first showing the x- and y- intercepts % of the lines, the second showing their intersection x_int_1 = C1./A1; x_int_2 = C2./A2; y_int_1 = C1./B1; y_int_2 = C2./B2; figure(1) clf subplot(1,2,1) plot([x_int_1 0],[0 y_int_1],'b',[x_int_2 0],[0 y_int_2],'r') title (['Lines intersect at x = ',num2str(x_i),' y = ',num2str(y_i)]) xlabel('x') ylabel('y') subplot(1,2,2) plot(x_i,y_i,'+') title (['Lines intersect at x = ',num2str(x_i),' y = ',num2str(y_i)]) xlabel('x') ylabel('y')