# XNA Calculate two points of triangle with given Z

I want to calculate the two points of a 3D triangle that have a specified Z coordinate. I guess the way to do this would be to somehow create a plane which is perfectly flat with my given Z coordinate and then calculate where the triangle intersects with it (or is this wrong).

If you know how to do this in any way please help. I have searched for but have not really found anything that seems to work.

• I don't think you have enough information to find the 3rd point. I believe you'll also need (at least one of) the angles of the corners adjacent to the missing point. – MichaelHouse Sep 19 '13 at 3:54
• I think I have enough info, I have the 3 Vector3 points of a triangle and I just want to calculate a linesegment of where this triangle intersects with a "plane" (As in a flat surface with a given Z coordinate). This surface could also possibly be defined as a rectangle... – Gerharddc Sep 19 '13 at 4:00
• I see, I read the question as you had two points and were trying to find the third based off a plane. My mistake. – MichaelHouse Sep 19 '13 at 4:16

## 2 Answers

Here is a method that I hope is intuitive enough, assuming z0 is the Z value of your intersection plane:

• start with an empty list of points L
• if (A.z - z0) * (B.z - z0) <= 0, it means AB intersects the plane:
• compute u = (B.z - z0) / (B.z - A.z)
• add point B + u * (A - B) to list L
• if (A.z - z0) * (C.z - z0) <= 0:
• compute u = (C.z - z0) / (C.z - A.z), add point C + u * (A - C) to list L
• if (B.z - z0) * (C.z - z0) <= 0:
• compute u = (C.z - z0) / (C.z - B.z), add point C + u * (B - C) to list L

At the end, if L contains two points, that's your segment; otherwise, either the triangle does not intersect or it's a degenerate case. Make sure you check for possible divisions by zero.

• This seems interesting, but I am not sure about the variables, I guess A B and C are the points of the triangle, but how can I multiply a value with them and where does the target z coordinate fit into all of this? – Gerharddc Sep 20 '13 at 10:10
• @Gerhman Yes, A B and C are the triangle points; multiplying a point with a value k consists in multiplying each of its x y and z components with k. Depending on the framework you use, there might already be a function or operator which does it for you. I assumed you wanted the intersection with the z = 0 plane but I’ll update my answer so it works with an arbitrary z0 value. – sam hocevar Sep 20 '13 at 20:14

You could just du a ray / plane intersection with two rays, after deciding how the triangle intersects with the plane (To intersect at all 1 point has to be on side A and two points on side B of the plane)

This is my standard 'way-over-the-top' ressource for intersections you can look at different implementations and pick the one best suiting your need.

I little less complex is this side, explaining what to do and providing a trianlge-ray algorithm in c (and a lot more under 'algorithms')