Advertisement
Guest User

Untitled

a guest
Jul 27th, 2016
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. // intersect3D_SegmentPlane(): find the 3D intersection of a segment and a plane
  2. // Input: S = a segment, and Pn = a plane = {Point V0; Vector n;}
  3. // Output: *I0 = the intersect point (when it exists)
  4. // Return: 0 = disjoint (no intersection)
  5. // 1 = intersection in the unique point *I0
  6. // 2 = the segment lies in the plane
  7. int
  8. intersect3D_SegmentPlane( Segment S, Plane Pn, Point* I )
  9. {
  10. Vector u = S.P1 - S.P0;
  11. Vector w = S.P0 - Pn.V0;
  12.  
  13. float D = dot(Pn.n, u);
  14. float N = -dot(Pn.n, w);
  15.  
  16. if (fabs(D) < SMALL_NUM) { // segment is parallel to plane
  17. if (N == 0) // segment lies in plane
  18. return 2;
  19. else
  20. return 0; // no intersection
  21. }
  22. // they are not parallel
  23. // compute intersect param
  24. float sI = N / D;
  25. if (sI < 0 || sI > 1)
  26. return 0; // no intersection
  27.  
  28. *I = S.P0 + sI * u; // compute segment intersect point
  29. return 1;
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement