version 1.0.0.0 (1.75 KB) by
Nassim Khaled

plane_line_intersect computes the intersection of a plane and a segment(or

%plane_line_intersect computes the intersection of a plane and a segment (or a straight line)

% Inputs:

% n: normal vector of the Plane

% V0: any point that belongs to the Plane

% P0: end point 1 of the segment P0P1

% P1: end point 2 of the segment P0P1

%

%Outputs:

% I is the point of interection

% Check is an indicator:

% 0 => disjoint (no intersection)

% 1 => the plane intersects P0P1 in the unique point I

% 2 => the segment lies in the plane

% 3=>the intersection lies outside the segment P0P1

%

% Example:

% Determine the intersection of following the plane x+y+z+3=0 with the segment P0P1:

% The plane is represented by the normal vector n=[1 1 1]

% and an arbitrary point that lies on the plane, ex: V0=[1 1 -5]

% The segment is represented by the following two points

% P0=[-5 1 -1]

%P1=[1 2 3]

% [I,check]=plane_line_intersect([1 1 1],[1 1 -5],[-5 1 -1],[1 2 3]);

Nassim Khaled (2020). straight line and plane intersection (https://www.mathworks.com/matlabcentral/fileexchange/17751-straight-line-and-plane-intersection), MATLAB Central File Exchange. Retrieved .

Created with
R14SP1

Compatible with any release

**Inspired:**
sliceDelaunay

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!Create scripts with code, output, and formatted text in a single executable document.

Shehryar AshrafHow would I extend this to calculate intersection of multiple lines with multiple planes. I have done that, but with nested for loops, i wish to vectorize my problem, how would I go about it?

APS502correction, i just relaised this script only tests if the line is interesecting the infiinite plane, not the bounded plane. - unfortunately i cant delte the previous comment!

APS502warning - there is an error in this script, but i cant find it. The following test should return the check = 3 but instead returns check = 1.

A =[ -6.8756 9.9090 10.0000],B =[ -6.0096 10.4090 10.0000],C =[ -6.0096 10.4090 11.0000],D=[ -6.8756 9.9090 11.0000];

P0 =[ 1.3978 40.0000 6.1149],P1 =[ 4.3943 -4.8078 0.3551];

quad = [A; B ; C; D];

line = [P0; P1];

AB = B-A

AD = D-A

n = cross(AB,AD)/sqrt(dot(cross(AB,AD),cross(AB,AD)))

[I,check]=plane_line_intersect(n,A,P0,P1)

figure;

plot3(line(:,1),line(:,2),line(:,3),'.-'); hold on;

patch(quad(:,1),quad(:,2),quad(:,3), 'green');

plot3(I(:,1),I(:,2),I(:,3),'.r'); hold on;

grid on;

axis equal;

James Chukwuemeka AgadaVery helpful in solving simple line and plane intersections

However, i'm having trouble implementing it to calculate intersection between a Gcode toolpath and 2 planes. That is multiple lines going through 2 planes.

Can anyone help?

Atle AalerudSinéad KearneyLukas HulinskyShugen Livery helpful.

I think you should check whether P0 is coincide with P1.

Bashar AlsadikJanuka AttanayakeVasileios VlachakisThanks a lot, very helpful function and very well implemented!

Darian ViezzerThis solution works perfectly. If someone is interested, I extended it such that it calculates the intersection of multiple segments with multiple planes (no loops, but 3 dimensional matrices are used).

Luis Fernando MosqueiraAlexander HeinzJamesJames GroveJack_111raminBart RibbensMarwan Nthnx a lot man....i had a tough job battering my brains to get this job done....g8 work!!

YasminI was using symbolic math toolbox earlier to solve this problem which doesnot get compiled when I use MCR. I used this code and it worked!!! Thanks so much!!

Chee Hou KokDear Mr. Nassim Khaled,

Is it possible to find out whether a straight line is intersecting a rectangle using your file?

Say, I have a line made of points (0,0) and (5,5) and a rectangle of points (1,1) (3,1) (3,4) and (1,4). How do know that the line is intersecting the rectangle?