MATLAB code of intersection (2024)

Info

This question is locked. Reopen it to edit or answer.

100 views (last 30 days)

Show older comments

Liza L on 28 Mar 2024

  • Link

    Direct link to this question

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

  • Link

    Direct link to this question

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

Locked: Rena Berman on 5 Jun 2024 at 18:16

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. I need a MATLAB code to draw this straight line and curves.

MATLAB code of intersection (2)

1 Comment

Show -1 older commentsHide -1 older comments

Rena Berman on 5 Jun 2024 at 18:15

Direct link to this comment

https://physical-modeling.mathworks.com/matlabcentral/answers/2100311-matlab-code-of-intersection#comment_3180121

  • Link

    Direct link to this comment

    https://physical-modeling.mathworks.com/matlabcentral/answers/2100311-matlab-code-of-intersection#comment_3180121

(Answers Dev) Restored edit

This question is locked.

Answers (3)

Matt J on 29 Mar 2024

  • Link

    Direct link to this answer

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

Edited: Matt J on 29 Mar 2024

Open in MATLAB Online

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 (5)

0 Comments

Show -2 older commentsHide -2 older comments

William Rose on 28 Mar 2024

  • Link

    Direct link to this answer

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

  • Link

    Direct link to this answer

    https://physical-modeling.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://physical-modeling.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 (8)

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://physical-modeling.mathworks.com/matlabcentral/answers/2100311-matlab-code-of-intersection#comment_3114761

  • Link

    Direct link to this comment

    https://physical-modeling.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 (10) .

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 (11) .

Once we know x_int, we can find y_int:

MATLAB code of intersection (12)

Plugging in the values for m1 and b1, this becomes

MATLAB code of intersection (13)

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 (14)

John D'Errico on 29 Mar 2024

  • Link

    Direct link to this answer

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

  • Link

    Direct link to this answer

    https://physical-modeling.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

This question is locked.

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 (16)

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: Dan Stracke

Last Updated:

Views: 5979

Rating: 4.2 / 5 (43 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Dan Stracke

Birthday: 1992-08-25

Address: 2253 Brown Springs, East Alla, OH 38634-0309

Phone: +398735162064

Job: Investor Government Associate

Hobby: Shopping, LARPing, Scrapbooking, Surfing, Slacklining, Dance, Glassblowing

Introduction: My name is Dan Stracke, I am a homely, gleaming, glamorous, inquisitive, homely, gorgeous, light person who loves writing and wants to share my knowledge and understanding with you.