1
Fork 0
mirror of https://github.com/SapphireServer/Sapphire.git synced 2025-04-26 14:37:44 +00:00
sapphire/src/common/Util/UtilMath.cpp

76 lines
1.7 KiB
C++
Raw Normal View History

2017-08-28 16:13:23 -03:00
#include <cmath>
2017-08-08 13:53:47 +02:00
#include "UtilMath.h"
2018-12-02 02:01:41 +01:00
float Sapphire::Util::distanceSq( float x, float y, float z, float x1, float y1, float z1 )
2017-08-08 13:53:47 +02:00
{
float deltaX = x - x1;
float deltaY = y - y1;
float deltaZ = z - z1;
2017-08-08 13:53:47 +02:00
return ( deltaX * deltaX + deltaY * deltaY + deltaZ * deltaZ );
2017-08-08 13:53:47 +02:00
}
2018-12-02 02:01:41 +01:00
float Sapphire::Util::distance( float x, float y, float z, float x1, float y1, float z1 )
2017-08-08 13:53:47 +02:00
{
return sqrtf( distanceSq( x, y, z, x1, y1, z1 ) );
2017-08-08 13:53:47 +02:00
}
float Sapphire::Util::distance( const Common::FFXIVARR_POSITION3& pos1, const Common::FFXIVARR_POSITION3& pos2 )
{
return sqrtf( distanceSq( pos1.x, pos1.y, pos1.z, pos2.x, pos2.y, pos2.z ) );
}
2018-12-02 02:01:41 +01:00
float Sapphire::Util::distance2DSq( float x, float y, float x1, float y1 )
2017-08-08 13:53:47 +02:00
{
float deltaX = x - x1;
float deltaY = y - y1;
return ( deltaX * deltaX + deltaY * deltaY );
2017-08-08 13:53:47 +02:00
}
2018-12-02 02:01:41 +01:00
float Sapphire::Util::distance2D( float x, float y, float x1, float y1 )
2017-08-08 13:53:47 +02:00
{
return sqrtf( distance2DSq( x, y, x1, y1 ) );
2017-08-08 13:53:47 +02:00
}
2018-12-02 02:01:41 +01:00
float Sapphire::Util::calcAngTo( float x, float y, float x1, float y1 )
2017-08-08 13:53:47 +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-12-02 02:01:41 +01:00
float Sapphire::Util::calcAngFrom( float x, float y, float x1, float y1 )
2017-08-08 13:53:47 +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-12-02 02:01:41 +01:00
uint16_t Sapphire::Util::floatToUInt16( float val )
2017-08-08 13:53:47 +02:00
{
return static_cast< uint16_t >( 0x8000 + val * 32.767f );
2017-08-08 13:53:47 +02:00
}
2018-12-02 02:01:41 +01:00
uint16_t Sapphire::Util::floatToUInt16Rot( float val )
2017-08-08 13:53:47 +02:00
{
return static_cast< uint16_t >( 0x8000 * ( ( val + PI ) ) / PI );
2017-08-08 13:53:47 +02:00
}
2018-12-02 02:01:41 +01:00
uint8_t Sapphire::Util::floatToUInt8Rot( float val )
2017-08-08 13:53:47 +02:00
{
return static_cast< uint8_t >( 0x80 * ( ( val + PI ) ) / PI );
2017-08-08 13:53:47 +02:00
}