MATLAB code of intersection

69 views (last 30 days)
Liza L
Liza L on 28 Mar 2024 at 20:15
Edited: Liza L on 5 Apr 2024 at 10:14
This question was flagged by Dyuman Joshi
Hello, i intersect two sets of lines from two different origins at a distance of 10 cm from 0 to 90 degrees with an angle difference of two degrees. A straight line is obtained from the intersection of lines of the same degree. From the collision of lines, twice the degree of curvature is obtained.

Answers (3)

Matt J
Matt J on 29 Mar 2024 at 2:34
Edited: Matt J on 29 Mar 2024 at 12:45
f=@(z) z(:,1:2)./z(:,3);
t=(0:2:30)'; e=ones(size(t));
D=[cosd(t), sind(t), 0*e];
L1=cross(e*[0 0 1], D); L2=cross(e*[5 0 1], D.*[-1 1 1]);
P=f(cross( L1(1:end-1,:) , L2(2:end,:) ));
plot(P(:,1), P(:,2), 'x-', 5-P(:,1),P(:,2),'x-'); xline(2.5);

William Rose
William Rose on 28 Mar 2024 at 22:35
@Liza L, what have you tried so far?
The first part of your question, drawing the straight lines, shouldn't be too hard. Draw a line by specifying its endpoints. You can use a for loop to call plot() multiple times, with "hold on", so that successive plots will all be kept on the fiogure.
Then there is the plot of the curve. The curve is composed of the set of points where the lines intersect. If you have the endpoints of a line, you can get the y=mx+b equation for the line. Then use the formua for the intersection of two lines to get the cordinates of one point. Repeat for successive pairs of lines to find a list on intersecting points which make up the curve of interest. Then plot those points.
  2 Comments
William Rose
William Rose on 29 Mar 2024 at 1:31
Here is a simple example of how you could draw the lines. Maybe you can come up with better way to do it.
deg=0:3:90;
x1end=10*cosd(deg); % line 1 x-endpoints
y1end=10*sind(deg); % line 1 y-endpoints
x2end=10-10*cosd(deg); % line 2 x-endpoints
y2end=y1end; % line 2 y-endpoints
figure;
for i=1:length(deg)
plot([0,x1end(i)],[0,y1end(i)],'-b'); hold on;
plot([10,x2end(i)],[0,y2end(i)],'-b')
end
axis equal
By the way, you said in your post that the origins should be 10 cm apart, but they are only 5 apart in your figure. I have made the origins 10 apart, above.
Now work on the intersections. It appears from the curves drawn in your plot that you are interested in the intersection of pairs of lines that differ by 0, 1, or -1 in their degree index. That was not obvious to me from the text of your intial posting.
William Rose
William Rose on 29 Mar 2024 at 17:08
The symbolic solution for the intersections, by @John D'Errico at the site he links to, is very impressive. And the solution of @Matt J, using cross products, is also very elegant.
Another solution is below. Not as nice as their solutions, but it just shows there are multiple ways to do things.
A general solution for the intersection of two lines is to find x that is the same for both. Line 1 is y=m1x+b1, and line 2 is y=m2x+b2. They intersect at the point where m1x+b1=m2x+b2, i.e. the intersection is at
.
For this particular problem, the lines that go through the origin have slope=m1=tan(deg1) and intercept b1=0, where deg1 is the line angle, in degrees. The lines that go through the point x=10,y=0 have slope m2=-tan(deg2) and intercept b2=10*tan(deg2). Plugging in the values for m1, b1, m2, b2 into the formula for x_int, and simplifying, we get
.
Once we know x_int, we can find y_int:
Plugging in the values for m1 and b1, this becomes
Use the formulas for x_int and y_int, above, to write a script that plots the curves.
STarting with the script I offered above, and adding to it, we get
deg=3:3:90;
x1end=10*cosd(deg); % line 1 x-endpoints
y1end=10*sind(deg); % line 1 y-endpoints
x2end=10-10*cosd(deg); % line 2 x-endpoints
y2end=y1end; % line 2 y-endpoints
figure;
for i=1:length(deg)
plot([0,x1end(i)],[0,y1end(i)],'-b'); hold on;
plot([10,x2end(i)],[0,y2end(i)],'-b')
end
axis equal
% Find intersection points where line 1 is 3 deg higher than line 2
xint1=10*tand(deg(1:19))./(tand(deg(2:20))+tand(deg(1:19)));
yint1=tand(deg(2:20)).*xint1;
% Find intersection points where line 1 is 3 deg lower than line 2
xint2=10*tand(deg(2:20))./(tand(deg(1:19))+tand(deg(2:20)));
yint2=tand(deg(1:19)).*xint2;
% Find intersection points where line 1 and line 2 have same degrees
xint3=10*tand(deg(1:20))./(tand(deg(1:20))+tand(deg(1:20)));
yint3=tand(deg(1:20)).*xint3;
% Plot the intersection points
plot(xint1,yint1,'-r.',xint2,yint2,'-g.',xint3,yint3,'-k.')

Sign in to comment.


John D'Errico
John D'Errico on 29 Mar 2024 at 2:07
Edited: John D'Errico on 29 Mar 2024 at 2:09
I answered exactly this question only recently. So your class must be all getting this homework assignment.
In my answer, I derived the governing equation of the intersections of those lines. Of course, your question also tells me I did their homework assignment. Sigh. Every once in a while one gets past.

Community Treasure Hunt

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

Start Hunting!