Given the triangle with vertices (a,b,c):
c / \ / \ / \ a - - - - b
Which is then subdivided into four triangles by halving each of the edges:
c / \ / \ ca / \ bc _ _ _ /\ /\ / \ / \ / \ / \ a - - - - ab - - - -b
Which results in four triangles (a, ab, ca), (b, bc, ab), (c, ca, bc), (ab, bc, ca).
Now given a point p. How do I determine in which triangle p lies, given that p is within the outer triangle (a, b, c)?
Currently I intend to use ab as the origin. Check whether it is to the left of right of the line "ca - ab" using the perp of "ca - ab" and checking the sign against the dot product of "ab - a" and the perp vector and the vector "p - ab". If it is the same or the dot product is zero then it must be in (a, ab, ca)... Continue with this procedure with the other outer triangles (b, ba, ab) & (c, ca, ba). In the end if it didn't match with these it must be contained within the inner triangle (ab, bc, ca).
Is there a better way to do it?
EDIT
Here is a little more info of the intended application of the algorithm:
I'm using this as a subdivision mask to generate a fine mesh over which I intend to interpolate. Each of the开发者_高级运维 triangles will be subdivided similarly up to a specified depth. I want to determine the triangle (at the maximum depth) within which the point p lies. With this I can evaluate a function at the point p using interpolation over the triangle. There is a class of triangles which is right-angled and they do comprise a significant portion, but they're much easier to work with and this algorithm isn't intended for them.
If the point is above ca/bc (i.e. in the top grey triangle) it's easy.
If the point is left of ca (i.e. in the left grey triangle) it's easy.
If the point is right of bc (i.e. in the right grey triangle) it's easy.
If the point is in the middle, all you have to do is determine if the point is above or below the black V.
You can do that by calulcating the y value of the line for the x value of the point and compare the result to the y value of the point.
if (y' > (y * x') / x) { // center triangle } else { // right triangle }
Is this the most efficient method? I don't know.
Is this an equilateral triangle? If so, then:
Let's say the height of your triangle is H. Use the distance formula to compute the distance from c to p. If d(c,p) < H/2, p is in the top triangle. If d(a,p) < H/2, p is in the left triangle. If d(b,p) < H/2, p is in the right triangle.
If none of the above are true, p is in the center triangle.
If your triangle isn't equilateral, then disregard my answer.
精彩评论