├── Code.cpp ├── README.md └── tri-tri.pdf /Code.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct coord 5 | { 6 | float x,y,z; 7 | }; 8 | 9 | class triangleCollision 10 | { 11 | public: 12 | 13 | coord _p1,_q1,_r1,_p2,_q2,_r2; 14 | 15 | inline void take_input(); 16 | 17 | inline float calculateDet(float a,float b,float c,float d); 18 | 19 | inline coord obtainVector(coord p,float l,coord q); 20 | 21 | coord obtainIntersectionPoint(coord p,coord q,coord r,coord s); 22 | 23 | inline float dot(coord p,coord q); 24 | 25 | inline coord cross(coord v1,coord v2); 26 | 27 | inline coord getVector(coord q,coord p); 28 | 29 | float checkOrientation(coord p,coord q,coord r); 30 | 31 | inline float getDistance(coord p,coord q); 32 | 33 | inline float determinant3d(coord p,coord q, coord r,coord s); 34 | 35 | bool checkingPointinTriangle(coord _p2,coord _q2,coord _r2,coord _p1); 36 | 37 | coord barycentricCoord(coord v3,coord v4,float d00,float d01,float d11,float denom,coord _p1,coord _p2); 38 | 39 | inline void show(coord v); 40 | 41 | void anyIntersection(coord u1,coord u2,coord v1,coord v2,coord _p2,coord _q2,coord _r2); 42 | 43 | void findIntersection(coord _p1,coord _q1,coord _r1,coord _p2,coord _q2,coord _r2);// function containing anyIntersection function for a pair of edges 44 | 45 | void check_type_of_intersection(); // ie, No intersection or crossing intersection or coplanar intersection 46 | 47 | void checkCrossIntersection(coord _p1,coord _q1,coord _r1,coord _p2,coord _q2,coord _r2,float z1,float z2,float z3); 48 | 49 | void checkCoplanarIntersection(coord _p1,coord _q1,coord _r1,coord _p2,coord _q2,coord _r2); 50 | 51 | void settingPositionsaccordingly(coord &_p1,coord &_q1,coord &_r1,coord &_p2,coord &_q2,coord &_r2,float z1,float z2,float z3,float zz1,float zz2,float zz3); 52 | 53 | inline void swapPosition(coord *p,coord *q); 54 | 55 | void find3DTypesAndIntersections(coord _p1,coord _q1,coord _r1,coord _p2,coord _q2,coord _r2,coord v1,coord v2,coord v3,coord v4,coord n1,coord n2); 56 | 57 | void CoplanarDetectedRegion1(coord _p1,coord _q1,coord _r1,coord _p2,coord _q2,coord _r2,float d1,float d2,float d3); 58 | 59 | void CoplanarDetectedRegion2(coord _p1,coord _q1,coord _r1,coord _p2,coord _q2,coord _r2,float d1,float d2,float d3); 60 | 61 | coord baryIntersectionPoint(coord u1,coord u2,coord v1,coord v2); 62 | 63 | coord baryLine(coord a,coord b); 64 | 65 | inline void ChangePermutationL(coord *p,coord *q,coord *r); 66 | 67 | inline void ChangePermutationR(coord *p,coord *q,coord *r); 68 | }; 69 | 70 | coord triangleCollision :: baryLine(coord a,coord b) 71 | { 72 | coord s; 73 | s.x=(a.y*b.z-a.z*b.y); 74 | s.y=(a.z*b.x-a.x*b.z); 75 | s.z=(a.x*b.y-a.y*b.x); 76 | return s; 77 | } 78 | 79 | coord triangleCollision :: baryIntersectionPoint(coord u1,coord u2,coord v1,coord v2) 80 | { 81 | coord e1=baryLine(u1,u2); 82 | coord e2=baryLine(v1,v2); 83 | coord s; 84 | s=baryLine(e1,e2); 85 | return s; 86 | } 87 | 88 | inline void triangleCollision :: take_input() 89 | { 90 | cin>>_p1.x>>_p1.y>>_p1.z; 91 | cin>>_q1.x>>_q1.y>>_q1.z; 92 | cin>>_r1.x>>_r1.y>>_r1.z; 93 | cin>>_p2.x>>_p2.y>>_p2.z; 94 | cin>>_q2.x>>_q2.y>>_q2.z; 95 | cin>>_r2.x>>_r2.y>>_r2.z; 96 | } 97 | 98 | inline float triangleCollision :: calculateDet(float a,float b,float c,float d) 99 | { 100 | return a*d-c*b; 101 | } 102 | 103 | inline coord triangleCollision :: obtainVector(coord p,float l,coord q) 104 | { 105 | coord s; 106 | s.x=p.x+l*q.x; 107 | s.y=p.y+l*q.y; 108 | s.z=p.z+l*q.z; 109 | return s; 110 | } 111 | 112 | coord triangleCollision :: obtainIntersectionPoint(coord p,coord q,coord r,coord s) 113 | { 114 | float l =calculateDet(q.x,-s.x,q.y,-s.y)/calculateDet(r.x-p.x,-s.x,r.y-p.y,-s.y); 115 | coord i; 116 | i=obtainVector(p,l,q); 117 | return i; 118 | } 119 | 120 | inline float triangleCollision :: dot(coord p,coord q) 121 | { 122 | return p.x*q.x+p.y*q.y+p.z*q.z; 123 | } 124 | 125 | inline coord triangleCollision :: getVector(coord q,coord p) 126 | { 127 | coord s; 128 | s.x=q.x-p.x; 129 | s.y=q.y-p.y; 130 | s.z=q.z-p.z; 131 | return s; 132 | } 133 | 134 | inline coord triangleCollision :: cross(coord v1,coord v2) 135 | { 136 | coord s; 137 | s.x=(v1.y*v2.z)-(v1.z*v2.y); 138 | s.y=-1*((v1.x*v2.z)-(v1.z*v2.x)); 139 | s.z=(v1.x*v2.y-v1.y*v2.x); 140 | return s; 141 | } 142 | 143 | inline float triangleCollision :: checkOrientation(coord p,coord q,coord r) 144 | { 145 | float f=(p.x-r.x)*(q.y-r.y)-(q.x-r.x)*(p.y-r.y); 146 | return f; 147 | } 148 | 149 | inline float triangleCollision :: getDistance(coord p,coord q) 150 | { 151 | float z=(q.x-p.x)*(q.x-p.x)+(q.y-p.y)*(q.y-p.y); 152 | return z; 153 | } 154 | 155 | inline float triangleCollision :: determinant3d(coord p,coord q, coord r,coord s) 156 | { 157 | p.x-=s.x; 158 | p.y-=s.y; 159 | p.z-=s.z; 160 | q.x-=s.x; 161 | q.y-=s.y; 162 | q.z-=s.z; 163 | r.x-=s.x; 164 | r.y-=s.y; 165 | r.z-=s.z; 166 | float z; 167 | z=p.x*(q.y*r.z-q.z*r.y)-p.y*(q.x*r.z-q.z*r.x)+p.z*(q.x*r.y-q.y*r.x); 168 | return z; 169 | } 170 | 171 | bool triangleCollision :: checkingPointinTriangle(coord _p2,coord _q2,coord _r2,coord _p1) 172 | { 173 | float a1,a2,a3; 174 | // checking if _p1 lies inside, on edge or on vertex: 175 | a1=checkOrientation(_p1,_p2,_q2); 176 | a2=checkOrientation(_p1,_q2,_r2); 177 | a3=checkOrientation(_p1,_r2,_p2); 178 | if(a1>0 && a2>0 && a3>0) 179 | return true; 180 | return false; 181 | } 182 | 183 | coord triangleCollision :: barycentricCoord(coord v3,coord v4,float d00,float d01,float d11,float denom,coord _p1,coord _p2) 184 | { 185 | coord v5=getVector(_p1,_p2); 186 | float d20=dot(v5,v3); 187 | float d21=dot(v5,v4); 188 | coord s; 189 | // cout<<"bary: "; 190 | // cout<=0.0f && s.y>=0.0f && s.z>=0.0f && t>=0.0f && t<=1.0f) 221 | { 222 | float xx,yy,zz; 223 | coord u1=s; 224 | xx=u1.x*_p2.x+u1.y*_q2.x+u1.z*_r2.x; 225 | yy=u1.x*_p2.y+u1.y*_q2.y+u1.z*_r2.y; 226 | zz=u1.x*_p2.z+u1.y*_q2.z+u1.z*_r2.z; 227 | cout<<"intersection point: "; 228 | cout<0.0f && b1.y*b2.y>0.0f && b1.z*b2.z>0.0f) 256 | cout<<"no intersection for _p1 and _q1\n"; 257 | else 258 | { 259 | // show(_p1);show(_q1); 260 | // cout<<":------\n"; 261 | // show(_p2);show(_q2); 262 | anyIntersection(b1,b2,p22,q22,_p2,_q2,_r2); 263 | // show(_q2);show(_r2); 264 | anyIntersection(b1,b2,q22,r22,_p2,_q2,_r2); 265 | // show(_r2);show(_p2); 266 | anyIntersection(b1,b2,r22,p22,_p2,_q2,_r2); 267 | } 268 | if(b3.x*b2.x>0.0f && b3.y*b2.y>0.0f && b3.z*b2.z>0.0f) 269 | cout<<"no intersection for _r1 and _q1\n"; 270 | else 271 | { 272 | // show(_q1);show(_r1); 273 | // cout<<":------\n"; 274 | // show(_p2);show(_q2); 275 | anyIntersection(b2,b3,p22,q22,_p2,_q2,_r2); 276 | // show(_q2);show(_r2); 277 | anyIntersection(b2,b3,q22,r22,_p2,_q2,_r2); 278 | // show(_r2);show(_p2); 279 | anyIntersection(b2,b3,r22,p22,_p2,_q2,_r2); 280 | 281 | } 282 | if(b1.x*b3.x>0.0f && b1.y*b3.y>0.0f && b1.z*b3.z>0.0f) 283 | cout<<"no intersection for _p1 and _r1\n"; 284 | else 285 | { 286 | // show(_p1);show(_r1); 287 | // cout<<":------\n"; 288 | // show(_p2);show(_q2); 289 | anyIntersection(b1,b3,p22,q22,_p2,_q2,_r2); 290 | // show(_q2);show(_r2); 291 | anyIntersection(b1,b3,q22,r22,_p2,_q2,_r2); 292 | // show(_r2);show(_p2); 293 | anyIntersection(b1,b3,r22,p22,_p2,_q2,_r2); 294 | } 295 | } 296 | 297 | void triangleCollision :: check_type_of_intersection() 298 | { 299 | float z1,z2,z3; 300 | z1=determinant3d(_p2,_q2,_r2,_p1); 301 | z2=determinant3d(_p2,_q2,_r2,_q1); 302 | z3=determinant3d(_p2,_q2,_r2,_r1); 303 | int f=2; 304 | // cout<0.0f && z2>0.0f && z3>0.0f) || (z1<0.0f && z2<0.0f && z3<0.0f)) 306 | f=0; 307 | if(z1<0.1f && z1>-0.1f && z2<0.1f && z2>-0.1f && z3<0.1f && z3>-0.1f) 308 | f=1; 309 | if(f==0) 310 | { 311 | cout<<"No Intersection!"; 312 | return; 313 | } 314 | // two vectors 315 | coord v3=getVector(_q2,_p2); 316 | coord v4=getVector(_r2,_p2); 317 | if(f==2) 318 | checkCrossIntersection(_p1,_q1,_r1,_p2,_q2,_r2,z1,z2,z3); 319 | else 320 | checkCoplanarIntersection(_p1,_q1,_r1,_p2,_q2,_r2); 321 | } 322 | 323 | inline void triangleCollision :: swapPosition(coord *p,coord *q) 324 | { 325 | coord s; 326 | s=*p; 327 | *p=*q; 328 | *q=s; 329 | return; 330 | } 331 | inline void triangleCollision :: ChangePermutationL(coord *p,coord *q,coord *r) 332 | { 333 | coord s; 334 | s=*p; 335 | *p=*q; 336 | *q=*r; 337 | *r=s; 338 | return; 339 | } 340 | inline void triangleCollision :: ChangePermutationR(coord *p,coord *q,coord *r) 341 | { 342 | coord s; 343 | s=*r; 344 | *r=*q; 345 | *q=*p; 346 | *p=s; 347 | return; 348 | } 349 | void triangleCollision :: settingPositionsaccordingly(coord &_p1,coord &_q1,coord &_r1,coord &_p2,coord &_q2,coord &_r2,float z1,float z2,float z3,float zz1,float zz2,float zz3) 350 | { 351 | // one on plane T2, T1: 352 | if(z1==0.0f || z2==0.0f || z3==0.0f) 353 | { 354 | if(z2==0.0f) 355 | ChangePermutationL(&_p1,&_q1,&_r1); 356 | else if(z3==0.0f) 357 | ChangePermutationR(&_p1,&_q1,&_r1); 358 | } 359 | // one on plane T1, T2: 360 | if(zz1==0.0f || zz2==0.0f || zz3==0.0f) 361 | { 362 | if(zz2==0.0f) 363 | ChangePermutationL(&_p2,&_q2,&_r2); 364 | else if(zz3==0.0f) 365 | ChangePermutationR(&_p2,&_q2,&_r2); 366 | } 367 | // to get _cout<0.0f and z1<0.0f 368 | if(z1>0.0f) 369 | { 370 | if(z2>0.0f && z3<0.0f) 371 | ChangePermutationR(&_p1,&_q1,&_r1); 372 | else if(z2<0.0f && z3>0.0f) 373 | ChangePermutationL(&_p1,&_q1,&_r1); 374 | } 375 | else 376 | { 377 | if(z2>0.0f && z3<0.0f) 378 | ChangePermutationL(&_p1,&_q1,&_r1); 379 | else if(z2<0.0f && z3>0.0f) 380 | ChangePermutationR(&_p1,&_q1,&_r1); 381 | } 382 | // to get _p2 on one side , two cases: zz1>0.0f and zz1<0.0f 383 | if(zz1>0.0f) 384 | { 385 | if(zz2>0.0f && zz3<0.0f) 386 | ChangePermutationR(&_p2,&_q2,&_r2); 387 | else if(zz2<0.0f && zz3>0.0f) 388 | ChangePermutationL(&_p2,&_q2,&_r2); 389 | } 390 | else 391 | { 392 | if(zz2>0.0f && zz3<0.0f) 393 | ChangePermutationL(&_p2,&_q2,&_r2); 394 | else if(zz2<0.0f && zz3>0.0f) 395 | ChangePermutationR(&_p2,&_q2,&_r2); 396 | } 397 | 398 | // to get _p1 on positive side: 399 | float y1=determinant3d(_p2,_q2,_r2,_p1); 400 | if(y1<0.0f) 401 | swapPosition(&_q2,&_r2); 402 | // to get _p2 on positive side: 403 | float y2=determinant3d(_p1,_q1,_r1,_p2); 404 | if(y2<0.0f) 405 | swapPosition(&_q1,&_r1); 406 | return ; 407 | } 408 | void triangleCollision :: find3DTypesAndIntersections(coord _p1,coord _q1,coord _r1,coord _p2,coord _q2,coord _r2,coord v1,coord v2,coord v3,coord v4,coord n1,coord n2) 409 | { 410 | 411 | if(determinant3d(_p1,_r1,_q2,_p2)>0.0f) 412 | { 413 | if(determinant3d(_p1,_q1,_r2,_p2)>0.0f) 414 | { 415 | coord pp=getVector(_p2,_p1); 416 | float z1=dot(pp,n1); 417 | float z3=dot(v4,n1); 418 | float lam2=-z1/z3; 419 | coord s2=obtainVector(_p2,lam2,v4); 420 | cout<0.0f) 447 | { 448 | coord pp=getVector(_p2,_p1); 449 | float z1=dot(pp,n1); 450 | float z2=dot(v3,n1); 451 | float z3=dot(v4,n1); 452 | float lam1=-z1/z2; 453 | float lam2=-z1/z3; 454 | coord s1=obtainVector(_p2,lam1,v3); 455 | coord s2=obtainVector(_p2,lam2,v4); 456 | cout<0.0f && zz2>0.0f && zz3>0.0f) || (zz1<0.0f && zz2<0.0f && zz3<0.0f)) 487 | ff=0; 488 | if(ff==0) 489 | { 490 | cout<<"No Intersection!"; 491 | return; 492 | } 493 | 494 | settingPositionsaccordingly(_p1,_q1,_r1,_p2,_q2,_r2,z1,z2,z3,zz1,zz2,zz3); 495 | float pr1,pr2; 496 | pr1=determinant3d(_p1,_q1,_p2,_q2); 497 | pr2=determinant3d(_p1,_r1,_r2,_p2); 498 | if(pr1<=0.0f && pr2<=0.0f) 499 | { 500 | cout<<"Triangles do intersect!\n"; 501 | cout<<"Intersection :\n"; 502 | coord v1=getVector(_q1,_p1); 503 | coord v2=getVector(_r1,_p1); 504 | coord v3=getVector(_q2,_p2); 505 | coord v4=getVector(_r2,_p2); 506 | coord n1=cross(v1,v2); 507 | coord n2=cross(v3,v4); 508 | find3DTypesAndIntersections(_p1,_q1,_r1,_p2,_q2,_r2,v1,v2,v3,v4,n1,n2);// 3D 509 | } 510 | else 511 | cout<<"No intersection in Crossing\n"; 512 | } 513 | 514 | void triangleCollision :: CoplanarDetectedRegion1(coord _p1,coord _q1,coord _r1,coord _p2,coord _q2,coord _r2,float d1,float d2,float d3) 515 | { 516 | // cout<d2 && d1>d3) 519 | { 520 | coord s=_p2; 521 | _p2=_r2; 522 | _r2=_q2; 523 | _q2=s; 524 | } 525 | else if(d3>d1 && d3>d2) 526 | { 527 | coord s=_r2; 528 | _r2=_p2; 529 | _p2=_q2; 530 | _q2=s; 531 | } 532 | // assumption of _p1 to be in region R1: 533 | if(checkOrientation(_r2,_p2,_q1)>=0.0f) 534 | { 535 | if(checkOrientation(_r2,_p1,_q1)>=0.0f) 536 | { 537 | if(checkOrientation(_p1,_p2,_q1)>=0.0f) 538 | { 539 | cout<<"Intersection in 2D\n"; 540 | findIntersection(_p1,_q1,_r1,_p2,_q2,_r2); 541 | 542 | } 543 | else 544 | { 545 | if(checkOrientation(_p1,_p2,_r1)>=0.0f) 546 | { 547 | if(checkOrientation(_q1,_r1,_p2)>=0.0f) 548 | { 549 | cout<<"Intersection in 2D\n"; 550 | findIntersection(_p1,_q1,_r1,_p2,_q2,_r2); 551 | 552 | } 553 | else 554 | cout<<"No Intersection in 2D\n"; 555 | } 556 | else 557 | cout<<"No Intersection in 2D\n"; 558 | } 559 | } 560 | else 561 | cout<<"No Intersection in 2D\n"; 562 | } 563 | else 564 | { 565 | if(checkOrientation(_r2,_p2,_r1)>=0.0f) 566 | { 567 | if(checkOrientation(_q1,_r1,_r2)>=0.0f) 568 | { 569 | if(checkOrientation(_p1,_p2,_r1)>0.0f) 570 | cout<<"No Intersection in 2D\n"; 571 | else 572 | { 573 | cout<<"Intersection in 2D\n"; 574 | findIntersection(_p1,_q1,_r1,_p2,_q2,_r2); 575 | 576 | } 577 | } 578 | else 579 | cout<<"No Intersection in 2D\n"; 580 | } 581 | else 582 | cout<<"No Intersection in 2D\n"; 583 | } 584 | } 585 | 586 | void triangleCollision :: CoplanarDetectedRegion2(coord _p1,coord _q1,coord _r1,coord _p2,coord _q2,coord _r2,float d1,float d2,float d3) 587 | { 588 | // assumption of _p1 to be in region R2: 589 | if(d1=0.0f) 604 | { 605 | if(checkOrientation(_q2,_r2,_q1)>=0.0f) 606 | { 607 | if(checkOrientation(_p1,_p2,_q1)>=0.0f) 608 | { 609 | if(checkOrientation(_p1,_q2,_q1)>0.0f) 610 | cout<<"No Intersection in 2D.\n"; 611 | else 612 | { 613 | cout<<"Intersection in 2D\n"; 614 | findIntersection(_p1,_q1,_r1,_p2,_q2,_r2); 615 | 616 | } 617 | } 618 | else 619 | { 620 | if(checkOrientation(_p1,_p2,_r1)>=0.0f) 621 | { 622 | if(checkOrientation(_r2,_p2,_r1)>=0.0f) 623 | { 624 | cout<<"Intersection in 2D\n"; 625 | findIntersection(_p1,_q1,_r1,_p2,_q2,_r2); 626 | 627 | } 628 | else 629 | { 630 | show(_p1); 631 | cout<<" \n"; 632 | show(_p2); 633 | show(_q2); 634 | show(_r2); 635 | cout<<"No Intersection in 2D..\n"; 636 | } 637 | } 638 | else 639 | cout<<"No Intersection in 2D...\n"; 640 | } 641 | } 642 | else 643 | { 644 | if(checkOrientation(_p1,_q2,_q1)>0.0f) 645 | cout<<"No Intersection in 2D....\n"; 646 | else 647 | { 648 | if(checkOrientation(_q2,_r2,_r1)>=0.0f) 649 | { 650 | if(checkOrientation(_q1,_r1,_q2)>=0.0f) 651 | { 652 | cout<<"Intersection in 2D\n"; 653 | findIntersection(_p1,_q1,_r1,_p2,_q2,_r2); 654 | 655 | } 656 | else 657 | cout<<"No Intersection in 2D*\n"; 658 | } 659 | else 660 | cout<<"No Intersection in 2D**\n"; 661 | } 662 | } 663 | } 664 | else 665 | { 666 | if(checkOrientation(_r2,_p2,_r1)>=0.0f) 667 | { 668 | if(checkOrientation(_q1,_r1,_r2)>=0.0f) 669 | { 670 | if(checkOrientation(_r1,_p1,_p2)>=0.0f) 671 | { 672 | cout<<"Intersection in 2D\n"; 673 | findIntersection(_p1,_q1,_r1,_p2,_q2,_r2); 674 | 675 | } 676 | else 677 | cout<<"No Intersection in 2D***\n"; 678 | } 679 | else 680 | { 681 | if(checkOrientation(_q1,_r1,_q2)>=0.0f) 682 | { 683 | if(checkOrientation(_q2,_r2,_r1)>=0.0f) 684 | { 685 | cout<<"Intersection in 2D\n"; 686 | findIntersection(_p1,_q1,_r1,_p2,_q2,_r2); 687 | 688 | } 689 | else 690 | cout<<"No Intersection in 2D****\n"; 691 | } 692 | else 693 | cout<<"No Intersection in 2D#\n"; 694 | } 695 | } 696 | else 697 | cout<<"No Intersection in 2D##\n"; 698 | } 699 | } 700 | 701 | void triangleCollision :: checkCoplanarIntersection(coord _p1,coord _q1,coord _r1,coord _p2,coord _q2,coord _r2) 702 | { 703 | //2-d test 704 | //ensuring counter-clockwise direction for T1 705 | if(checkOrientation(_p1,_q1,_r1)<0.0f) 706 | swapPosition(&_q1,&_r1); 707 | //ensuring counter-clockwise direction for T2 708 | if(checkOrientation(_p2,_q2,_r2)<0.0f) 709 | swapPosition(&_q2,&_r2); 710 | bool y,z; 711 | bool intersection=false; 712 | if(checkingPointinTriangle(_p2,_q2,_r2,_p1)) 713 | { 714 | cout<<"_p1 inside t2 in 2d plane\n"; 715 | intersection=true; 716 | } 717 | float a1,a2,a3; 718 | a1=checkOrientation(_p1,_p2,_q2); 719 | a2=checkOrientation(_p1,_q2,_r2); 720 | a3=checkOrientation(_p1,_r2,_p2); 721 | // cout<0.0f && a2>0.0f && a3<0.0f) 741 | fz=1; 742 | else if(a1>0.0f && a3>0.0f && a2<0.0f) 743 | fz=1; 744 | else if(a2>0.0f && a3>0.0f && a1<0.0f) 745 | fz=1; 746 | float d1,d2,d3; 747 | d1=getDistance(_p1,_p2); 748 | d2=getDistance(_p1,_q2); 749 | d3=getDistance(_p1,_r2); 750 | // need to ensure _p1 is distant from _q2 than _r2 and _p2 for region R1 751 | if(fz==1) 752 | CoplanarDetectedRegion1(_p1,_q1,_r1,_p2,_q2,_r2,d1,d2,d3); 753 | else 754 | CoplanarDetectedRegion2(_p1,_q1,_r1,_p2,_q2,_r2,d1,d2,d3); 755 | } 756 | 757 | int main() 758 | { 759 | clock_t t; 760 | t = clock(); 761 | triangleCollision object; 762 | object.take_input(); 763 | object.check_type_of_intersection(); 764 | t = clock() - t; 765 | cout << "time: " << t << " miliseconds" << "\n"; 766 | } 767 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # An intersection algorithm 2 | Detecting the intersection between two triangles in 3-Dimensional space and determining the intersection points, edges and area. 3 | 4 | Used when : A shape (made up of small triangles) collide with an another shape (also, made up of small triangles) in 3D space. Used for simulation of colliding objects and getting intersections points (if collision occured) and detection of collision. 5 | 6 | More Research : Concept of AABB (Axis-Aligned Bounding Box) can be also used. 7 | And many other boxes also exist. 8 | 9 | 10 | Bonus: Added a visualizer: https://github.com/risgpta/Ant-Colony-Optimisation 11 | -------------------------------------------------------------------------------- /tri-tri.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/risgpta/An-intersection-algorithm/d2c451e07da9288da7fe1ed4546fa4359cef2f4a/tri-tri.pdf --------------------------------------------------------------------------------