function [x_i,y_i] = line_intersect_a(m1,b1,m2,b2) % function [x_i,y_i] = line_intersect_a(m1,b1,m2,b2) % 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 % NaN ("Not a Number") for x and y, and no plot % is produced % Input parameters % m1 = slope of line 1 % b1 = y-intercept of line 1 % m2 = slope of line 2 % b2 = y-intercept of line 2 % % The equations of the lines are % y = m1.*x + b1 % y = m2.*x + b2 % Example % [x_i,y_i] = line_intersect_a(1,1,2,2) % Name: Steve Martel % Date: 9/15/04 % Check to see if lines intersect. % The lines do not intersect if the slopes are the same. if m1 == m2 % The lines are parallel and do not intersect x = NaN; y = NaN; return else % The lines are not parallel and therefore intersect % Find the coordinates of the intersection x_i = (b2 - b1)./(m1 - m2); y_i = m1.*x_i + b1; % 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 = -b1./m1; x_int_2 = -b2./m2; figure(1) clf subplot(1,2,1) plot([x_int_1 0],[0 b1],'b',[x_int_2 0],[0 b2],'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') end