Intersection between two curved lines

조회 수: 18 (최근 30일)
Róbert Straka
Róbert Straka 2020년 12월 16일
답변: Star Strider 2020년 12월 16일
Hello I need a help with finding intersection point on Y-axis between two curved lines on the picture below.
Below is the code Im runnig to generate the points for these lines, if it helps
%Generovanie bodov noža
clc
rn = 1.3;
f = 0.5;
ap = 0.2;
po = 10;
dx = 0.01;
lambda = 0;
gamma = -8;
Kr = 51;
Kr1 = 51;
xa = (rn*(sin(Kr)))*-1;
xb = rn*(sin(Kr1));
ya = (xa*(tan(Kr)))+(rn/(cos(Kr)))-rn;
%priamka A
N = 92
xpA = xa - (0:N-1)*dx;
xpA = xpA';
xpA = sort(xpA);
xpA = round(xpA,2);
ypA = (xpA*tan(Kr))+(rn/(cos(Kr)))-rn;
ypA1 = sort(ypA);
ypA1 = round(ypA,2);
%priamka B
xpB = xb + (0:N-1)*dx;
xpB = xpB';
xpB = round(xpB,2);
ypB = (-xpB*tan(Kr1))+(rn/(cos(Kr1)))-rn;
ypB = round(ypB,2);
%kruznica
N1 = 500;
xk1 = xa + (1:N1)*dx;
xk1 = round(xk1,2);
xk1 = xk1';
[c,d]= intersect(xk1,min(xpB))
xk = xa +(1:d-1)*dx;
xk = round(xk,2);
xk = xk';
yk = (sqrt((rn^2)-(xk.^2)))-rn;
yk = round(yk,2);
X = [xpA;xk;xpB];
Y = [ypA;yk;ypB];
b = po-ap;
x = X(1);
X1 = X-x;
Y1 = Y+b;
X2 = X1+f;
Y2 = Y1;
plot(X1,Y1)
hold on
plot(X2,Y1)

채택된 답변

Star Strider
Star Strider 2020년 12월 16일
The part of the code after
X = [xpA;xk;xpB];
Y = [ypA;yk;ypB];
changes to:
b = po-ap;
x = X(1);
X1 = X-x;
Y1 = Y+b;
X2 = X1+f;
Y2 = Y1;
[Y1max,Y1ix] = max(Y1);
[Y2max,Y2ix] = max(Y2);
Xi = linspace(min([X1(Y1ix);X2(Y2ix)]), max([X1(Y1ix);X2(Y2ix)]), 20);
Y1i = interp1(X1,Y1,Xi);
Y2i = interp1(X2,Y2,Xi);
Ydif = sort(Y2i-Y1i);
Xq = interp1(Ydif, Xi, 0);
Yq = interp1(Xi, Y1i, Xq);
figure
plot(X1,Y1)
hold on
plot(X2,Y1)
plot(Xq, Yq, 'sg', 'MarkerSize', 10)
hold off
text(Xq, Yq, sprintf('\\uparrow\nX = %.4f\nY = %.4f',Xq,Yq), 'HorizontalAlignment','center', 'VerticalAlignment','top')
producing:
.

추가 답변 (1개)

Image Analyst
Image Analyst 2020년 12월 16일
To find the overlapping point, see the part I added at the end:
% Generovanie bodov noža
clc
rn = 1.3;
f = 0.5;
ap = 0.2;
po = 10;
dx = 0.01;
lambda = 0;
gamma = -8;
Kr = 51;
Kr1 = 51;
xa = (rn*(sin(Kr)))*-1;
xb = rn*(sin(Kr1));
ya = (xa*(tan(Kr)))+(rn/(cos(Kr)))-rn;
% priamka A
N = 92
xpA = xa - (0:N-1)*dx;
xpA = xpA';
xpA = sort(xpA);
xpA = round(xpA,2);
ypA = (xpA*tan(Kr))+(rn/(cos(Kr)))-rn;
ypA1 = sort(ypA);
ypA1 = round(ypA,2);
% priamka B
xpB = xb + (0:N-1)*dx;
xpB = xpB';
xpB = round(xpB,2);
ypB = (-xpB*tan(Kr1))+(rn/(cos(Kr1)))-rn;
ypB = round(ypB,2);
% kruznica
N1 = 500;
xk1 = xa + (1:N1)*dx;
xk1 = round(xk1,2);
xk1 = xk1';
[c,d]= intersect(xk1,min(xpB))
xk = xa +(1:d-1)*dx;
xk = round(xk,2);
xk = xk';
yk = (sqrt((rn^2)-(xk.^2)))-rn;
yk = round(yk,2);
X = [xpA;xk;xpB];
Y = [ypA;yk;ypB];
b = po-ap;
x = X(1);
X1 = X-x;
Y1 = Y+b;
X2 = X1+f;
Y2 = Y1;
plot(X1,Y1)
hold on
plot(X2,Y1)
%-------------------------------------------------------------------------------
% Added by Image Analyst
% Find out where the two curves are closest.
% Use pdist2 to find distance of every point in set 1
% to every point in set 2
distances = pdist2([X1, Y1], [X2, Y1]);
% Make diagonal infinity because we don't want to find distance of points to themselves.
rows = size(distances, 1);
% Find out where the min is
[minValue, minIndex] = min(distances(:))
[row, col] = ind2sub(size(distances), minIndex)
% Row = 204, meaning index 204 of set 1, and col = 154 meaning index 154 of set 2
x1 = X1(row)
x2 = X2(col) % Should be the same!
fprintf('The overlap is closest to X = %f.\n', X1(row));
% Put up a vertical line there.
xline(X1(row), 'Color', 'm', 'LineWidth', 2);
yline(Y1(row), 'Color', 'm', 'LineWidth', 2);

카테고리

Help CenterFile Exchange에서 Design Condition Indicators Interactively에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by