2010-01-12 14:25:49 8 Comments

Is there an easy way to determine if a point is inside a triangle? It's 2D, not 3D.

### Related Questions

#### Sponsored Content

#### 30 Answered Questions

### [SOLVED] Is floating point math broken?

**2009-02-25 21:39:02****Cato Johnston****335112**View**3119**Score**30**Answer- Tags: math language-agnostic floating-point floating-accuracy

#### 48 Answered Questions

#### 54 Answered Questions

### [SOLVED] How do I check if an array includes a value in JavaScript?

**2008-10-25 22:14:40****brad****2683184**View**4133**Score**54**Answer- Tags: javascript arrays algorithm time-complexity javascript-objects

#### 35 Answered Questions

### [SOLVED] Determine Whether Two Date Ranges Overlap

**2008-11-28 14:48:35****Ian Nelson****430211**View**1300**Score**35**Answer- Tags: datetime math language-agnostic

#### 5 Answered Questions

### [SOLVED] How do I determine whether my calculation of pi is accurate?

**2013-01-11 17:15:32****Ishan Sharma****119980**View**773**Score**5**Answer- Tags: algorithm math language-agnostic pi

#### 20 Answered Questions

### [SOLVED] How do CSS triangles work?

**2011-08-16 03:54:51****Stanislav Shabalin****171656**View**1879**Score**20**Answer- Tags: css geometry polygon css-shapes

#### 36 Answered Questions

### [SOLVED] How can I pair socks from a pile efficiently?

**2013-01-19 15:34:35****amit****414926**View**3946**Score**36**Answer- Tags: algorithm sorting language-agnostic matching

#### 35 Answered Questions

### [SOLVED] Fastest way to determine if an integer's square root is an integer

**2008-11-17 13:43:21****Kip****280420**View**1490**Score**35**Answer- Tags: java math optimization perfect-square

#### 14 Answered Questions

### [SOLVED] What is the optimal algorithm for the game 2048?

**2014-03-12 05:37:21****nitish712****944563**View**1935**Score**14**Answer- Tags: algorithm logic artificial-intelligence 2048

## 25 comments

## @Shaon Majumder 2018-08-08 14:10:14

This is the simplest concept to determine if a point is inside or outside the triangle or on an arm of a triangle.

Determination of a point is inside a triangle by determinants:

The simplest working code:

## @Joseph Merdrignac 2017-12-08 23:11:28

Since there's no JS answer,

Clockwise & Counter-Clockwise solution:

EDIT: there was a typo for det computation (

`cy - ay`

instead of`cx - ax`

), this is fixed.https://jsfiddle.net/jniac/rctb3gfL/

I'm using here the same method as described above: a point is inside ABC if it is respectively on the "same" side of each line AB, BC, CA.

## @xApple 2018-07-23 12:34:42

I tired this code and it doesn't work. It always returns False.

## @Joseph Merdrignac 2018-07-24 16:16:40

hmmm... you probably made a mistake. Here's a fiddle with that function running : jsfiddle.net/jniac/rctb3gfL

## @Joseph Merdrignac 2018-07-24 16:59:55

i've seen your Python response, we are using the same method, if i use one more line (

`let det = (bx - ax) * (cy - ay) - (by - ay) * (cy - ay)`

), this is to determine the triangle winding order, so the method will works with CW & CCW triangles (see jsFiddle).## @Joseph Merdrignac 2018-07-24 19:46:03

hm i made a mistake, i wrote:

`let det = (bx - ax) * (cy - ay) - (by - ay) * (cy - ay)`

instead of`let det = (bx - ax) * (cy - ay) - (by - ay) * (cx - ax)`

so this is fixed, thanks for reporting## @Maxim Kamalov 2014-04-20 18:36:43

I needed point in triangle check in "controlable environment" when you're absolutely sure that triangles will be clockwise. So, I took

Perro Azul's jsfiddle and modified it as suggested bycoprocfor such cases; also removed redundant 0.5 and 2 multiplications because they're just cancel each other.http://jsfiddle.net/dog_funtom/H7D7g/

Here is equivalent C# code for Unity:

## @ravi tanwar 2019-02-03 05:41:39

