1

Say I have a triangle in three dimensions, and I know the coordinates (x,y,z) of every point - how would I go about finding any point that lies inside of this triangle? Say I know the x and y of the point I want, how would I find the Z?

EDIT: alfasin's answers was closest to the solution - I used the three points to construct a plane equation

a(x-x0) + b(y-y0) + c(z-z0) = 0

where (a,b,c) is the normal vector and plugged in my values and solved for Z. Thanks for the help everyone.

user1118321
  • 25,567
  • 4
  • 55
  • 86
HiroLord
  • 13
  • 1
  • 5
  • For what application do you need this? You can project the Point to a plane (see [How to project a 3d point to a 3d plane](http://stackoverflow.com/questions/9605556/how-to-project-a-3d-point-to-a-3d-plane)) and check if the projected point is inside the triangle. This can be done using barycentric coordinates (see [this](http://stackoverflow.com/a/6075960/2344898)). – weeska Sep 17 '13 at 23:13
  • I have a triangle in 3D space and I want to be able to pick (x,y,z) where I am positive the x and y coordinates exist inside of this triangle. I then need to know, however, what the Z coordinate is at this Point. – HiroLord Sep 17 '13 at 23:21
  • You can only be certain that the point is 'inside' the triangle if you have some kind of projection. What you need is a Ray-Triangle intersection test. – weeska Sep 17 '13 at 23:29

2 Answers2

0

If your triangle is made of three points T1, T2, T3 and the point you're trying to find is P, you could do a cross product between lines:

(T1, T2) x (T1, P) = R1

(T2, T3) x (T2, P) = R2

(T3, T1) x (T3, P) = R3

If the resultant lines R1, R2, R3 are parallel, then I would assume the point is within the triangle. Could someone verify/disprove this?

[Edit]: Here's a resource to find if lines are parallel: https://math.stackexchange.com/questions/194242/decide-whether-two-lines-are-parallel

But the second part of your question implies that you're trying to MAKE a point fit into a triangle? Is this correct? Excuse the lack of commenting- I don't have the SO cred to do so at the moment.

Community
  • 1
  • 1
SWPhantom
  • 674
  • 5
  • 18
  • I'm saying that I'm picking a point (x,y,z) where I am CERTAIN the x and y coordinates are inside of this particular triangle, but I need to find the Z coordinate. – HiroLord Sep 17 '13 at 23:19
  • Alright. That changes that up a bit. Thanks for clarifying. You COULD do the method described above and try to approximate by seeing how close to 1, or -1 the parallel lines will get (using the parallelism method described in the link I added to my initial comment). Otherwise, let me look some more... – SWPhantom Sep 17 '13 at 23:32
0

You can find the equation that represents the surface by applying the 3 points to the following:

ax+by+cz+d = 0 

after you found a,b and c you have the surface - so the first thing would be to check if point P is on the surface. If it is, we still have to check that it's on the triangle.

For that we'll create a diagonal line, that goes through P, to each one of the edges, or to be more exact, to the linear equation which "includes" the edges (please excuse my poor English - I learned this stuff in my native language so bear with me).

This can be done by taking each pair of points (from the triangle), say A=(x1,y1,z1) and B=(x2,y2,z2) and assigning them to the first equation. we'll get a linear equation:

y = mx + n.

Here m is the incline - so the diagonal equation will have

m' = -1/m

and since we know the values of P we can use both to find the value of n. Now that we have both equations we can find the point that exists on both lines. Let's call this point P'.

It's easy to find if P' is between A and B. if it is (for every pair!) - then P is inside the triangle, else, it's not.

Example: enter image description here

Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129