Answers for "check if line intersects triangle"

C++
0

check if line intersects triangle

bool isTriangleIntersecting(vec3d p1,vec3d p2,vec3d p3,vec3d qq1,vec3d qq2) {
			
  //p1 , 2 , 3 are points of triangle
  // q1 and q2 defines a line
  
			vec3d q1 = subtractvec(qq1, qq2);
			q1 = normalize(q1);
			q1 = multiplicatevec(q1, vec3d{1000,1000,1000});
			q1 = addvec(q1, qq1);


			vec3d q2 = subtractvec(qq2, qq1);
			q2 = normalize(q2);
			q2 = multiplicatevec(q2, vec3d{ 1000,1000,1000 });
			q2 = addvec(q2, qq2);



			if (GetNaturality(volumeOfTetrahedron(q1, p1, p2, p3)) != GetNaturality(volumeOfTetrahedron(q2, p1, p2, p3))) {
				if (GetNaturality(volumeOfTetrahedron(q1, q2, p1, p2)) == GetNaturality(volumeOfTetrahedron(q1, q2, p2, p3)) && GetNaturality(volumeOfTetrahedron(q1, q2, p2, p3)) == GetNaturality(volumeOfTetrahedron(q1, q2, p3, p1))) {
					return true;
				}
			}
			return false;
		}

vec3d normalize(vec3d v) {
			float lenght = sqrt(v.posx * v.posx + v.posy * v.posy + v.posz * v.posz);
			v.posx /= lenght;
			v.posy /= lenght;
			v.posz /= lenght;
			return v;
		}

float GetNaturality(float f) {
			if (f == 0) {
				return 0;
			}
			return f / abs(f);
		}

		vec3d multiplicatevec(vec3d origin, vec3d v) {
			origin.posx *= v.posx;
			origin.posy *= v.posy;
			origin.posz *= v.posz;
			return origin;
		}

		vec3d subtractvec(vec3d origin,vec3d v) {
			origin.posx -= v.posx;
			origin.posy -= v.posy;
			origin.posz -= v.posz;
			return origin;
		
		}

		vec3d addvec(vec3d origin, vec3d v) {
			origin.posx += v.posx;
			origin.posy += v.posy;
			origin.posz += v.posz;
			return origin;

		}
Posted by: Guest on September-28-2021

Browse Popular Code Answers by Language