what is the mechanism of intersection between two lines ?
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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;
0 Kommentare
Antworten (2)
David Goodmanson
am 26 Aug. 2017
Bearbeitet: David Goodmanson
am 26 Aug. 2017
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 Kommentare
Image Analyst
am 3 Sep. 2017
Well, do you have an image, OR do you have equations of lines? Exactly what are you starting with?
Siehe auch
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!