Find intersection of two lines in MATLAB (2024)

One computational geometry question that we will want to address is how to determine the intersection of two line segments. This will allow for further solutions for more complex questions, including a general solution regarding whether a point is inside or outside of a convex or non-convex polygon. Previously, we’ve described how to define a line segment in MATLAB, and we will use this definition in our current method for solving for line intersections.

Note: Much credit for this post and explanation should be given to Gareth Rees. While preparing this post, I ran across his response, and I can’t do it much more justice, so here is his general implementation in MATLAB.
There are 5 possibilities if we have two line segments:
1) The two line segments are collinear and overlapping (intersecting portion is a line segment)
2) The two line segments are collinear and disjoint (not intersecting)
3) The two line segments are parallel (not intersecting)
4) Not parallel and intersect
5) Not parallel and non-intersecting

In order to determine collinearity and intersections, we will take advantage of the cross product. A cross product returns the vector perpendicular to two given vectors. Alternatively, if two segments are parallel, the cross product will be 0 (as A X B = |A|*|B|*sin(theta), and theta of 0 or 180 will return 0), and this will provide a great check for discriminating possibilities 1,2,3 and 4,5. The built-in cross MATLAB function will provide the cross product of two vectors, but doing so requires that the vectors be defined in three dimensions. We can therefore either append a 0 to all of our 2-D line segments or use the following function, which returns only the k vector (ignoring the i and j vectors) of the cross product.

%% Cross product returning z valuefunction z = cross(a, b) z = a(1)*b(2) - a(2)*b(1);

Now let’s define our function “checkSegmentIntersection”, which will take as input two line segments (A and B). These input arguments will be 2×2 arrays with each row describing the endpoints of the line segment. The output arguments of “doesIntersect” will be a boolean value true/false and “intersection” will provide the intersection point (or line segment) if there is an intersection (or overlap). Our default initialization of “false” and NaN will be the outputs for the second, third and fifth possibilities. We will therefore only check for the first and fourth possibilities.

function [doesIntersect, intersection] = checkSegmentIntersection(A, B) % Check if two line segments (A and B) intersect in 2D space. % initialize output values doesIntersect = false; intersection = NaN;

As we described previously, we will utilize parametric equations for the two line segments, such that segment1 = p + t*r and segment2 = q + u*s, where t and u range from 0 to 1.

% Solve for all of these variables given the two line segmentsp = A(1,:);r = parameterizeLine(A(1,:), A(2,:));q = B(1,:);s = parameterizeLine(B(1,:), B(2,:));

Then, as described by Gareth Rees, we can solve for the intersection using the following two equations:

% t = (q-p) x s/(r x s)% u = (q-p) x r/(r x s)% Solve for cross productsr_cross_s = cross(r, s);q_p_cross_s = cross(q-p, s);q_p_cross_r = cross(q-p, r);% solve for t and ut = q_p_cross_s / r_cross_s;u = q_p_cross_r / r_cross_s;

Now, let’s check if the two line segments are collinear or parallel. If the line segments are collinear/parallel and q_p_cross_r is not 0, then there is no intersection. Otherwise, if it is 0, we will find the line segment where overlapping occurs.

%% First Possibilityif r_cross_s == 0 if q_p_cross_r == 0 t0 = dot(q-p,r)/dot(r,r); if t0 >= 0 && t0 <= 1 doesIntersect = true; % return a line segment where intersection occurs intersection = [p; p+t0*r]; end end

On the other hand, if the line segments are not parallel (r_cross_s ~= 0), then we check to see if they intersect within the range of 0 to 1 for both u and t.

%% Fourth possibilityelse if t >= 0 && t <= 1 && u >=0 && u <= 1 doesIntersect = true; intersection = p + t * r; end end

Let’s do some validation of our code. First, for condition 1, let’s use A = [4 -1; 0 5]; and B = [3 0.5; 6 -4]; I’ve uploaded a version of the code that includes plotResults as an input argument if you want to give it a try, but here would be the output:

[doesInt, inter] = checkSegmentIntersection(A,B,true)doesInt = logical 1inter = 4.0000 -1.0000 3.0000 0.5000

For conditions 2 and 3, we would need collinear lines that do not intersect and parallel lines, respectively. Let’s use A = [4 -1; 0 5]; B = [6 -4; 8 -7] and [5 0; 1 6], respectively. Both conditions will return the following results for the intersection, with the following graphical representations.

[doesInt, inter] = checkSegmentIntersection(A,B,true)doesInt = logical 0inter = NaN

For condition 4, let’s generate line segments that intersect. Using A = [4 -1; 0 5]; and B = [5 2; 1 -2];

[doesInt, inter] = checkSegmentIntersection(A,B,true)doesInt = logical 1inter = 3.2000 0.2000

Finally, non-parallel lines that do not intersect. A = [4 -1; 0 5]; and B = [0 0 2 1];

[doesInt, inter] = checkSegmentIntersection(A,B,true)doesInt = logical 0inter = NaN

Take a look at the uploaded checkSegmentIntersection.m file if you want to try some line segment examples as well. If you have any comments or questions, please feel free to let us know.

