% segment1_num.m - numerical resolution %{ Assigned two points in the real plane, P1, P2; we want to establish if a third point Pj belongs, or not, to the segment between P1 and P2. %} clear; clc; %close all %% line and segment between these two points P1=[1 1]'; P2=[3 2]'; %% parametric equation of the line through P1 and P2 (of the segment if lambda in [0,1]) % Symbolic equation is used only to draw the segment and the line. syms lambda real P = P1 + lambda*(P2 - P1); % parametric vector eq of the line d = (P2 - P1); % director vector %% Other points P3=[2 1]'; P4=[5 3]'; P5=[2.5 1.75]'; %% Display segment and points figure(1); clf h=ezplot(P(1),P(2),[-1 3]); set(h,'Color','r','LineStyle','--') axis([0 6 0 4]) axis equal; hold on; grid on; box on h=ezplot(P(1),P(2),[0 1]); set(h,'Color','r','LineWidth',3) plot([P1(1) P2(1)],[P1(2) P2(2)],'Color','r','Marker','.','MarkerSize',24) HT=text(P1(1),P1(2),' P_1','FontSize',14,'Color','k','FontWeight','Bold','VerticalAlignment','bottom','HorizontalAlignment','right'); HT=text(P2(1),P2(2),' P_2','FontSize',14,'Color','k','FontWeight','Bold','VerticalAlignment','bottom','HorizontalAlignment','right'); title('Segment between P_1 and P_2','FontSize',14) h=plot(P3(1),P3(2),'om','MarkerSize',10,'MarkerFaceColor','m'); HT=text(P3(1),P3(2),' P_3','FontSize',14,'Color','m','FontWeight','Bold','VerticalAlignment','top'); h=plot(P4(1),P4(2),'og','MarkerSize',10,'MarkerFaceColor','g'); HT=text(P4(1),P4(2),' P_4','FontSize',14,'Color','g','FontWeight','Bold','VerticalAlignment','top'); h=plot(P5(1),P5(2),'ob','MarkerSize',10,'MarkerFaceColor','b'); HT=text(P5(1),P5(2),' P_5','FontSize',14,'Color','b','FontWeight','Bold','VerticalAlignment','top'); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% P3 is not on the line: 2) without solve(), as a linear system S=(P3-P1)./d line = S(1) == S(2) % check for point on the line segm = S(1) > 0 & S(1) < 1 % if on the line, check for point on the segment if line && segm % if on the line, check for point on the segment disp(['P3 = (' num2str(P3') ') belongs to the segment.']); disp('') else disp(['P3 = (' num2str(P3') ') does not belong to the segment,']) if line fprintf('\tbut it lies on the line.\n\n') else fprintf('\tand it does not lie on the line neither.\n\n') end end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% P4 on the line, but outside the segment: 2) without solve(), as a linear system S=(P4-P1)./d line = S(1) == S(2) % check for point on the line segm = S(1) > 0 & S(1) < 1 % if on the line, check for point on the segment if line && segm % if on the line, check for point on the segment disp(['P4 = (' num2str(P4') ') belongs to the segment.']); disp('') else disp(['P4 = (' num2str(P4') ') does not belong to the segment,']) if line fprintf('\tbut it lies on the line.\n\n') else fprintf('\tand it does not lie on the line neither.\n\n') end end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% P5 on the segment: 2) without solve(), as a linear system S=(P5-P1)./d line = S(1) == S(2) % check for point on the line segm = S(1) > 0 & S(1) < 1 % if on the line, check for point on the segment if line && segm % if on the line, check for point on the segment disp(['P5 = (' num2str(P5') ') belongs to the segment.']); disp('') else disp(['P5 = (' num2str(P5') ') does not belong to the segment,']) if line fprintf('\tbut it lies on the line.\n\n') else fprintf('\tand it does not lie on the line neither.\n\n') end end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%