2017-08-28 16:13:23 -03:00
|
|
|
#include <cmath>
|
2017-08-08 13:53:47 +02:00
|
|
|
#include "UtilMath.h"
|
|
|
|
|
2018-11-29 16:55:48 +01:00
|
|
|
float Sapphire::Math::Util::distanceSq( float x, float y, float z, float x1, float y1, float z1 )
|
2017-08-08 13:53:47 +02:00
|
|
|
{
|
2018-08-29 21:40:59 +02:00
|
|
|
float deltaX = x - x1;
|
|
|
|
float deltaY = y - y1;
|
|
|
|
float deltaZ = z - z1;
|
2017-08-08 13:53:47 +02:00
|
|
|
|
2018-08-29 21:40:59 +02:00
|
|
|
return ( deltaX * deltaX + deltaY * deltaY + deltaZ * deltaZ );
|
2017-08-08 13:53:47 +02:00
|
|
|
}
|
|
|
|
|
2018-11-29 16:55:48 +01:00
|
|
|
float Sapphire::Math::Util::distance( float x, float y, float z, float x1, float y1, float z1 )
|
2017-08-08 13:53:47 +02:00
|
|
|
{
|
2018-08-29 21:40:59 +02:00
|
|
|
return sqrtf( distanceSq( x, y, z, x1, y1, z1 ) );
|
2017-08-08 13:53:47 +02:00
|
|
|
}
|
|
|
|
|
2018-11-29 16:55:48 +01:00
|
|
|
float Sapphire::Math::Util::distance2DSq( float x, float y, float x1, float y1 )
|
2017-08-08 13:53:47 +02:00
|
|
|
{
|
2018-08-29 21:40:59 +02:00
|
|
|
float deltaX = x - x1;
|
|
|
|
float deltaY = y - y1;
|
|
|
|
return ( deltaX * deltaX + deltaY * deltaY );
|
2017-08-08 13:53:47 +02:00
|
|
|
}
|
|
|
|
|
2018-11-29 16:55:48 +01:00
|
|
|
float Sapphire::Math::Util::distance2D( float x, float y, float x1, float y1 )
|
2017-08-08 13:53:47 +02:00
|
|
|
{
|
2018-08-29 21:40:59 +02:00
|
|
|
return sqrtf( distance2DSq( x, y, x1, y1 ) );
|
2017-08-08 13:53:47 +02:00
|
|
|
}
|
|
|
|
|
2018-11-29 16:55:48 +01:00
|
|
|
float Sapphire::Math::Util::calcAngTo( float x, float y, float x1, float y1 )
|
2017-08-08 13:53:47 +02:00
|
|
|
{
|
2018-08-29 21:40:59 +02:00
|
|
|
float dx = x - x1;
|
|
|
|
float dy = y - y1;
|
|
|
|
if( dy != 0.0f )
|
|
|
|
{
|
|
|
|
return atan2( dy, dx );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 0.0f;
|
|
|
|
}
|
2017-08-08 13:53:47 +02:00
|
|
|
}
|
|
|
|
|
2018-11-29 16:55:48 +01:00
|
|
|
float Sapphire::Math::Util::calcAngFrom( float x, float y, float x1, float y1 )
|
2017-08-08 13:53:47 +02:00
|
|
|
{
|
2018-08-29 21:40:59 +02:00
|
|
|
float dx = x - x1;
|
|
|
|
float dy = y - y1;
|
|
|
|
if( dy != 0.0f )
|
|
|
|
{
|
|
|
|
return atan2( dy, dx );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 0.0f;
|
|
|
|
}
|
2017-08-08 13:53:47 +02:00
|
|
|
}
|
|
|
|
|
2018-11-29 16:55:48 +01:00
|
|
|
uint16_t Sapphire::Math::Util::floatToUInt16( float val )
|
2017-08-08 13:53:47 +02:00
|
|
|
{
|
2018-08-29 21:40:59 +02:00
|
|
|
return static_cast< uint16_t >( 0x8000 + val * 32.767f );
|
2017-08-08 13:53:47 +02:00
|
|
|
}
|
|
|
|
|
2018-11-29 16:55:48 +01:00
|
|
|
uint16_t Sapphire::Math::Util::floatToUInt16Rot( float val )
|
2017-08-08 13:53:47 +02:00
|
|
|
{
|
2018-08-29 21:40:59 +02:00
|
|
|
return static_cast< uint16_t >( 0x8000 * ( ( val + PI ) ) / PI );
|
2017-08-08 13:53:47 +02:00
|
|
|
}
|
|
|
|
|
2018-11-29 16:55:48 +01:00
|
|
|
uint8_t Sapphire::Math::Util::floatToUInt8Rot( float val )
|
2017-08-08 13:53:47 +02:00
|
|
|
{
|
2018-08-29 21:40:59 +02:00
|
|
|
return static_cast< uint8_t >( 0x80 * ( ( val + PI ) ) / PI );
|
2017-08-08 13:53:47 +02:00
|
|
|
}
|