what is the mechanism of intersection between two lines ?

18 vues (au cours des 30 derniers jours)
sufian ahmed
sufian ahmed le 26 Août 2017
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;

Réponses (2)

David Goodmanson
David Goodmanson le 26 Août 2017
Modifié(e) : David Goodmanson le 26 Août 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 commentaires
sufian ahmed
sufian ahmed le 28 Août 2017
@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 le 3 Sep 2017
Well, do you have an image, OR do you have equations of lines? Exactly what are you starting with?

Connectez-vous pour commenter.


Image Analyst
Image Analyst le 3 Sep 2017

Catégories

En savoir plus sur Mathematics and Optimization dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by