Advertisement
Levii_Valenok

Untitled

Apr 20th, 2023
826
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.21 KB | None | 0 0
  1.  
  2. #include <iostream>
  3.  
  4. float determinant(float A[4][4]) {
  5.     float det = 0;
  6.     float submatrix[3][3];
  7.     int sign = 1;
  8.  
  9.     for (int i = 0; i < 4; i++) {
  10.         int sub_i = 0;
  11.         for (int j = 1; j < 4; j++) {
  12.             int sub_j = 0;
  13.             for (int k = 0; k < 4; k++) {
  14.                 if (k != i) {
  15.                     submatrix[sub_i][sub_j] = A[j][k];
  16.                     sub_j++;
  17.                 }
  18.             }
  19.             sub_i++;
  20.         }
  21.         det = det + sign * A[0][i] * (
  22.                 submatrix[0][0] * (submatrix[1][1] * submatrix[2][2] - submatrix[2][1] * submatrix[1][2])
  23.                 - submatrix[0][1] * (submatrix[1][0] * submatrix[2][2] - submatrix[2][0] * submatrix[1][2])
  24.                 + submatrix[0][2] * (submatrix[1][0] * submatrix[2][1] - submatrix[2][0] * submatrix[1][1]));
  25.         sign = -sign;
  26.     }
  27.  
  28.     return det;
  29. }
  30.  
  31. bool isPointInsideTetrahedron(float x1, float y1, float z1, float x2, float y2, float z2,
  32.                               float x3, float y3, float z3, float x4, float y4, float z4,
  33.                               float xp, float yp, float zp) {
  34.     // Calculate the determinants of the four submatrices formed by replacing the
  35.     // x, y, z coordinates of the tetrahedron vertices with those of the point
  36.     float A1[4][4] = {{x1, y1, z1, 1},
  37.                       {x2, y2, z2, 1},
  38.                       {x3, y3, z3, 1},
  39.                       {x4, y4, z4, 1}};
  40.  
  41.     float A2[4][4] = {{xp, xp, xp, 1},
  42.                       {x2, y2, z2, 1},
  43.                       {x3, y3, z3, 1},
  44.                       {x4, y4, z4, 1}};
  45.  
  46.     float A3[4][4] = {{x1, y1, z1, 1},
  47.                       {xp, xp, xp, 1},
  48.                       {x3, y3, z3, 1},
  49.                       {x4, y4, z4, 1}};
  50.  
  51.     float A4[4][4] =
  52.             {{x1, y1, z1, 1},
  53.              {x2, y2, z2, 1},
  54.              {xp, xp, xp, 1},
  55.              {x4, y4, z4, 1}};
  56.  
  57.     float A5[4][4] = {{x1, y1, z1, 1},
  58.                       {x2, y2, z2, 1},
  59.                       {x3, y3, z3, 1},
  60.                       {xp, xp, xp, 1}};
  61.  
  62.     float det1 = determinant(A1);
  63.     float det2 = determinant(A2);
  64.     float det3 = determinant(A3);
  65.     float det4 = determinant(A4);
  66.     float det5 = determinant(A5);
  67.  
  68.     // The point is inside the tetrahedron if and only if all four determinants have the same sign
  69.     if ((det1 > 0 && det2 > 0 && det3 > 0 && det4 > 0 && det5 > 0) || (det1 < 0 && det2 < 0 && det3 < 0 && det4 < 0 && det5 < 0)) {
  70.         return true;
  71.     } else {
  72.         return false;
  73.     }
  74. }
  75.  
  76. int main() {
  77.  
  78.     float time = 0;
  79. // Define the tetrahedron vertices and the test point
  80.     float x1 = 0, y1 = 0, z1 = -0.7;
  81.     float x2 = -1.5, y2 = 0, z2 = 0;
  82.     float x3 = 0, y3 = -0.2, z3 = 0;
  83.     float x4 = 0.3, y4 = 0.4, z4 = 0.5;
  84.     float xp = 0, yp = 0, zp = 0;
  85.  
  86. // Check if the point is inside the tetrahedron
  87.     if (isPointInsideTetrahedron(x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4, xp, yp, zp)) {
  88.         std::cout << "The point (" << xp << ", " << yp << ", " << zp << ") is inside the tetrahedron.\n";
  89.     } else {
  90.         std::cout << "The point (" << xp << ", " << yp << ", " << zp << ") is outside the tetrahedron.\n";
  91.     }
  92.  
  93.  
  94.  
  95.     return 0;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement