Navigation 
 Home 
 Search 
 Site map 

 Contact Graeme 
 Home 
 Email 
 Twitter

 Skip Navigation LinksMath Help > Geometry > Point And Triangle > Point And Triangle

JavaScript Program
<script language="JavaScript"><!--
function calc() {
if (fAB()*fBC()>0 && fBC()*fCA()>0) {
   document.form1.out.value = "Inside"}
else {
   document.form1.out.value = "Not Inside"}
}
function fAB() {
return (eval((document.form1.y.value-document.form1.y1.value)*
(document.form1.x2.value-document.form1.x1.value) -
(document.form1.x.value-document.form1.x1.value)*
(document.form1.y2.value-document.form1.y1.value)))
}
function fBC() {
return (eval((document.form1.y.value-document.form1.y2.value)*
(document.form1.x3.value-document.form1.x2.value) -
(document.form1.x.value-document.form1.x2.value)*
(document.form1.y3.value-document.form1.y2.value)))
}
function fCA() {
return (eval((document.form1.y.value-document.form1.y3.value)*
(document.form1.x1.value-document.form1.x3.value) -
(document.form1.x.value-document.form1.x3.value)*
(document.form1.y1.value-document.form1.y3.value)))
}
// --></script>

And here's what this algorithm does:

Type the x and y values for a point P, and a triangle A, B, C:

Point P (,)

Triangle A (,),  B (,),  C (,)

   Point P is  the triangle.

So far, you've seen this algorithm in REXX and JavaScript.  Would you like to see it in Excel?  If you have Microsoft Excel (The Office 98 or later version) installed on your computer, you can click here to download the spreadsheet itself.  It has a nifty feature: It includes a graph that depicts the triangle and the point in addition to giving the result of the algorithm.  Enjoy!

DukeW123 pointed out a simpler algorithm than mine.  Click here to see it.

Also, I found on web page http://steve.hollasch.net/cgindex/math/barycentric.html a method using barycentric coordinates:

A fail-proof method is to compute the barycentric coordinates. For a triangle {(x1,y1), (x2,y2), (x3,y3)} and some point (x0,y0), calculate

b0 =  (x2 - x1) * (y3 - y1) - (x3 - x1) * (y2 - y1)
b1 = ((x2 - x0) * (y3 - y0) - (x3 - x0) * (y2 - y0)) / b0 
b2 = ((x3 - x0) * (y1 - y0) - (x1 - x0) * (y3 - y0)) / b0
b3 = ((x1 - x0) * (y2 - y0) - (x2 - x0) * (y1 - y0)) / b0 

Then if b1, b2, and b3 are all > 0, (x0,y0) is strictly inside the triangle; if bi = 0 and the other two coordinates are positive, (x0,y0) lies on the edge opposite (xi,yi); if bi and bj = 0, (x0,y0) lies on (xk,yk); if bi < 0, (x0,y0) lies outside the edge opposite (xi,yi); if all three coordinates are negative, something else is wrong. This method does not depend on the cyclic order of the vertices.

Return to the first page of this "Point and Triangle" topic.

Internet references

a method using barycentric coordinates: http://steve.hollasch.net/cgindex/math/barycentric.html 

Related pages in this website

Back to Triangles

Barycentric Coordinates -- if they're all between 0 and 1, then the point is inside the triangle

 

The webmaster and author of this Math Help site is Graeme McRae.