Join the given point with all the vertices of the triangle and calculate the area of each of the three sub-triangles thus formed now compare the area of original triangle with the sum of the area of those 3 triangles if that comes out to be equal, then the point lies inside otherwise not.
On Fri, Jul 29, 2011 at 1:34 AM, Don <[email protected]> wrote: > // True if point (x,y) is above line defined by two points > // If line is vertical, "above" is defined as to the right > bool above(double lx1, double ly1, double lx2, double ly2, double x, > double y) > { > return (lx1 != lx2) ? (y > (ly1+(x-lx1)*(ly2-ly1)/(lx2-lx1))) : (x > > lx1); > } > > // True if point 1 and point 2 are on the same side of the line > defined by l1 and l2 > bool sameSide(double lx1, double ly1, double lx2, double ly2, double > px1, double py1, double px2, double py2) > { > return above(lx1, ly1, lx2, ly2, px1, py1) == above(lx1, ly1, lx2, > ly2, px2, py2); > } > > // True if point (x,y) is inside triangle > bool pointInTriangle(double x1, double y1, double x2, double y2, > double x3, double y3, double x, double y) > { > return sameSide(x1,y1,x2,y2,x,y,x3,y3) && > sameSide(x1,y1,x3,y3,x,y,x2,y2) && > sameSide(x2,y2,x3,y3,x,y,x1,y1); > } > > -- > You received this message because you are subscribed to the Google Groups > "Algorithm Geeks" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]. > For more options, visit this group at > http://groups.google.com/group/algogeeks?hl=en. > > -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