One of the easiest ways to check if the area formed by the vertices of triangle (x1,y1),(x2,y2),(x3,y3) is positive or not.

Area can by calculated by the formula:

or python code can be written as:

## @Pawel 2016-05-13 23:34:09

Supposedly high-performance code which I adapted in JavaScript (article below):

`pointInTriangle(p, p0, p1, p2)`

- for counter-clockwise triangles`pointInTriangle(p, p0, p1, p2)`

- for clockwise trianglesLook in jsFiddle (performance test included), there's also winding checking in a separate function. Or press "Run code snippet" below

Inspired by this: http://www.phatcode.net/articles.php?id=459

## @John Bananas 2012-03-18 01:22:31

I wrote this code before a final attempt with Google and finding this page, so I thought I'd share it. It is basically an optimized version of Kisielewicz answer. I looked into the Barycentric method also but judging from the Wikipedia article I have a hard time seeing how it is more efficient (I'm guessing there is some deeper equivalence). Anyway, this algorithm has the advantage of not using division; a potential problem is the behavior of the edge detection depending on orientation.

In words, the idea is this: Is the point s to the left of or to the right of both the lines AB and AC? If true, it can't be inside. If false, it is at least inside the "cones" that satisfy the condition. Now since we know that a point inside a trigon (triangle) must be to the same side of AB as BC (and also CA), we check if they differ. If they do, s can't possibly be inside, otherwise s must be inside.

Some keywords in the calculations are line half-planes and the determinant (2x2 cross product). Perhaps a more pedagogical way is probably to think of it as a point being inside iff it's to the same side (left or right) to each of the lines AB, BC and CA. The above way seemed a better fit for some optimization however.

## @surgemcgee 2019-05-08 17:14:42

This test is about 140-180% faster than the first one provided (thanks to you both btw :). I ran the code here: paste.ubuntu.com/p/k5w7ywH4p8 using the nodejs v8 engine with optimizations disabled and got the following results: :w !node -p --minimal test1: 114.852ms test2: 64.330ms test1: 115.650ms test2: 63.491ms test1: 117.671ms test2: 65.353ms test1: 119.146ms test2: 63.871ms test1: 118.271ms test1: 118.670ms test2: 63.352ms

## @xuiqzy 2020-05-23 11:27:12

@surgemcgee why would you run it without optimizations? Isn't that more removed from reality then?

## @surgemcgee 2020-05-24 12:40:10

@xuiqzy Well, my program contains the two different solutions. I have yet to administer the fastest method of doing it. Perhaps that comment should be removed and replaced with my completed works regarding this..

## @Glenn Slayden 2013-12-31 17:34:51

C#version of the barycentric method posted by andreasdr and Perro Azul. Note that the area calculation can be avoided if`s`

and`t`

have opposite signs. I verified correct behavior with a pretty thorough unit test._{[edit]}^{accepted suggested modification by @Pierre; see comments}## @Luke Dupin 2018-11-20 19:01:26

The solution with the ending if statement works for clockwise and counter clockwise triangle points.

## @Glenn Slayden 2019-11-20 01:46:00

@LukeDupin Not sure I understand your comment. This answer works as posted for any supplied ordering of the 3 points.

## @Adam Gawne-Cain 2014-08-17 06:22:25

Java version of barycentric method:

The above code will work accurately with integers, assuming no overflows. It will also work with clockwise and anticlockwise triangles. It will not work with collinear triangles (but you can check for that by testing det==0).

The barycentric version is fastest if you are going to test different points with the same triangle.

The barycentric version is not symmetric in the 3 triangle points, so it is likely to be less consistent than Kornel Kisielewicz's edge half-plane version, because of floating point rounding errors.

Credit: I made the above code from Wikipedia's article on barycentric coordinates.

## @Alex Byrth 2017-06-21 18:53:22

Nice ! It can even improved to use javax.vecmath's Point3f / Point2f tuples, in order to better handle the data input.

## @Cédric Dufour 2015-12-04 17:08:58

By using the analytic solution to the barycentric coordinates (pointed out by

Andreas Brinck) and:coprocandThomas Eding)One can minimize the number of "costly" operations:

