Build 10 classic Flash games and learn game development along the way with this ultra-fast paced game development course.

If you love this blog, this is the book for you.

Get the source code of 12 commercial Flash games, which have been loaded more than 50 million times!

Learn from real world successful examples.

Get it now

Box2D for Flash Games teaches you how to make Flash physics games from scratch with the most advanced features.

Create the new Flash game smashing hit.

# Algorithm to determine if a point is inside a triangle with mathematics (no hit test involved)

A couple of months ago I published an algorithm to determine if a point is inside a square with mathematics (no hit test involved).

Are you ready for some more maths? This time I am publishing an algorithm to determine if a point is inside a triangle.

Although checking if a point is inside “something” may seem just a programming exercise, during next days I will show you how many awesome things you can do with it.

The idea is simple. Given an ABC triangle, every side cuts the plane in two. We can say a point is inside a triangle when its position in all three planes is on the same side of the triangle, or when it’s not in any plane on the opposite side of the triangle.

Look at this picture:

We can say a point is inside the triangle when it’s outside of plane AB, outside plane BC and outside plane CA, or inside “rest of the world”-plane AB, inside “rest of the world”-plane BC and inside “rest of the world”-plane CA.

This is what we are going to do:

And this is the result: click the mouse to draw a random triangle, move the mouse inside the triangle to turn it red.

Rate this post: (21 votes, average: 4.95 out of 5)
Flash Templates provided by Template Monster are pre-made web design products developed using Flash technology.
They can be easily customized to meet the unique requirements of your project.

## This post has 14 comments

1. ### kartofelek

on June 18, 2012 at 12:33 pm

Sometimes this algoritm draw a “dual line” and dont detect mouse pointer.

2. ### Antoan

on June 18, 2012 at 12:54 pm

I believe this way is faster. It is my interpretation of using a determinant in a 3×3 matrix.

function isInside(p1:Point, p2:Point,p3:Point):Boolean
{
return (p1.x*(p2.y-p3.y)+p2.x*(p3.y-p1.y)+p3.x*(p1.y-p2.y) >= 0);
}

Your vertices should be given in clockwise order (or was it anti-clockwise? I don’t remember)

3. ### senthil

on June 18, 2012 at 1:42 pm

Thanks for the simple math logic! Is this only for inside or on the triangle also?

4. ### Pierre Chamberlain

on June 18, 2012 at 3:13 pm

@Antoan, where’s the 4th point that refers to the user’s location / point that needs to be analysed within the triangle?

@Emanuele, wouldn’t the sign(…):int method perform faster if you did a conditional check if the number is above 0 = return 1, under 0 = return -1, else = 0? I haven’t done any tests myself, but something tells me calling Math.abs(…) is slower than a quick check of greater-than / less-than.

5. ### Rahyar

on June 18, 2012 at 10:07 pm

It was very interesting. Thanks

6. ### Ben Reynolds

on June 19, 2012 at 2:38 am

Awesome, I’d never thought of that!

7. ### Algorithm to determine if a point is inside a triangle with mathematics (no hit test involved) – Emanuele Feronato « eaflash

on June 19, 2012 at 9:31 am

8. ### Antoan

on June 26, 2012 at 8:13 pm

@Pierre, Oops, my bad, that actually just finds the determinant, this is the actual code:

function det(p1:Point, p2:Point,p3:Point):Number{
return p1.x*(p2.y-p3.y)+p2.x*(p3.y-p1.y)+p3.x*(p1.y-p2.y);
}

function isInside(p:Point, p1:Point, p2:Point, p3:Point):Boolean{
return det(p, p1, p2) >=0 && det(p, p2, p3) >= 0 && det(p, p3, p1) >= 0;
}

9. ### steve anson

on June 28, 2012 at 10:27 am

excellent explanation of math involved here, using 2 techniques, cross product and barycentric coordinates

http://www.blackpawn.com/texts/pointinpoly/default.html

10. ### Kate

on June 30, 2012 at 2:39 am

IS this code released under a specific license? It would be helpful if you release it under some license so it can be used widely.

11. ### Emanuele Feronato

on July 2, 2012 at 12:23 am

Kate, use it as you want :)

12. ### Zuze

on July 7, 2012 at 7:54 pm

+1. Good job!

13. ### Bruno Berti

on July 13, 2012 at 3:12 pm

Thanks for the excellent explanation!

I’m making a game where the player needs to create various types of triangles. By relative lengths of sides: scalene, isosceles and equilateral, and by internal angles: acute, obtuse and right. Do you have a tip for the calculation of differentiation of these triangles?

14. ### Sank

on September 14, 2012 at 8:55 am

@Antoan Thanks for the formula, it helped and it’s ‘clockwise’ :)