% segment2_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 rng('default') % Set the random number generator to the default seed(0) P1=rand(2,1); P2=2*rand(2,1); %% 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 0]'; % not on the line P4=P1 + pi/2*(P2 - P1); % P4 on the line, but outside of the segment P5=P1 + pi/4*(P2 - P1); % P5 on the line, and inside the segment %% Display segment and points figure(2); clf h=ezplot(P(1),P(2),[-2 3]); set(h,'Color','r','LineStyle','--') axis([-1.5 3 -1 3]) 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','top','HorizontalAlignment','right'); HT=text(P2(1),P2(2),' P_2','FontSize',14,'Color','k','FontWeight','Bold','VerticalAlignment','top','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','bottom'); 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','HorizontalAlignment','right'); 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','bottom'); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% 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 of the segment: 2) without solve(), as a linear system S=(P4-P1)./d %line = S(1) == S(2) % THIS CODE DOES NOT WORK IN GENERAL! line = abs(S(1) - S(2)) < 1e-8 % 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) % THIS CODE DOES NOT WORK IN GENERAL! line = abs(S(1) - S(2)) < 1e-8 % 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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%