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 points of two lines in Matlab? ›

[ xi , yi ] = polyxpoly( x1 , y1 , x2 , y2 ) returns the intersection points of two polylines in a planar, Cartesian system, with vertices defined by x1 , y1 , x2 and y2 .

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 do you find the intersection of two sets? ›

For any two sets A and B, the intersection, A ∩ B (read as A intersection B) lists all the elements that are present in both sets (common elements of A and B). For example, if Set A = {1,2,3,4,5} and Set B = {3,4,6,8}, A ∩ B = {3,4}.

How can two lines intersect at two points? ›

We know that the two non-parallel lines can intersect at a single point only which means they doesn't intersect at more than one points. Therefore, the given statement is false. The correct statement is that the two lines can only intersect at one point.

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

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.

How to find intersections of 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.

What is the intersection of two straight lines given? ›

The intersection of two straight lines gives us a point.

Two straight lines can intersect only once and hence can form a single point. The point can also give us the exact location or can be used as a reference for measurements.

What is the intersect tool in MATLAB? ›

c = intersect( shape1,shape2 ) generates the shape obtained by intersecting shape1 and shape2 and returns a polygon object for resultant 2-D shape or a custom 3-D object for resultant 3-D shape. Alternatively, you can also use the '&' operator to intersect the shapes ( c = shape1 & shape2 ).

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 rectangles in MATLAB? ›

area = rectint(A,B) returns the area of intersection of the rectangles specified by position vectors A and B . If A and B each specify one rectangle, the output area is a scalar. A and B can also be matrices, where each row is a position vector.

How to find the intersection of two lines? ›

To algebraically find the intersection of two straight lines, write the equation for each line with y on the left side. Next, write down the right sides of the equation so that they are equal to each other and solve for x.

What is the symbol for the intersection of two lines? ›

The symbol we use for the intersection is ∩. The word that you will often see that indicates an intersection is "and".

How do you find the point of intersection of 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 point of intersection of two planes? ›

1. Take the cross product of the normal vectors to find the directional vector of the line of intersection: n → α × n → β = ( 1 , 2 , − 3 ) × ( 1 , − 1 , 2 ) = ( 4 − 3 , − 3 − 2 , − 1 − 2 ) = ( 1 , − 5 , − 3 ) . 2. Set z = 0 and solve the system of equations to find the point P .

How do you find the intersection of two rectangles in Matlab? ›

area = rectint(A,B) returns the area of intersection of the rectangles specified by position vectors A and B . If A and B each specify one rectangle, the output area is a scalar. A and B can also be matrices, where each row is a position vector.

References

Top Articles
Latest Posts
Article information

Author: Francesca Jacobs Ret

Last Updated:

Views: 5339

Rating: 4.8 / 5 (48 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Francesca Jacobs Ret

Birthday: 1996-12-09

Address: Apt. 141 1406 Mitch Summit, New Teganshire, UT 82655-0699

Phone: +2296092334654

Job: Technology Architect

Hobby: Snowboarding, Scouting, Foreign language learning, Dowsing, Baton twirling, Sculpting, Cabaret

Introduction: My name is Francesca Jacobs Ret, I am a innocent, super, beautiful, charming, lucky, gentle, clever person who loves writing and wants to share my knowledge and understanding with you.