% code
%%%%Determine point of intersection between perpendicular lines(from curve
%%%%2) and curve 1%%%%%%%%%
clear all; clc;Y= [318 318 317 317 317 317 317 318 318 319 320 320 320 321 321 321 320 320 320 320 318 318 318 318 318 318];X = [322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347];Y1 = [338 338 338 338 338 338 339 339 339 339 339 339 339 339 339 339 339 339 340 340 340 340 340 340 340 340]; for i=1:length(X) X0 = X(i); %%%%columnwise%%%%%%
n = 100; % 100 or some higher number for nice curves
f = 0.1; % this defines the length of the tangent
x = linspace(min(X),max(X),n); y2 = interp1(X,Y1,x,'spline');%%%%for smooth curve 1
y = interp1(X,Y,x,'spline'); %%%%for smooth curve 2
xt = X0+(f*(max(X)-min(X))*[-1 0 1]);%%%x coordinates of tangent
m = diff(y)./diff(x);%%%%calculate the slope of the tangent
m = [m, m(end)]; % so just add one at the end
k = min([find(x >= X0,1,'first'), length(x)]);yt = y(k) + m(k)*(0.1*(max(X)-min(X))*[-1 0 1]);%%%%y coordinates of tangent
%%%%%%%plot normal to the tangent%%%%%5
A=[xt(:,1),yt(:,1)];B=[xt(:,3),yt(:,3)];null(A-B);xa = [A(:,1), B(:,1)];ya = [A(:,2),B(:,2)];gg=1.5*(max(Y1)-min(Y));% this defines the length of the normal
normal = [mean(xa),mean(ya)] + gg*null(A-B)';line([mean(xa),normal(1)],[mean(ya),normal(2)],'color','r','LineWidth',2);%%%Determine point of intersection of perpendicular lines (from curve2) and curve 1
v1=[mean(xa),normal(1,1)];v2=[mean(ya),normal(1,2)];%%%Points that represents perpedicular lines
[x0,y0,iout,jout] = intersections(v1,v2,x,y2);plot(X,Y,'ro', x,y,'b',xt,yt,'--g',x,y2,'b'); hold on;plot(x0,y0,'bo') end %%%%%%%%Main aim is to determine max & min distance between two curves.
MATLAB: The code(given) only store last points of intersection.How to store all points of intersection
point of intersection between lines and a curve
Best Answer