From e33e19d0587146859d48a134ec9fd94e7b7ba5cd Mon Sep 17 00:00:00 2001 From: "FWoltermann@gmail.com" Date: Thu, 8 Dec 2011 14:53:40 +0000 Subject: Initial upload --- Opcode/Ice/IceSegment.cpp | 57 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 Opcode/Ice/IceSegment.cpp (limited to 'Opcode/Ice/IceSegment.cpp') diff --git a/Opcode/Ice/IceSegment.cpp b/Opcode/Ice/IceSegment.cpp new file mode 100644 index 0000000..4b51343 --- /dev/null +++ b/Opcode/Ice/IceSegment.cpp @@ -0,0 +1,57 @@ +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +/** + * Contains code for segments. + * \file IceSegment.cpp + * \author Pierre Terdiman + * \date April, 4, 2000 + */ +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +/** + * Segment class. + * A segment is defined by S(t) = mP0 * (1 - t) + mP1 * t, with 0 <= t <= 1 + * Alternatively, a segment is S(t) = Origin + t * Direction for 0 <= t <= 1. + * Direction is not necessarily unit length. The end points are Origin = mP0 and Origin + Direction = mP1. + * + * \class Segment + * \author Pierre Terdiman + * \version 1.0 + */ +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// Precompiled Header +#include "Stdafx.h" + +using namespace IceMaths; + +float Segment::SquareDistance(const Point& point, float* t) const +{ + Point Diff = point - mP0; + Point Dir = mP1 - mP0; + float fT = Diff | Dir; + + if(fT<=0.0f) + { + fT = 0.0f; + } + else + { + float SqrLen= Dir.SquareMagnitude(); + if(fT>=SqrLen) + { + fT = 1.0f; + Diff -= Dir; + } + else + { + fT /= SqrLen; + Diff -= fT*Dir; + } + } + + if(t) *t = fT; + + return Diff.SquareMagnitude(); +} -- cgit v1.1