Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm intersecting a line in 2D and I calculate the X,Y coordinates of the intersection point. What I need is the Z of the intersection point given the X,Y,Z of the line points, and the X,Y of the intersection. From what I understand of equations it should be a one-liner but I don't know enough math to get there.

share|improve this question

1 Answer 1

up vote 7 down vote accepted

You can use either the x or the y value of the intersection to compute the z-value (using x):

t = (x - X1) / (X2 - X1)
z = Z1 + (Z2 - Z1) * t

Where x is the x-intersection. X1, X2 and Z1, Z2 are your known x and z-values, respectively, of the segment's endpoints.

share|improve this answer
    
I'd probably calculate 't' by checking that (X2-X1) is non-zero first, if it is you can use that, if it is zero though then switch to using the Y values. That'll ensure you don't run in to trouble on horizontal or vertical lines. –  Roger Perkins Feb 12 '11 at 22:36
    
That's a good point--it's probably the problem of not actually being a line segment in that case, though, as OP's choosing the xy plane for some presumably meaningful reason. But that's just speculation on a vague description. –  Drew Cummins Feb 12 '11 at 23:10

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.