Find intersection of two lines in MATLAB (2024)

FAQs

How do you find the intersection of two lines? ›

FAQs on Intersection of Two Lines

That is, have them in this form: y = mx + b. Set the two equations for y equal to each other. Solve for x. This will be the x-coordinate for the point of intersection.

What is the intersection command in Matlab? ›

Description. C = intersect( A,B ) returns the data common to both A and B , with no repetitions. C is in sorted order. If A and B are tables or timetables, then intersect returns the set of rows common to both tables.

How to find the intercept of a graph in Matlab? ›

You can do that in MATLAB using the "find(diff(sign('your_data')))" and "interp1" refer to this code with the consideration of sample 2D arrays. t = linspace(0, 2*pi, 1000); ph = -rand(10,1);

How to find the intersection between two functions? ›

Here's a step-by-step process:
  1. Write down the equations of the two functions, f(x) and g(x).
  2. Set the two functions equal to each other: f(x) = g(x).
  3. Solve the equation for x. ...
  4. For each x-value found in step 3, plug it back into either f(x) or g(x) to find the corresponding y-value.

How do you find the intersection of two sets? ›

Step 1: Determine all of the elements in the first set. Step 2: Determine all of the elements in the second set. Step 3: The intersection is formed by including all of the elements that appear in both Step 1 and Step 2.

What is the formula for the intersection of lines? ›

Point of intersection means the point at which two lines intersect. These two lines are represented by the equation a1x + b1y + c1= 0 and a2x + b2y + c2 = 0, respectively. Given figure illustrate the point of intersection of two lines. We can find the point of intersection of three or more lines also.

How to find point of intersection of two lines vectors? ›

To find the point of intersection of these two lines, we must find a point P that lies on both lines. At the point of intersection the coordinates for L1 will equal the coordinates for L2. This means there exist values of s and t such that the x, y and z coordinates of the two lines are equal.

What is XOR command in MATLAB? ›

Description. xor( A , B ) represents the logical exclusive disjunction. xor(A,B) is true when either A or B is true. If both A and B are true or false, xor(A,B) is false.

How do you find the intersection of two circles in MATLAB? ›

[ xout , yout ] = circcirc( centerx1 , centery1 , radius1 , centerx2 , centery2 , radius2 ) finds the intersection of two circles with the specified centers and radii, in Cartesian coordinates.

How to use det function in MATLAB? ›

Go to function:

d = det(X) returns the determinant of the square matrix X . If X contains only integer entries, the result d is also an integer.

How do you find the intercepts of a line graph? ›

Since two points determine any line, we can graph lines using the x- and y-intercepts. To find the x-intercept, set y=0 and solve for x. To find the y-intercept, set x=0 and solve for y. This method of finding x- and y-intercepts will be used throughout our study of algebra because it works for any equation.

How to find the intercept of two lines in R? ›

intersect() function in R Programming Language is used to find the intersection of two Objects. This function takes two objects like Vectors, Dataframes, etc. as arguments and results in a third object with the common data of both objects.

What is the formula for the intercept of a graph? ›

The intercept form of the equation of a line has an equation x/a + y/b = 1, where 'a' is the x-intercept, and 'b' is the y-intercept.

How do you find the point of intersection when given two equations? ›

Point of Intersection Formula
  1. The point of intersection formula is used to find the point of intersection of the two lines, that is the meeting point of two lines. ...
  2. a1x+b1y+c1=0 and a2x+b2y+c2=0.
  3. x= (b1c2-b2c1)/(a1b2-a2b1)
  4. y=(c1a2-c2a1)/(a1b2-a2b1)
  5. (x, y) = ((b1c2-b2c1)/(a1b2-a2b1), (c1a2-c2a1)/(a1b2-a2b1))

How do you find the intersection of two events? ›

We apply P(A ∩ B) formula to calculate the probability of two independent events A and B occurring together. It is given as, P(A∩B) = P(A) × P(B), where, P(A) is Probability of an event “A” and P(B) = Probability of an event “B”.

How do you find the intersection of two elements? ›

A ∩ B = {x : x ∈ A and x ∈ B}

That means x is an element of A ∩ B, if and only if x is an element of both A and B. Thus, we can use the word “AND” to represent the intersection of sets. Sometimes, the above expression can also be referred to as the intersection of sets formula.

How do you find the point of intersection of two vector equations? ›

By setting the values of x, y and z for these equations equal to one another, a value for both of the parameters can be found. These values can then be substituted back into the relevant vector equation to discover the point of intersection.

References

Top Articles
Latest Posts
Article information

Author: Terrell Hackett

Last Updated:

Views: 6326

Rating: 4.1 / 5 (52 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Terrell Hackett

Birthday: 1992-03-17

Address: Suite 453 459 Gibson Squares, East Adriane, AK 71925-5692

Phone: +21811810803470

Job: Chief Representative

Hobby: Board games, Rock climbing, Ghost hunting, Origami, Kabaddi, Mushroom hunting, Gaming

Introduction: My name is Terrell Hackett, I am a gleaming, brainy, courageous, helpful, healthy, cooperative, graceful person who loves writing and wants to share my knowledge and understanding with you.