# Calculate the Z of a line intersection given the XY coordinates?

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.

## 1 Answer

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.

• 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