% segment1_sym.m - symbolic 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]) syms lambda real P = P1 + lambda*(P2 - P1); % parametric vector eq of the line %% Other points P3=[2 1]'; % P3 not on the line P4=[5 3]'; % P4 on the line, but outside the segment P5=[2.5 1.75]'; % P5 on the line, and inside the segment %% 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 not on the line: 1) with symbolic solve() S=solve(P-P3 == 0,lambda) if ~isempty(S) double(S) > 0 & double(S) < 1 if double(S) > 0 & double(S) < 1 disp('P3 belongs to the segment between P1 and P2.') else disp('P3 lies on the line passing through P1 and P2,') disp(' but it is outside the segment.') end else disp('P3 is not on the line passing through P1 and P2.') end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% P4 on the line, but outside of the segment: 1) with symbolic solve() S=solve(P-P4 == 0,lambda) if ~isempty(S) double(S) > 0 & double(S) < 1 if double(S) > 0 & double(S) < 1 disp('P4 belongs to the segment between P1 and P2.') else disp('P4 lies on the line passing through P1 and P2,') disp(' but it is outside the segment.') end else disp('P4 is not on the line passing through P1 and P2.') end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% P5 on the segment: 1) with symbolic solve() S=solve(P-P5 == 0,lambda) if ~isempty(S) double(S) > 0 & double(S) < 1 if double(S) > 0 & double(S) < 1 disp('P5 belongs to the segment between P1 and P2.') else disp('P5 lies on the line passing through P1 and P2,') disp(' but it is outside the segment.') end else disp('P5 is not on the line passing through P1 and P2.') end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%