Code can be pasted in

Perro Azuljsfiddle or try it by clicking "Run code snippet" belowLeading to:

This compares quite well with

Kornel Kisielewiczsolution (25 recalls, 1 storage, 15 subtractions, 6 multiplications, 5 comparisons), and might be even better if clockwise/counter-clockwise detection is needed (which takes 6 recalls, 1 addition, 2 subtractions, 2 multiplications and 1 comparison in itself, using the analytic solution determinant, as pointed out byrhgb).## @Jack D'Aurizio 2016-08-06 21:13:53

Nice solution. I think is is quite equivalent to my last approach here on MSE: math.stackexchange.com/questions/51326/…

## @Giovanni Funchal 2016-11-28 17:51:05

I just tested the code as is, and it doesn't work for me (example p -4.69317198, -6.99191951 p0 -7.05846786 0.596718192 p1 -6.8703599 -2.36565161 p2 -4.69317198, -6.99191951)

## @Cédric Dufour 2016-12-14 13:15:32

@GiovanniFunchal Strange, your example works for me, both in the jsfiddle (replace the initial "point" and "triangle" definitions) and my local Python implementation. Numeric precision issues (try stripping some decimals) ?

## @Eyal 2017-05-29 08:13:54

Your seems the fastest in my test: jsfiddle.net/eyal/gxw3632c/27 . The difference between all the methods is quite small, though.

## @devnullicus 2020-03-02 21:59:39

Try triangle (-1,-1), (1,-1), (0,1) and point (0,-1). Returns false when it should return true because s(2) + t(2) > d (2). Something wrong with the math on edges of the triangle, it seems, as the point p is right on the border between p0 and p1 and it's not a simple matter of converting a < to a <= or something like that.

## @devnullicus 2020-03-03 00:33:46

Acually, after further study, it DOES appear that it can be easily fixed. Changing the last line of ptInTriangle to "return s >= 0.0 && t >= 0.0 && (s + t) <= 2.0 * A * sgn" seems to work.

## @Developer 2014-01-06 11:32:43

Here is an efficient

Pythonimplementation:and an example output:

## @ThorSummoner 2020-04-08 05:00:19

I haven't been able to to make this work, for example for the point in the triangle [(0,0), (3,0), (3,4)], neither points (1,1) or (0,0) test positive. I tried with both clockwise and ant-clockwise triangle points.

## @Ramiro R.C. 2017-09-25 16:13:20

Other function in

python, faster thanDeveloper's method(for me at least) and inspired byCédric Dufoursolution:You can test it with:

It takes a lot to plot, but that grid is tested in 0.0195319652557 seconds against 0.0844349861145 seconds of

Developer's code.Finally the code comment:

## @Code Pope 2019-05-15 12:35:40

This function is not working. Give

`ptInTriang([11,45],[45, 45],[45, 45] ,[44, 45])`

and it will return`true`

although it is false## @Ajay Anand 2017-04-20 19:52:19

It can not be more efficient than this! Each side of a triangle can have independent position and orientation, hence three calculations: l1, l2 and l3 are definitely needed involving 2 multiplications each. Once l1, l2 and l3 are known, result is just a few basic comparisons and boolean operations away.

## @xApple 2018-07-23 12:57:41

Here is a solution in python that is efficient, documented and contains three unittests. It's professional-grade quality and ready to be dropped into your project in the form of a module as is.

There is an additional optional graphical test for the algorithm above to confirm its validity:

Producing the following graphic:

## @QuestionFeed 2018-02-12 13:00:15

almost perfect Cartesian coordinates converted from barycentric are exported within *v (x) and *w (y) doubles. Both export doubles should have a * char before in every case, likely: *v and *w Code can be used for the other triangle of a quadrangle too. Hereby signed wrote only triangle abc from the clockwise abcd quad.

the o point is inside ABC triangle for testing with with second triangle call this function CDA direction, and results should be correct after

`*v=1-*v;`

and`*w=1-*w;`

for the quadrangle## @Kornel Kisielewicz 2010-01-12 14:27:02

In general, the simplest (and quite optimal) algorithm is checking on which side of the half-plane created by the edges the point is.

