IceSegment.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2. /**
  3. * Contains code for segments.
  4. * \file IceSegment.cpp
  5. * \author Pierre Terdiman
  6. * \date April, 4, 2000
  7. */
  8. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  9. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  10. /**
  11. * Segment class.
  12. * A segment is defined by S(t) = mP0 * (1 - t) + mP1 * t, with 0 <= t <= 1
  13. * Alternatively, a segment is S(t) = Origin + t * Direction for 0 <= t <= 1.
  14. * Direction is not necessarily unit length. The end points are Origin = mP0 and Origin + Direction = mP1.
  15. *
  16. * \class Segment
  17. * \author Pierre Terdiman
  18. * \version 1.0
  19. */
  20. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  21. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  22. #include "../Opcode.h"
  23. using namespace IceMaths;
  24. float Segment::SquareDistance(const Point& point, float* t) const
  25. {
  26. Point Diff = point - mP0;
  27. Point Dir = mP1 - mP0;
  28. float fT = Diff | Dir;
  29. if(fT<=0.0f)
  30. {
  31. fT = 0.0f;
  32. }
  33. else
  34. {
  35. float SqrLen= Dir.SquareMagnitude();
  36. if(fT>=SqrLen)
  37. {
  38. fT = 1.0f;
  39. Diff -= Dir;
  40. }
  41. else
  42. {
  43. fT /= SqrLen;
  44. Diff -= fT*Dir;
  45. }
  46. }
  47. if(t) *t = fT;
  48. return Diff.SquareMagnitude();
  49. }