Math – Find the intersection between two vectors in matlab

I have a very simple MATLAB question. What is the easiest way to find the intersection between two vectors. I am not familiar with various MATLAB functions-it seems there should be one like this. < p>

For example, if I have a vector from (0,0) to (6,6) and another vector from (0,6) to (6,0), I need to determine They intersect at (3,3).

One solution is to use this tutorial for Finding the intersection point of two lines in the equation derived in 2-D (update: this is an Internet archive link, because the site no longer exists). You can first create two matrices: one for storing the x-coordinates of the end points of the lines , The other is used to keep the y coordinate.

x = [0 0; 6 6]; %# Starting points in first row, ending points in second row< br />y = [0 6; 6 0];

Then the equation from the above source can be encoded as follows:

dx = diff( x); %# Take the differences down each column
dy = diff(y);
den = dx(1)*dy(2)-dy(1)*dx(2); %# Precompute the denominator
ua = (dx(2)*(y(1)-y(3))-dy(2)*(x(1)-x(3)))/den;
ub = (dx(1)*(y(1)-y(3))-dy(1)*(x(1)-x(3)))/den;

Now, You can calculate the intersection of two lines:

xi = x(1)+ua*dx(1);
yi = y(1)+ua* dy(1);

For the example in the question, the above code gives xi = 3 and yi = 3, as expected. If you want to check whether the intersection is between the end points of the line (i.e. They are finite line segments), you only need to check whether the values ​​ua and ub are between 0 and 1:

< /p>

isInSegment = all(([ua ub] >= 0) & ([ua ub] <= 1));

There are a few more tutorials I linked above:

>If the denominator den is 0, then the two lines are parallel. >If the denominator and numerator of the ua and ub equations are 0, the two lines coincide.

I have a very simple MATLAB question. What is the easiest way to find the intersection between two vectors. I am not familiar with various MATLAB functions-it seems that there should be one like this.

< p> For example, if I have a vector from (0,0) to (6,6) and another vector from (0,6) to (6,0), I need to make sure they are in (3,3) Intersect at the place.

One solution is to use the equation derived in this tutorial for finding the intersection point of two lines in 2-D (update: This is an Internet archive link, because the site no longer exists). You can create two matrices first: one to hold the x-coordinates of the end points of the line, and the other to hold the y-coordinates.

x = [0 0; 6 6]; %# Starting points in first row, ending points in second row
y = [0 6; 6 0];

Then the equations from the above sources can be encoded as follows:

dx = diff(x); %# Take the differences down each column
dy = diff( y);
den = dx(1)*dy(2)-dy(1)*dx(2); %# Precompute the denominator
ua = (dx(2)*(y(1 )-y(3))-dy(2)*(x(1)-x(3)))/den;
ub = (dx(1)*(y(1)-y(3) )-dy(1)*(x(1)-x(3)))/den;

Now, you can calculate the intersection of two lines:

xi = x(1)+ua*dx(1);
yi = y(1)+ua*dy(1);

For the example in the question, the above The code gives xi = 3 and yi = 3. As expected. If you want to check whether the intersection is between the end points of the line (that is, they are finite line segments), you only need to check whether the values ​​ua and ub are between 0 and 1:

isInSegment = all(([ua ub] >= 0) & ([ua ub] <= 1));

There are a few more tutorials I linked above:< /p>

>If the denominator den is 0, then the two lines are parallel. >If the denominator and numerator of the ua and ub equations are 0, the two lines coincide.

Leave a Comment

Your email address will not be published.