How to find intersection between lines

1 Ansicht (letzte 30 Tage)
Hossein Alishahi
Hossein Alishahi am 6 Mär. 2021
Kommentiert: Walter Roberson am 2 Jan. 2025
I have below code
%% Input Values
clear all
clc
alpha1=input('Enter positive "alpha1"?');
alpha2=input('Enter positive "alpha2"?');
beta1=input('Enter positive "beta1"?');
beta2=input('Enter positive "beta2"?');
lambda=input('Enter positive "lambda"?');
eta1=input('Enter positive "eta1"?');
eta2=input('Enter positive "eta2"?');
P=input('Enter positive "P"?');
%% Feasible Area
x = -1:P;m = 0; c = eta2; y = m * x + c;
plot(x, y, 'black')
y = -1:P; m = 0; c = eta1; y = m * x + c;
plot(y, x, 'black')
eta1=min (eta1,P); eta2=min (eta2,P);
if eta1==0 && eta2==0;
X=[0 0 P]; Y=[0 P 0];
fill(X,Y,[0.85 0.85 0.85]); axis([-1 P -1 P])
end
if eta1+eta2<P
X=[0 0 eta1 eta1]; Y=[0 eta2 eta2 0];
fill(X,Y,[0.85 0.85 0.85]);
else
X=[0 0 P-eta2 eta1 eta1]; Y=[0 eta2 eta2 P-eta1 0];
fill(X,Y,[0.85 0.85 0.85]); axis([-1 P -1 P])
end
X=[0 0]; Y=[0 P];
line(X,Y,'Color','black')
hold on
line(Y,X,'Color','black')
g = @(x,y) x+y-P;
y1=fimplicit(g,[0 P 0 P])
f = @(x,y) log((1+alpha1*x./(1+alpha2*y))) -lambda*log((1+beta1*x./(1+beta2*y)));
y2=fimplicit(f,[-1 P -1 P], '-')
hold on
grid on
Where I consider alpha1=1, alpha2=0.2, beta1=1; beta2=2, lambda=2, eta1=2, eta2=3, P=1
I want to find the intersection for lines in the picture. Could you please help me!

Antworten (2)

darova
darova am 6 Mär. 2021
use polyxpoly
  2 Kommentare
Hossein Alishahi
Hossein Alishahi am 6 Mär. 2021
Thank you for your consideration.
It does not work for fimplicit function which has ploted the lines. Could you please give me more information in specefic codes
Best,
Hossein
darova
darova am 7 Mär. 2021
  • use contour to get lines
  • use polyxpoly to calculate interseciton
clc,clear
[x,y,z] = peaks(30);
[~,h] = contour(x,y,z,[0 0]);
x2 = [-2 3];
y2 = [-3 0];
h1 = get(h,'children');
x1 = get(h1(3),'xdata'); % extract 3d contour
y1 = get(h1(3),'ydata');
[xc,yc] = polyxpoly(x1,y1,x2,y2); % find intersection
hold on
plot(x1,y1,'.r')
plot(x2,y2,'linewidth',2)
plot3(xc,yc,'or')
hold off

Melden Sie sich an, um zu kommentieren.


Gautam
Gautam am 2 Jan. 2025
Hello Hossein,
If you want to find the point of intersection of any tow lines programmatically, you can do so following the steps below:
  1. Create handles to the line objects for which you want to find the intersection point, like:
line1 = line(X,Y,'Color','black');
line2 = line(Y,X,'Color','black');
2. Use the “XData” and “YData” properties of the line objects to find the point of intersection:
interX = line1.XData(line1.XData == line2.XData);>>interY = line1.YData(line1.YData == line2.YData);
3. You can show the point of intersection on the plot using
plot(interX, interY, 'rO', 'MarkerSize',6, 'MarkerFaceColor','red')
This would give you the intersection point like:
Following this, you can find the intersection etween any two lines
  1 Kommentar
Walter Roberson
Walter Roberson am 2 Jan. 2025
Comparing the XData and YData properties of two lines will generally not work.
X = [0 3];
Y = [1 2];
line1 = line(X,Y,'Color','black');
line2 = line(Y,X,'Color','black');
interX = line1.XData(line1.XData == line2.XData);
interY = line1.YData(line1.YData == line2.YData);
plot(interX, interY, 'rO', 'MarkerSize',6, 'MarkerFaceColor','red')
There is no-place that the XData from the one line happens to be exactly the XData from the other line.
The XData property does not interpolate. The result is not the same as
cla
X = 0:.1:3;
Y = linspace(1,2,numel(X));
line1 = line(X,Y,'Color','black');
line2 = line(Y,X,'Color','black');
interX = line1.XData(line1.XData == line2.XData);
interY = line1.YData(line1.YData == line2.YData);
plot(interX, interY, 'rO', 'MarkerSize',6, 'MarkerFaceColor','red')

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Line Plots finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by