Code covered by the BSD License  

Highlights from
inside triangle

Be the first to rate this file! 8 Downloads (last 30 days) File Size: 1.51 KB File ID: #22690

inside triangle

by Nassim Khaled

 

15 Jan 2009

inside_triangle is used to check if a point P is inside the triangle P1P2P3 or not.

| Watch this File

File Information
Description

%inside_triangle is used to check if a point P is inside
%the triangle P1P2P3 or not.
%
%Inputs: P, P1, P2 and P3 are vectors of length 2 or three of the
% form [x y z] or [x y]
%
%Output: True
%True=1 => P is on or inside P1P2P3
%True=0 => P is outside P1P2P3
%
%Example:
%True=inside_triangle([0.5 0.5],[0 0],[0 2],[2 0]);
%
%The following algorithm is implemented
% If P is ON or INSIDE the triangle
%
% Area(PP1P2) + Area(PP2P3) + Area(PP3P1) = Area(P1P2P3)
%
% If P is OUTSIDE then,
%
% Area(PP1P2) + Area(PP2P3) + Area(PP3P1) > Area(P1P2P3)
%
% Area of a triangle can be found using the determinant:
%
% Area = abs(1/2 * |x1 y1 1| )
% |x2 y2 1|
% |x3 y3 1|
%

MATLAB release MATLAB 7.0.1 (R14SP1)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (2)
22 Aug 2011 seda

I tried to use this function, but, it does not work for all test, for example
True=inside_triangle([25 20],[20 10],[25 30],[30 20]), basically it gives correct result that sum:75, area:75, but it gives true=0

or for another example;
True=inside_triangle([28.76 41.00],[28.7060 40.7020],[29.0592 41.0657],[28.7730 41.1810]) these values are lat. and lon. in degrees, and are in the triangle normally, but function gives True=0
What is the reason of that??

Seda Yelkenci

02 Nov 2011 Oren

Looks like it will work.
Is it needed to input in specific order?
For example:
P = [2 3]
P1 = [0 0]
P2 = [-5 10]
P3 = [5 10]
so:
True=inside_triangle(P,P1,P2,P3)
or
True=inside_triangle(P,P2,P1,P3)
?

Please login to add a comment or rating.
Tag Activity for this File
Tag Applied By Date/Time
triangle Nassim Khaled 15 Jan 2009 16:35:42
plane Nassim Khaled 15 Jan 2009 16:35:42
inside Nassim Khaled 15 Jan 2009 16:35:42
point Nassim Khaled 15 Jan 2009 16:35:42
geometry Nassim Khaled 15 Jan 2009 16:35:42
area Nassim Khaled 15 Jan 2009 16:35:42
mathematics Nassim Khaled 15 Jan 2009 16:35:42

Contact us at files@mathworks.com