what is the mechanism of intersection between two lines ?

3 次查看(过去 30 天)
I got this code from internet, It works for intersection of two lines. but i dont understand how it works ? Please anyone explain it.
x1 = [v(1,1) v(1,2)]; %vertical line X intersection point.
y1 = [v(2,1) v(2,2)];%vertical line Y intersection point.
%line2
x2 = [h(1,1) h(1,2)];%Horizontal line X intersection point.
y2 = [h(2,1) h(2,2)];%Horizontal line Y intersection point.
%fit linear polynomial
p1 = polyfit(x1,y1,1);
p2 = polyfit(x2,y2,1);
%calculate intersection
x_intersect = fzero(@(x) polyval(p1-p2,x),3);
y_intersect = polyval(p1,x_intersect);
P(1)=x_intersect;
P(2)=y_intersect;

回答(2 个)

David Goodmanson
David Goodmanson 2017-8-26
编辑:David Goodmanson 2017-8-26
Hi sufian,
Well, you can find all kinds of code on the internet, and on this forum most people are more likely to provide code ideas than try to unravel code by some third party. Especially in a case like this which uses two polyfits, an fzero and a polyval to solve this problem, all of which are not necessary.
Assume there is a line defined by endpoints a and b and another defined by endpoints c and d. Each point is given by a 2x1 column vector such as [ax; ay]. Any point along the line ab (or its extension off the ends) is described by p = a + alpha*(b-a) for some scalar alpha, similarly for line cd. The intersection point occurs when
a + alpha*(b-a) = c + beta*(d-c)
for some unique alpha and beta which you have to solve for. If you put this into matrix form you can arrive at
[(b-a) (d-c)]*lambda = (c-a)
where [...] is 2x2, and lambda is the vector [alpha; -beta] as you can check. (In this equation the top row is x components and the bottom row is y components). The solution is simply
lambda = [(b-a) (d-c)]\(c-a);
then p = a + lambda(1)*(b-a)
and also p = c - lambda(2)*(d-c)
as a check.
  5 个评论
sufian ahmed
sufian ahmed 2017-8-28
@jan i want to find this intersections, showed in image. But how can i do the code in matlab. since i have:
Line 1: [x1 y1] [x2 y2] Line 2: [x3 y3] [x4 y4]
Image Analyst
Image Analyst 2017-9-3
Well, do you have an image, OR do you have equations of lines? Exactly what are you starting with?

请先登录,再进行评论。


Image Analyst
Image Analyst 2017-9-3

Community Treasure Hunt

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

Start Hunting!

Translated by