Here's some high quality info in this topic on GameDev, including performance issues.

And here's some code to get you started:

## @Kornel Kisielewicz 2010-01-12 14:51:22

It's commonly used in 2D. Barycentric coordinates tend to confuse people. Also, given the cooridates of the triangle, and the point cordinate, I'm unsure about the efficiency of using barycentrics.

## @Andreas Brinck 2010-01-12 15:09:44

@Kornel The barycentric version is more efficient in 2D as well. Your solution also has the problem that it will report a different result for points exactly on the edges of the triangle depending on wether the triangle is specified in clockwise or counter clockwise order.

## @user252020 2010-01-16 04:57:47

For my purposes (the reason I found this site) the original answer proposed by Kornel Kisielewicz is much more efficient. I'm working with an LCD display with BYTE size coordinates and a very typical microprocessor where integer multiply is a very fast instruction, and division is much, much, slower. Numeric issues are also much smaller, due to no division! all calculations are exact. Thanks, Rick

## @David Doria 2013-03-25 15:29:24

So the sign() function tells you which side of the halfplane (formed by the line between p2 and p3) p1 is?

## @Thash 2016-09-09 15:28:09

Note that if you assume some order of the vertices (say counter clockwise), you don't need to calculate all of those determinants all the time. In fact in the best case, 1 determinant is enough to find that the point is not inside the triangle.

## @user3289695 2016-10-14 10:53:31

Thank you! I made a JavaScript version of your code, and I don't know why or how it works, but it does! It has been very useful to me with an HTML5 canvas on which I draw a set of triangles.

## @Bernhard 2016-12-19 12:19:24

In relation to this answer, I found this question on Math.SE useful: math.stackexchange.com/questions/51326/…

## @Morris Franken 2017-05-16 21:20:27

Be aware though, this function will always return

`true`

if`v1`

,`v2`

and`v3`

are equal.## @Eyal 2017-05-29 07:30:09

Math.sign is very slow. Better to change the last line to: return b1*b2>0 && b2*b3>0; Then remove all calls to sign.

## @Yves Daoust 2018-04-29 09:08:24

@AndreasBrinck: you are missing that the barycentric approach requires the computation of the same 2x2 minors. But what's more, it performs a division by the 3x3 system determinant, which is useless, costly and numerically dangerous. And saying the "this does not belong to the algorithm" is wrong. Nothing beats the orientation test.

## @Redu 2016-12-03 17:59:01

Honestly it is as simple as Simon P Steven's answer however with that approach you don't have a solid control on whether you want the points on the edges of the triangle to be included or not.

My approach is a little different but very basic. Consider the following triangle;

In order to have the point in the triangle we have to satisfy 3 conditions

In this method you have full control to include or exclude the point on the edges individually. So you may check if a point is in the triangle including only the |AC| edge for instance.

So my solution in JavaScript would be as follows;

## @andreasdr 2013-01-17 15:42:17

I agree with

Andreas Brinck, barycentric coordinates are very convenient for this task. Note that there is no need to solve an equation system every time: just evaluate the analytical solution. UsingAndreas' notation, the solution is:where

`Area`

is the (signed) area of the triangle:Just evaluate

`s`

,`t`

and`1-s-t`

. The point`p`

is inside the triangle if and only if they are all positive.EDIT: Note that the above expression for the area assumes that the triangle node numbering is counter-clockwise. If the numbering is clockwise, this expression will return a negative area (but with correct magnitude). The test itself (

`s>0 && t>0 && 1-s-t>0`

) doesn't depend on the direction of the numbering, however, since the expressions above that are multiplied by`1/(2*Area)`

also change sign if the triangle node orientation changes.EDIT 2: For an even better computational efficiency, see

coproc's comment below (which makes the point that if the orientation of the triangle nodes (clockwise or counter-clockwise) is known beforehand, the division by`2*Area`

in the expressions for`s`

and`t`

can be avoided). See alsoPerro Azul's jsfiddle-code in the comments underAndreas Brinck's answer.## @Andreas Brinck 2013-01-18 15:36:30

That

issolving the equation system :)## @andreasdr 2013-01-19 15:43:46

