# Z-index of an arbitrary point on a flattened 3-dimensional triangle

I have a triangle in a 3-dimensional coordinate system that I want to draw to a screen. I'm able to flatten the triangle to 2 dimensions and determine whether an arbitrary point on the screen falls within the flattened object. What I don't know how to do is determine the Z-index of that point.

As an example, let's say the triangle is bounded by the points $(2, 2, 0)$, $(2, 8, 5)$, and $(8, 5, 10)$ (where higher Z values are behind lower ones, in case that's not clear) and mapped to a $10 \times 10$ screen. The flattened points, according to the system I'm using, are $(2, 2)$, $(3, 7)$, and $(6.5, 5)$. The arbitrary point $(5, 3)$ falls within this flattened triangle; how can I determine the Z-index of that point on the original triangle?

-

You can calculate the barycentric coordinates of the point (as described by Wikipedia) and then calculate the $z$ index as the weighted average of the $z$ indices of the vertices, with the barycentric coordinates as weights.