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 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 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 do you find the intersection point 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 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.

What is the formula for coordinates of point of intersection of two lines? ›

Properties of Intersection of Sets
LawRule of Intersection
Commutative LawA ∩ B = B ∩ A
Associative Law(A ∩ B) ∩ C = A ∩ (B ∩ C)
Law of ϕ and Uϕ ∩ A = ϕ, U ∩ A= A
Idempotent LawA ∩ A = A
1 more row

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 do you find the point of 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 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 to 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 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 to find points of intersection of two lines in Matlab? ›

Direct link to this answer
  1. t=0:.1:10;
  2. y1=sin(t); % say f1.
  3. y2=exp(t); %f2.
  4. intersection=find(y1==y2);
Oct 3, 2018

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

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.

How do you check if two polygons intersect in Matlab? ›

polyout = intersect( poly1 , poly2 ) returns a polyshape object whose regions are the geometric intersection of two polyshape objects. The intersection contains the regions of poly1 and poly2 that overlap. poly1 and poly2 must have compatible array sizes.

How do you find the point on the intersection line of two planes? ›

To find a point on the line, you set one of the coordinates in the equations of both planes equal to zero and solve the system of equations you end up with. Note! If you set z = 0 and the line of intersection is perpendicular to the z -axis, no points on the line have z = 0 .

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.

References

Top Articles
Latest Posts
Article information

Author: Rev. Porsche Oberbrunner

Last Updated:

Views: 6328

Rating: 4.2 / 5 (53 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Rev. Porsche Oberbrunner

Birthday: 1994-06-25

Address: Suite 153 582 Lubowitz Walks, Port Alfredoborough, IN 72879-2838

Phone: +128413562823324

Job: IT Strategist

Hobby: Video gaming, Basketball, Web surfing, Book restoration, Jogging, Shooting, Fishing

Introduction: My name is Rev. Porsche Oberbrunner, I am a zany, graceful, talented, witty, determined, shiny, enchanting person who loves writing and wants to share my knowledge and understanding with you.