Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Function to determine if a line intersects a triangle

Subject: Function to determine if a line intersects a triangle

From: Liana

Date: 29 Mar, 2011 20:27:04

Message: 1 of 6

Hello All,

I'd like to find out if there is some MATLAB built-in function used to determine the intersection between a triangle and a line. I need just to know if the line intersects the triangle - 1: intersects, 0: do not intersect.

Thanks.

Subject: Function to determine if a line intersects a triangle

From: Florin Neacsu

Date: 29 Mar, 2011 20:40:20

Message: 2 of 6

"Liana" wrote in message <imtfao$g3c$1@fred.mathworks.com>...
> Hello All,
>
> I'd like to find out if there is some MATLAB built-in function used to determine the intersection between a triangle and a line. I need just to know if the line intersects the triangle - 1: intersects, 0: do not intersect.
>
> Thanks.

Hi,

I don't think there is a built in function for that. But you can write your own function.
There are a few ways you can do this, it depends if it's 2D, 3D, if you are dealing with a line or a segment ...

Have a look here : http://softsurfer.com/Archive/algorithm_0106/algorithm_0106.htm#dist3D_Segment_to_Segment

It's one method.

Good luck.
Florin

Subject: Function to determine if a line intersects a triangle

From: Liana

Date: 29 Mar, 2011 21:18:22

Message: 3 of 6

Thank you very much. Or maybe I will simply use the function of Sebastian Hölz, and apply it for my needs: http://www.mathworks.com/matlabcentral/fileexchange/8908.

"Florin Neacsu" <fneacsu2@gmail.com> wrote in message <imtg3k$1h$1@fred.mathworks.com>...
> "Liana" wrote in message <imtfao$g3c$1@fred.mathworks.com>...
> > Hello All,
> >
> > I'd like to find out if there is some MATLAB built-in function used to determine the intersection between a triangle and a line. I need just to know if the line intersects the triangle - 1: intersects, 0: do not intersect.
> >
> > Thanks.
>
> Hi,
>
> I don't think there is a built in function for that. But you can write your own function.
> There are a few ways you can do this, it depends if it's 2D, 3D, if you are dealing with a line or a segment ...
>
> Have a look here : http://softsurfer.com/Archive/algorithm_0106/algorithm_0106.htm#dist3D_Segment_to_Segment
>
> It's one method.
>
> Good luck.
> Florin

Subject: Function to determine if a line intersects a triangle

From: Matt J

Date: 29 Mar, 2011 21:24:05

Message: 4 of 6

"Florin Neacsu" <fneacsu2@gmail.com> wrote in message <imtg3k$1h$1@fred.mathworks.com>...
>
> I don't think there is a built in function for that. But you can write your own function.
> There are a few ways you can do this, it depends if it's 2D, 3D, if you are dealing with a line or a segment ...
================

If dealing with a 2D line (not a line segment) the following ought to be enough

function bool=intersectTriangle(A,B,C,V)
%
% bool=intersectTriangle(A,B,C,V)
%
%Given the line whos equation is A*x+B*y+C=0 and 2x3 matrix V whose
%columns are vertices of a triangle, the function returns bool=true
%if the line intersects the triangle and bool=false otherwise.


V(end+1,:)=1;
Signs=sign([A,B,C]*V);
bool=~all(Signs==Signs(1));

Subject: Function to determine if a line intersects a triangle

From: Florin Neacsu

Date: 29 Mar, 2011 22:12:05

Message: 5 of 6

"Liana" wrote in message <imtiau$b2v$1@fred.mathworks.com>...
> Thank you very much. Or maybe I will simply use the function of Sebastian Hölz, and apply it for my needs: http://www.mathworks.com/matlabcentral/fileexchange/8908.
>
> "Florin Neacsu" <fneacsu2@gmail.com> wrote in message <imtg3k$1h$1@fred.mathworks.com>...
> > "Liana" wrote in message <imtfao$g3c$1@fred.mathworks.com>...
> > > Hello All,
> > >
> > > I'd like to find out if there is some MATLAB built-in function used to determine the intersection between a triangle and a line. I need just to know if the line intersects the triangle - 1: intersects, 0: do not intersect.
> > >
> > > Thanks.
> >
> > Hi,
> >
> > I don't think there is a built in function for that. But you can write your own function.
> > There are a few ways you can do this, it depends if it's 2D, 3D, if you are dealing with a line or a segment ...
> >
> > Have a look here : http://softsurfer.com/Archive/algorithm_0106/algorithm_0106.htm#dist3D_Segment_to_Segment
> >
> > It's one method.
> >
> > Good luck.
> > Florin


Hi,

I couldn't see the program you are suggesting (the link is broken).
As I said you should pay attention to the difference between line / segment and 2d/3d. In 3D, often you get a nonzero value as the minimum distance for lines that are supposed to intersect, so you should choose a threshold.

Regards,
Florin

Subject: Function to determine if a line intersects a triangle

From: Liana

Date: 29 Mar, 2011 22:23:05

Message: 6 of 6

Yes, you are right. I will add a threshold. Sorry, the link is:
http://www.mathworks.com/matlabcentral/fileexchange/8908-curve-intersect-2

"Liana" wrote in message <imtiau$b2v$1@fred.mathworks.com>...
> Thank you very much. Or maybe I will simply use the function of Sebastian Hölz, and apply it for my needs: http://www.mathworks.com/matlabcentral/fileexchange/8908.
>
> "Florin Neacsu" <fneacsu2@gmail.com> wrote in message <imtg3k$1h$1@fred.mathworks.com>...
> > "Liana" wrote in message <imtfao$g3c$1@fred.mathworks.com>...
> > > Hello All,
> > >
> > > I'd like to find out if there is some MATLAB built-in function used to determine the intersection between a triangle and a line. I need just to know if the line intersects the triangle - 1: intersects, 0: do not intersect.
> > >
> > > Thanks.
> >
> > Hi,
> >
> > I don't think there is a built in function for that. But you can write your own function.
> > There are a few ways you can do this, it depends if it's 2D, 3D, if you are dealing with a line or a segment ...
> >
> > Have a look here : http://softsurfer.com/Archive/algorithm_0106/algorithm_0106.htm#dist3D_Segment_to_Segment
> >
> > It's one method.
> >
> > Good luck.
> > Florin

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us