Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer 1

up vote 2 down vote accepted

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.

share|improve this answer
    
This worked perfectly! Thanks a ton for the information. –  Fraxtil Jul 22 '12 at 21:32
    
@Fraxtil: You're welcome! –  joriki Jul 22 '12 at 21:33
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.