Yes, my point is that any criticism of your method based on the computational cost of solving the equation system is unfounded, since that doesn't have to be done as part of the algorithm.

## @coproc 2013-02-04 21:20:56

The efficiency can be improved by not dividing through

`2*Area`

, i.e. by calculating`s´=2*|Area|*s`

and`t´=2*|Area|*t`

(if the orientation of the points - clockwise or counter-clockwise - is not known, the sign of`Area`

has to be checked, of course, but otherwise it maybe does not even need to be computed), since for checking`s>0`

it suffices to check`s´>0`

. And instead of checking`1-s-t>0`

it suffices to check`s´+t´<2*|Area|`

.## @rhgb 2014-01-16 16:42:13

I may add that if

`p0->p1->p2`

iscounter-clockwiseinCartesian(which is usuallyclockwiseinscreen coordinates), the`Area`

calculated by this method will be positive.## @user2600366 2016-09-30 12:25:51

i wonder, what does it mean clockwise using 3 coordinates, do u mean that p0 is the highest, ? can u please explain?

## @andreasdr 2016-10-02 08:19:29

@user2600366 When you travel along the boundary of the triangle in the direction p0 -> p1 -> p2 -> p0, and so on, you will have the interior of the triangle either always to your right or always to your left. In the former case, the numbering is clockwise, in the latter case, it is counter-clockwise.

## @rbaleksandar 2016-10-17 15:24:10

@user2600366 While Barycentric is awesome there are other options such as the one that completely depends on the clockwise-orietnation of the vertices (which defines the winding of the triangle). I forgot its name but basically you check whether the point in on the left (inside) of each edge of the triangle (where right means outside). If you screw up the winding of the triangle you end up with nonsense. There you can see how the orientation plays role and what it means. This answer as well as the comments are great and extremely helpful! Also check Realtime Collision Detection book.

## @Tyler Kelly 2018-10-04 22:17:11

can someone further explain how to handle unknown orientation + handling negative coordinates?

## @user6839822 2018-10-31 14:04:39

@edit 2: if you leave out the division by 2*Area then the check on 1-s-t>0 will be true for points outside of the area. So the factor 1/(2*Area) actually is important!

## @andreasdr 2019-01-10 11:50:33

The reason that you can avoid division by 2*Area if you know the orientation of the points is that in that case, you know the sign of Area. Looking at both cases: For clockwise / counter-clockwise orientation, Area>0 / Area < 0, so the factor 1/(2*Area) preserves / flips the sign of the three expressions. Thus, the point is within the triangle if all the expressions are positive / negative.

## @Bela Bessenyei 2016-08-18 18:21:56

The easiest way and it works with all types of triangles is simply determine the angles of the P point A, B , C points angles. If any of the angles are bigger than 180.0 degree then it is outside, if 180.0 then it is on the circumference and if acos cheating on you and less than 180.0 then it is inside.Take a look for understanding http://math-physics-psychology.blogspot.hu/2015/01/earlish-determination-that-point-is.html

## @Orup 2016-07-14 19:33:55

I just want to use some simple vector math to explain the barycentric coordinates solution which Andreas had given, it will be way easier to understand.

(1-s)

|v0v2| / |v0v2| = tp|v0v1| / |v0v1|we get 1 - s = tp, then 1 = s + tp. If any t > tp, which 1 < s + t where is on the double dash line, the vector is outside the triangle, any t <= tp, which 1 >= s + t where is on single dash line, the vector is inside the triangle.

Then if we given any s in [0, 1], the corresponding t must meet 1 >= s + t, for the vector inside triangle.

So finally we get v = s * v02 + t * v01, v is inside triangle with condition s, t, s+t belongs to [0, 1]. Then translate to point, we have

p - p0 = s * (p1 - p0) + t * (p2 - p0), with s, t, s + t in [0, 1]

which is the same as Andreas' solution to solve equation system p = p0 + s * (p1 - p0) + t * (p2 - p0), with s, t, s + t belong to [0, 1].

## @Yves Daoust 2018-04-29 09:02:13

You can just say that you use the local frame defined by the three vertices so that the sides become s=0, t=0 and s+t=1. The affine coordinate transformation is a well-known operation of linear algebra.

