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 to find the intersection of two lines? ›

Two distinct lines intersect at the most at one point. To find the intersection of two lines we just need to solve their equations. The alternative way is to graph the lines and find their point of intersection. The lines will intersect only if they are non-parallel lines.

How to find the point of intersection of two graphs in matlab? ›

I would do it like this:
  1. % define x Axis and evaluate functions.
  2. x_points = -5:0.1:5;
  3. function1 = -x+1;
  4. function2 = x+1;
  5. index_intersection = find(function1 == function2);
  6. x_value_intersection = x_points(index_intersection);
  7. y_value_intersection = function1(index_intersection);
  8. % plot functions and intersection point:

How to find the intersection of two vectors in Matlab? ›

Intersection of Two Vectors and Their Indices

Find the values common to both A and B , as well as the index vectors ia and ib , such that C = A(ia) and C = B(ib) .

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);

What is the point of intersection in Matlab? ›

The point of intersection is the meeting point of two straight lines. If two crossing straight lines have the same equations, the intersection point can be found by solving both equations at the same time.

How do you find the intersection of two values? ›

To determine the points of intersection of two functions, you need to find the values of the independent variable (usually x) for which both functions have the same dependent variable value (usually y). In other words, you need to find the x-values for which the two functions are equal.

How do you find the intersection of two relations? ›

For b , going off the last example, the intersection R1∩R2 would consist of ordered pairs {(1,1)}. Now applying that idea here, R1∩R2 relation would be {(a,b)|a divides b or a is a multiple of b} or {(a,b)|a=cb or b=ak for some integers k and c}.

How do you find the intersection of two graphs? ›

The Intersection Between Two Graphs

You find the point where the graph of f and the graph of g intersects by solving the equation f ( x ) = g ( x ) . Insert x into f ( x ) = − x − 1 because it's the simpler expression of the two. You could also insert x into g ( x ) , but it would be more work.

How do you find the point of intersection of two trend lines? ›

If you have the equations of the lines, then just subtract one from the other, find the zero of the difference for the x coordinate and just calculate the linear function for each of the lines to get the single y coordinate.

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

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.

What is the intersect command in Matlab? ›

Description. intersect( shape1,shape2 ) plots a shape generated by intersecting the shape1 and shape2 . Alternatively, you can also use the '&' operator to intersect the shapes ( shape1 & shape2 ).

How do you find the intersection of a line and circle in Matlab? ›

[ xout , yout ] = linecirc( slope , intercpt , centerx , centery , radius ) finds the intersection of a line with the specified slope and intercept and a circle with the specified center and radius, in Cartesian coordinates.

How do you find the intersection of two linear equations? ›

To find the point of intersection algebraically, solve each equation for y, set the two expressions for y equal to each other, solve for x, and plug the value of x into either of the original equations to find the corresponding y-value. The values of x and y are the x- and y-values of the point of intersection.

How to find the coordinates of 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 to find the coordinates of a point of intersection of two diagonals? ›

Therefore, the intersection point can be found by calculating the midpoint of one of the diagonals. Let's calculate the midpoint of the diagonal AC (A to C). Midpoint formula: \((x_1 + x_2)/2, (y_1 + y_2)/2\).

How do you find the point of intersection of two graphs? ›

When the graphs of y = f(x) and y = g(x) intersect , both graphs have exactly the same x and y values. So we can find the point or points of intersection by solving the equation f(x) = g(x). The solution of this equation will give us the x value(s) of the point(s) of intersection.

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.

Top Articles
Latest Posts
Article information

Author: Lakeisha Bayer VM

Last Updated:

Views: 6403

Rating: 4.9 / 5 (69 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Lakeisha Bayer VM

Birthday: 1997-10-17

Address: Suite 835 34136 Adrian Mountains, Floydton, UT 81036

Phone: +3571527672278

Job: Manufacturing Agent

Hobby: Skimboarding, Photography, Roller skating, Knife making, Paintball, Embroidery, Gunsmithing

Introduction: My name is Lakeisha Bayer VM, I am a brainy, kind, enchanting, healthy, lovely, clean, witty person who loves writing and wants to share my knowledge and understanding with you.