Shift some seg functions to the cpp file

This commit is contained in:
Marek Roszko 2021-06-02 07:49:09 -04:00
parent 6919d12c70
commit 13abb9f947
2 changed files with 58 additions and 56 deletions

View File

@ -33,7 +33,6 @@
#include <type_traits> // for swap
#include <core/optional.h>
#include <math/util.h> // for rescale
#include <math/vector2d.h>
typedef OPT<VECTOR2I> OPT_VECTOR2I;
@ -406,22 +405,6 @@ private:
int m_index;
};
inline VECTOR2I SEG::LineProject( const VECTOR2I& aP ) const
{
VECTOR2I d = B - A;
ecoord l_squared = d.Dot( d );
if( l_squared == 0 )
return A;
ecoord t = d.Dot( aP - A );
int xp = rescale( t, ecoord{ d.x }, l_squared );
int yp = rescale( t, ecoord{ d.y }, l_squared );
return A + VECTOR2I( xp, yp );
}
inline int SEG::LineDistance( const VECTOR2I& aP, bool aDetermineSide ) const
{
ecoord p = ecoord{ A.y } - B.y;
@ -439,45 +422,6 @@ inline SEG::ecoord SEG::TCoef( const VECTOR2I& aP ) const
return d.Dot( aP - A);
}
inline const VECTOR2I SEG::NearestPoint( const VECTOR2I& aP ) const
{
VECTOR2I d = B - A;
ecoord l_squared = d.Dot( d );
if( l_squared == 0 )
return A;
ecoord t = d.Dot( aP - A );
if( t < 0 )
return A;
else if( t > l_squared )
return B;
int xp = rescale( t, (ecoord)d.x, l_squared );
int yp = rescale( t, (ecoord)d.y, l_squared );
return A + VECTOR2I( xp, yp );
}
inline const VECTOR2I SEG::ReflectPoint( const VECTOR2I& aP ) const
{
VECTOR2I d = B - A;
VECTOR2I::extended_type l_squared = d.Dot( d );
VECTOR2I::extended_type t = d.Dot( aP - A );
VECTOR2I c;
if( !l_squared )
c = aP;
else
{
c.x = A.x + rescale( t, static_cast<VECTOR2I::extended_type>( d.x ), l_squared );
c.y = A.y + rescale( t, static_cast<VECTOR2I::extended_type>( d.y ), l_squared );
}
return 2 * c - aP;
}
inline std::ostream& operator<<( std::ostream& aStream, const SEG& aSeg )
{
aStream << "[ " << aSeg.A << " - " << aSeg.B << " ]";

View File

@ -222,3 +222,61 @@ bool SEG::Contains( const VECTOR2I& aP ) const
{
return Distance( aP ) <= 1;
}
const VECTOR2I SEG::NearestPoint( const VECTOR2I& aP ) const
{
VECTOR2I d = B - A;
ecoord l_squared = d.Dot( d );
if( l_squared == 0 )
return A;
ecoord t = d.Dot( aP - A );
if( t < 0 )
return A;
else if( t > l_squared )
return B;
int xp = rescale( t, (ecoord) d.x, l_squared );
int yp = rescale( t, (ecoord) d.y, l_squared );
return A + VECTOR2I( xp, yp );
}
const VECTOR2I SEG::ReflectPoint( const VECTOR2I& aP ) const
{
VECTOR2I d = B - A;
VECTOR2I::extended_type l_squared = d.Dot( d );
VECTOR2I::extended_type t = d.Dot( aP - A );
VECTOR2I c;
if( !l_squared )
c = aP;
else
{
c.x = A.x + rescale( t, static_cast<VECTOR2I::extended_type>( d.x ), l_squared );
c.y = A.y + rescale( t, static_cast<VECTOR2I::extended_type>( d.y ), l_squared );
}
return 2 * c - aP;
}
VECTOR2I SEG::LineProject( const VECTOR2I& aP ) const
{
VECTOR2I d = B - A;
ecoord l_squared = d.Dot( d );
if( l_squared == 0 )
return A;
ecoord t = d.Dot( aP - A );
int xp = rescale( t, ecoord{ d.x }, l_squared );
int yp = rescale( t, ecoord{ d.y }, l_squared );
return A + VECTOR2I( xp, yp );
}