68 lines
1.6 KiB
C#
68 lines
1.6 KiB
C#
/*
|
|
* Author: Andrey Pokidov
|
|
* License: Apache-2.0
|
|
* Date: 18 Nov 2024
|
|
*/
|
|
|
|
namespace BGC
|
|
{
|
|
public class DegreeFP32
|
|
{
|
|
public const float RADIANS_IN_DEGREE = 1.745329252E-2f;
|
|
public const float TURNS_IN_DEGREE = 2.7777777778E-3f;
|
|
|
|
public static float ToRadians(float degrees)
|
|
{
|
|
return degrees * RADIANS_IN_DEGREE;
|
|
}
|
|
|
|
public static float ToTurns(float degrees)
|
|
{
|
|
return degrees * TURNS_IN_DEGREE;
|
|
}
|
|
|
|
public static float ToUnits(float degrees, AngleUnit toUnit)
|
|
{
|
|
if (toUnit == AngleUnit.RADIANS)
|
|
{
|
|
return degrees * RADIANS_IN_DEGREE;
|
|
}
|
|
|
|
if (toUnit == AngleUnit.TURNS)
|
|
{
|
|
return degrees * TURNS_IN_DEGREE;
|
|
}
|
|
|
|
return degrees;
|
|
}
|
|
|
|
public static float Normalize(float radians, AngleRange range)
|
|
{
|
|
if (range == AngleRange.UNSIGNED_RANGE)
|
|
{
|
|
if (0.0f <= radians && radians < 360.0f)
|
|
{
|
|
return radians;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (-180.0f < radians && radians <= 180.0f)
|
|
{
|
|
return radians;
|
|
}
|
|
}
|
|
|
|
float turns = radians * TURNS_IN_DEGREE;
|
|
|
|
turns -= MathF.Floor(turns);
|
|
|
|
if (range == AngleRange.SIGNED_RANGE && turns > 0.5f)
|
|
{
|
|
turns -= 1.0f;
|
|
}
|
|
|
|
return turns * 360.0f;
|
|
}
|
|
}
|
|
}
|