MATLAB code of intersection (2024)

14 views (last 30 days)

Show older comments

Liza L on 28 Mar 2024

  • Link

    Direct link to this question

    https://matlabcentral.mathworks.com/matlabcentral/answers/2100311-matlab-code-of-intersection

  • Link

    Direct link to this question

    https://matlabcentral.mathworks.com/matlabcentral/answers/2100311-matlab-code-of-intersection

Edited: Liza L on 5 Apr 2024

This question was flagged by Dyuman Joshi

Show flagsHide flags

  • Flagged as Unclear by Dyuman Joshi on 5 Apr 2024.

    User has edited away (key details from) the question.

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.

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

Answers (3)

Matt J on 29 Mar 2024

  • Link

    Direct link to this answer

    https://matlabcentral.mathworks.com/matlabcentral/answers/2100311-matlab-code-of-intersection#answer_1433201

  • Link

    Direct link to this answer

    https://matlabcentral.mathworks.com/matlabcentral/answers/2100311-matlab-code-of-intersection#answer_1433201

Edited: Matt J on 29 Mar 2024

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);

MATLAB code of intersection (3)

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

William Rose on 28 Mar 2024

  • Link

    Direct link to this answer

    https://matlabcentral.mathworks.com/matlabcentral/answers/2100311-matlab-code-of-intersection#answer_1433156

  • Link

    Direct link to this answer

    https://matlabcentral.mathworks.com/matlabcentral/answers/2100311-matlab-code-of-intersection#answer_1433156

@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

Show NoneHide None

William Rose on 29 Mar 2024

Direct link to this comment

https://matlabcentral.mathworks.com/matlabcentral/answers/2100311-matlab-code-of-intersection#comment_3114181

  • Link

    Direct link to this comment

    https://matlabcentral.mathworks.com/matlabcentral/answers/2100311-matlab-code-of-intersection#comment_3114181

Open in MATLAB Online

@Liza L

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

MATLAB code of intersection (6)

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 on 29 Mar 2024

Direct link to this comment

https://matlabcentral.mathworks.com/matlabcentral/answers/2100311-matlab-code-of-intersection#comment_3114761

  • Link

    Direct link to this comment

    https://matlabcentral.mathworks.com/matlabcentral/answers/2100311-matlab-code-of-intersection#comment_3114761

Open in MATLAB Online

@Liza L,

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

MATLAB code of intersection (8) .

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

MATLAB code of intersection (9) .

Once we know x_int, we can find y_int:

MATLAB code of intersection (10)

Plugging in the values for m1 and b1, this becomes

MATLAB code of intersection (11)

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.')

MATLAB code of intersection (12)

Sign in to comment.

John D'Errico on 29 Mar 2024

  • Link

    Direct link to this answer

    https://matlabcentral.mathworks.com/matlabcentral/answers/2100311-matlab-code-of-intersection#answer_1433186

  • Link

    Direct link to this answer

    https://matlabcentral.mathworks.com/matlabcentral/answers/2100311-matlab-code-of-intersection#answer_1433186

Edited: John D'Errico on 29 Mar 2024

I answered exactly this question only recently. So your class must be all getting this homework assignment.

https://www.mathworks.com/matlabcentral/answers/2092511-lines-that-meet-each-other?s_tid=srchtitle

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.

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

See Also

Categories

MATLABGraphics2-D and 3-D PlotsSurfaces, Volumes, and PolygonsSurface and Mesh Plots

Find more on Surface and Mesh Plots in Help Center and File Exchange

Tags

  • matlab
  • line
  • intersection

Community Treasure Hunt

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

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


MATLAB code of intersection (14)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia Pacific

Contact your local office

MATLAB code of intersection (2024)
Top Articles
Latest Posts
Article information

Author: Foster Heidenreich CPA

Last Updated:

Views: 6257

Rating: 4.6 / 5 (76 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Foster Heidenreich CPA

Birthday: 1995-01-14

Address: 55021 Usha Garden, North Larisa, DE 19209

Phone: +6812240846623

Job: Corporate Healthcare Strategist

Hobby: Singing, Listening to music, Rafting, LARPing, Gardening, Quilting, Rappelling

Introduction: My name is Foster Heidenreich CPA, I am a delightful, quaint, glorious, quaint, faithful, enchanting, fine person who loves writing and wants to share my knowledge and understanding with you.