## @Pierre 2015-11-30 23:45:09

There are pesky edge conditions where a point is exactly on the common edge of two adjacent triangles. The point cannot be in both, or neither of the triangles. You need an arbitrary but consistent way of assigning the point. For example, draw a horizontal line through the point. If the line intersects with the other side of the triangle on the right, the point is treated as though it is inside the triangle. If the intersection is on the left, the point is outside.

If the line on which the point lies is horizontal, use above/below.

If the point is on the common vertex of multiple triangles, use the triangle with whose center the point forms the smallest angle.

More fun: three points can be in a straight line (zero degrees), for example (0,0) - (0,10) - (0,5). In a triangulating algorithm, the "ear" (0,10) must be lopped off, the "triangle" generated being the degenerate case of a straight line.

## @ihayet 2014-08-03 18:56:04

If you know the co-ordinates of the three vertices and the co-ordinates of the specific point, then you can get the area of the complete triangle. Afterwards, calculate the area of the three triangle segments (one point being the point given and the other two being any two vertices of the triangle). Thus, you will get the area of the three triangle segments. If the sum of these areas are equal to the total area (that you got previously), then, the point should be inside the triangle. Otherwise, the point is not inside the triangle. This should work. If there are any issues, let me know. Thank you.

## @Yves Daoust 2014-02-02 11:27:31

If you are looking for speed, here is a procedure that might help you.

Sort the triangle vertices on their ordinates. This takes at worst three comparisons. Let Y0, Y1, Y2 be the three sorted values. By drawing three horizontals through them you partition the plane into two half planes and two slabs. Let Y be the ordinate of the query point.

Costs two more comparisons. As you see, quick rejection is achieved for points outside of the "bounding slab".

Optionally, you can supply a test on the abscissas for quick rejection on the left and on the right (

`X <= X0' or X >= X2'`

). This will implement a quick bounding box test at the same time, but you'll need to sort on the abscissas too.Eventually you will need to compute the sign of the given point with respect to the two sides of the triangle that delimit the relevant slab (upper or lower). The test has the form:

The complete discussion of

`i, j, k`

combinations (there are six of them, based on the outcome of the sort) is out of the scope of this answer and "left as an exercise to the reader"; for efficiency, they should be hard-coded.If you think that this solution is complex, observe that it mainly involves simple comparisons (some of which can be precomputed), plus 6 subtractions and 4 multiplies in case the bounding box test fails. The latter cost is hard to beat as in the worst case you cannot avoid comparing the test point against two sides (no method in other answers has a lower cost, some make it worse, like 15 subtractions and 6 multiplies, sometimes divisions).

UPDATE: Faster with a shear transform

As explained just above, you can quickly locate the point inside one of the four horizontal bands delimited by the three vertex ordinates, using two comparisons.

You can optionally perform one or two extra X tests to check insideness to the bounding box (dotted lines).

Then consider the "shear" transform given by

`X'= X - m Y, Y' = Y`

, where`m`

is the slope`DX/DY`

for the highest edge. This transform will make this side of the triangle vertical. And since you know on what side of the middle horizontal you are, it suffices to test the sign with respect to a single side of the triangle.Assuming you precomputed the slope

`m`

, as well as the`X'`

for the sheared triangle vertices and the coefficients of the equations of the sides as`X = m Y + p`

, you will need in the worst case`X' = X - m Y`

;`X >< m' Y + p'`

against the relevant side of the sheared triangle.## @psiman 2012-10-29 23:28:23

What I do is precalculate the three face normals,

in 3D by cross product of side vector and the face normal vector.

in 2D by simply swapping components and negating one,

then inside/outside for any one side is when a dot product of the side normal and the vertex to point vector, change sign. Repeat for other two (or more) sides.

Benefits:

a lot is precalculated so great for multiple point testing on same triangle.

early rejection of common case of more outside than inside points. (also if point distribution weighted to one side, can test that side first.)

## @Andreas Brinck 2010-01-12 14:41:40

Solve the following equation system:

The point

`p`

is inside the triangle if`0 <= s <= 1`

and`0 <= t <= 1`

