2017-08-28 16:13:23 -03:00
|
|
|
#include <cmath>
|
2017-08-08 13:53:47 +02:00
|
|
|
#include "UtilMath.h"
|
|
|
|
|
2019-06-02 00:34:22 +10:00
|
|
|
using namespace Sapphire::Common;
|
|
|
|
|
|
|
|
float 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
|
|
|
}
|
|
|
|
|
2019-06-02 00:34:22 +10:00
|
|
|
float 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
|
|
|
}
|
|
|
|
|
2019-06-02 21:39:37 +10:00
|
|
|
float Util::distance( const Sapphire::Common::FFXIVARR_POSITION3& pos1,
|
|
|
|
const Sapphire::Common::FFXIVARR_POSITION3& pos2 )
|
2019-01-28 13:40:03 +11:00
|
|
|
{
|
|
|
|
return sqrtf( distanceSq( pos1.x, pos1.y, pos1.z, pos2.x, pos2.y, pos2.z ) );
|
|
|
|
}
|
|
|
|
|
2019-06-02 00:34:22 +10:00
|
|
|
float 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
|
|
|
}
|
|
|
|
|
2019-06-02 00:34:22 +10:00
|
|
|
float 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
|
|
|
}
|
|
|
|
|
2019-06-02 00:34:22 +10:00
|
|
|
float 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
|
|
|
}
|
|
|
|
|
2019-06-02 00:34:22 +10:00
|
|
|
float 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
|
|
|
}
|
|
|
|
|
2019-06-02 00:34:22 +10:00
|
|
|
uint16_t 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
|
|
|
}
|
|
|
|
|
2019-06-02 00:34:22 +10:00
|
|
|
uint16_t 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
|
|
|
}
|
|
|
|
|
2019-06-02 00:34:22 +10:00
|
|
|
uint8_t 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 );
|
2021-08-16 17:18:29 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
float Util::floatFromUInt16Rot( uint16_t rot )
|
|
|
|
{
|
|
|
|
return rot / 32768.0f * PI - PI;
|
2017-08-08 13:53:47 +02:00
|
|
|
}
|