IceSegment.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. // Precompiled Header
  23. #include "Stdafx.h"
  24. using namespace IceMaths;
  25. float Segment::SquareDistance(const Point& point, float* t) const
  26. {
  27. Point Diff = point - mP0;
  28. Point Dir = mP1 - mP0;
  29. float fT = Diff | Dir;
  30. if(fT<=0.0f)
  31. {
  32. fT = 0.0f;
  33. }
  34. else
  35. {
  36. float SqrLen= Dir.SquareMagnitude();
  37. if(fT>=SqrLen)
  38. {
  39. fT = 1.0f;
  40. Diff -= Dir;
  41. }
  42. else
  43. {
  44. fT /= SqrLen;
  45. Diff -= fT*Dir;
  46. }
  47. }
  48. if(t) *t = fT;
  49. return Diff.SquareMagnitude();
  50. }