Original Post
I'm trying to check if a point is inside a triangle. I'm doing this by finding the area of the main triangle, and then the area of three triangles using the three points from the first triangle, and the point i want to check if its inside the first triangle. If the point is inside the first triangle, then the three smaller triangles' areas added up should equal the main triangles area, otherwise if the point is outside the main triangle, the three smaller triangles areas summed up will be greater than the main triangles area.
So my question is, Can i find the areas of the triangles WITHOUT using the sqrt() function? that function is so expensive, it could mean the difference between 400 fps and 4 fps (I didn't find those exact fps results, but it does mean the difference between smooth gameplay, and very choppy gameplay).
I've tried to just remove all the sqrt() functions, I figured the result should be the same in the end, but i was wrong.
Anyway, here's my code:
I've tried to remove just the sqrt() functions from the final areas of each triangle, but that results in more of an approximation than exact, so i lose a lot of accuracy.
I would like to keep the accuracy
any ideas?
So my question is, Can i find the areas of the triangles WITHOUT using the sqrt() function? that function is so expensive, it could mean the difference between 400 fps and 4 fps (I didn't find those exact fps results, but it does mean the difference between smooth gameplay, and very choppy gameplay).
I've tried to just remove all the sqrt() functions, I figured the result should be the same in the end, but i was wrong.
Anyway, here's my code:
//Find area of first triangle
float distX = triV1.x - triV2.x;
float distY = triV1.y - triV2.y;
float distZ = triV1.z - triV2.z;
float edgeLength1 = sqrt(distX*distX + distY*distY + distZ*distZ);
distX = triV1.x - triV3.x;
distY = triV1.y - triV3.y;
distZ = triV1.z - triV3.z;
float edgeLength2 = sqrt(distX*distX + distY*distY + distZ*distZ);
distX = triV2.x - triV3.x;
distY = triV2.y - triV3.y;
distZ = triV2.z - triV3.z;
float edgeLength3 = sqrt(distX*distX + distY*distY + distZ*distZ);
float s = (edgeLength1 + edgeLength2 + edgeLength3)/2.0f;
float mainTriArea = sqrt(s*(s-edgeLength1)*(s-edgeLength2)*(s-edgeLength3));
//Find areas of the three triangles created with the point
float smallTriArea[3] = {0.0f, 0.0f, 0.0f};
XMFLOAT3 triVert[4];
triVert[0] = triV1;
triVert[1] = triV2;
triVert[2] = triV3;
triVert[3] = triV1; //When i=2, i+1 will be triV1
//Find 3 triangle areas using the plane intersecting point
for(int i = 0; i < 3; i++)
{
distX = point.x - triVert.x;
distY = point.y - triVert.y;
distZ = point.z - triVert.z;
edgeLength1 = sqrt(distX*distX + distY*distY + distZ*distZ);
distX = point.x - triVert[i+1].x;
distY = point.y - triVert[i+1].y;
distZ = point.z - triVert[i+1].z;
edgeLength2 = sqrt(distX*distX + distY*distY + distZ*distZ);
distX = triVert.x - triVert[i+1].x;
distY = triVert.y - triVert[i+1].y;
distZ = triVert.z - triVert[i+1].z;
edgeLength3 = sqrt(distX*distX + distY*distY + distZ*distZ);
s = (edgeLength1 + edgeLength2 + edgeLength3)/2.0f;
smallTriArea = sqrt(s*(s-edgeLength1)*(s-edgeLength2)*(s-edgeLength3));
}
float totalSmallTriArea = smallTriArea[0] + smallTriArea[1] + smallTriArea[2];
//Compare the three smaller triangle areas with the main triangles area
//Subtract a small value from totalSmallTriArea to make up for inacuracy
if(mainTriArea >= (totalSmallTriArea - 0.001f))
{
return true;
}I've tried to remove just the sqrt() functions from the final areas of each triangle, but that results in more of an approximation than exact, so i lose a lot of accuracy.
I would like to keep the accuracy
any ideas?