Determine if a point lies within area of a triangle
I need to write an algorithm to determine whether a point lies within the area of a triangle. (meaning on the inside) I developed a theory of a way that I think will work, but want to know if there is a better way to do this.
The way I came up with is to first organize the points in order of either highest to lowest, or farthest left to farthest right. Then find the equation of a line from the point to each vertex in the triangle. Then if that line lies between or on the other two vertices for all three of the lines, the point is inside the triangle. And if it lies outside (ie to the left or right or above or below) of the other two vertices on any of the lines, it is outside. I attached a picture to show what I mean.
In the left picture, you can see that the line going almost up and down lies to the right of the left vertex, and to the left of the right vertex, so it is good. The one with the negative slope below the right vertex, and above the middle vertex. and the line with the negative slope lies below the left vertex, and above the middle vertex.
In the second picture, the line going through the middle vertex lies below both the left and right vertex. The line going through the left vertex lies to the left of both the middle and right vertex.
I'll have to think about how to do the algorithm. Slope should be easy to figure out as I am given the locations of the vertices, and the point will be the origin, and I think I can use point-slope format for a line to see how it fares. But before I spend the energy, is there a better way to do this? Something simpler, or requiring fewer computations, or easier to see in general.
access the co ordinates of the triangle
hi...i have the same prob...i want to access al the co ordinates which lies inside the area of the triangle.i need a algo for that..plz help..thanks in advance