and`s + t <= 1`

.`s`

,`t`

and`1 - s - t`

are called the barycentric coordinates of the point`p`

.## @Daniel Rikowski 2010-01-12 14:51:53

This is faster than the half-plane check, but perhaps a little bit harder to grasp if you are new to barycentric coordinates.

## @Matthieu N. 2010-01-16 22:14:22

With trivial exits (not implemented) in Kornel's method, his can actually far more efficient than yours. If you actually try to compute s and t you'll know what I mean.

## @Andreas Brinck 2010-01-17 08:49:06

@Ben You can implement trivial exits in this version as well, for instance you can begin by calculating only

`s`

and exit if it isn't in the`[0 1]`

range. If the triangles can be both clockwise and counter clockwise you'll still have to calculate at least two signs in the halfplane version. Finally, how effective any trivial exits are depend on the input data.## @andreasdr 2013-01-17 15:45:11

Exactly three years later, here are my 2 cents: Just solve for s and t analytically first, and let the program evaluate those expressions. The point p is then in the triangle if and only if s, t and 1-s-t are all positive. It doesn't get more computationally efficient than that.

## @urraka 2013-04-09 01:01:23

I wanted to test this so I made a jsfiddle, relying on @andreasdr solution and coproc comment: jsfiddle.net/PerroAZUL/zdaY8/1

## @H'H 2013-08-23 15:30:56

Is this algorithm efficient for an unstructured mesh with lots of triangle elements?

## @Thomas Eding 2014-05-02 16:15:01

Optimization:

`s + t <= 1`

implies`s <= 1`

and`t <= 1`

if`s >= 0`

and`t >= 0`

.## @Flayn 2015-08-25 10:30:59

The article totologic.blogspot.fr/2014/01/… proposed by @Logic post helped me to better understand this solution

## @Eyal 2017-05-27 18:19:15

I found an implementation of barycentric in javascript that fails for a pont that is very far from the triangle due to floating point: jsperf.com/point-in-triangle and it fails with this triangle and point: {"a":{"x":-40.49496078491211,"y":-8.738582611083984},"b":{"x":-41.94913723574891,"y":-6.313193949870829},"c":{"x":-41.94775545887751,"y":-6.315498584878696} and point {x: 47.87142596679951, y: -6.500680708968048} Is there an accurate version?

## @Darkmoor 2017-11-08 14:27:23

For an intuitive explanation.

## @maaartinus 2018-02-28 23:16:28

@AndreasBrinck I can't see any support for your claim that the accepted answer is "pretty inefficient" when compared to yours. I'd bet, in fixed point it's more efficient. You wrote nothing about how you solve the equations system - it's trivial, until you consider possible numerical problems.

## @Jan Turoň 2018-03-21 15:57:04

I voted you down, Andreas, as you fail to understand the question. OP asked for

easyway, notefficient. You even comment on easy solution of Kornel and keep talking about efficiency.## @MannyC 2020-07-13 20:40:29

I just wanted to add a comment for people with difficulties grasping the barycentric coordinates. Taking these coordinates is like aligning our x axis with the side

`p1-p0`

and our y axis with`p2-p0`

. In this basis the triangle is a half square with vertices (0,0) (1,0) and (0,1). It's very easy too see the conditions on`s`

,`t`

and`s+t`

after one draws it.## @Simon P Stevens 2010-01-12 14:31:40

A simple way is to:

Two good sites that explain alternatives are:

blackpawn and wolfram

## @Kornel Kisielewicz 2010-01-12 14:35:03

Um, that method isn't exactly efficient, and is very prone to numerical errors...

## @Simon P Stevens 2010-01-12 14:38:40

It's quite the opposite, it's very inefficient :-) It's just one simple way though, that's easy to implement. Can you give an example of a numerical error this would cause?

## @Redu 2016-12-03 17:31:29

While to me this simply seems to be the best of all answers under this topic, i guess the points on the edges of the triangle are calculated to be included to the triangle and you haven't got a solid control on that.

## @lonewarrior556 2018-01-20 10:08:48

checking if it's exactly 2pi is numerically impossible given pi's irrational. However you just need to check if the angles add up to something greater than pi.