Before going to the flag and marking this as a dupe, please acknowledge that I have minor c++ experience, and I don't completely understand answers given to some questions regarding matrices or higher up-there maths.
I have triangle defined by 3 points, A(0,0,0), B(1,0,0), C(0,1,1)
I have a line segment, defined by 2 points, D(0,1/2,0), and E(1,1/2,1)
Now, regarding the first link, I understand that if the volumes of tetra ABCD = 0, or tetra ABCE = 0, that there is a point within the triangles face. But when negative volumes are mentioned:
Here is one way to solve your problem. Compute the volume of the tetrahedron Td = (a,b,c,d) and Te = (a,b,c,e). If either volume of Td or Te is zero, then one endpoint of the segment de lies on the plane containing triangle (a,b,c). If the volumes of Td and Te have the same sign, then de lies strictly to one side, and there is no intersection. If Td and Te have opposite signs, then de crosses the plane containing (a,b,c).
How can a volume be negative? Am I missing something?
I have this code for a 2d triangle, but I cannot find anything online for a 3d triangle which is understandable/easily translatable into code,
def areaOfTri(x1, y1, x2, y2, x3, y3):
return abs((x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2)) / 2.0)
def pointIn2dTri(x1, y1, x2, y2, x3, y3, x, y):
# Calculate area of triangle ABC
A = areaOfTri (x1, y1, x2, y2, x3, y3)
# Calculate area of triangle PBC
A1 = areaOfTri (x, y, x2, y2, x3, y3)
# Calculate area of triangle PAC
A2 = areaOfTri (x1, y1, x, y, x3, y3)
# Calculate area of triangle PAB
A3 = areaOfTri (x1, y1, x2, y2, x, y)
# Check if sum of A1, A2 and A3
# is same as A
if(A == A1 + A2 + A3):
return True
else:
return False