mirror of
https://github.com/space-wizards/RobustToolbox.git
synced 2026-02-15 03:30:53 +01:00
RobustToolbox projects should be named Robust.* This PR changes the RobustToolbox projects from SS14.* to Robust.* Updates SS14.* prefixes/namespaces to Robust.* Updates SpaceStation14.sln to RobustToolbox.sln Updates MSBUILD/SS14.* to MSBUILD/Robust.* Updates CSProject and MSBuild references for the above Updates git_helper.py Removes Runserver and Runclient as they are unusable
149 lines
4.6 KiB
C#
149 lines
4.6 KiB
C#
using System;
|
|
|
|
namespace Robust.Shared.Maths
|
|
{
|
|
public static class FloatMath
|
|
{
|
|
private const int LookupSize = 1024 * 64; //has to be power of 2
|
|
private static readonly float[] getSin, getCos;
|
|
public const float RadToDeg = (float) (180.0 / Math.PI);
|
|
public const float DegToRad = (float) (Math.PI / 180.0);
|
|
|
|
static FloatMath()
|
|
{
|
|
getSin = new float[LookupSize];
|
|
getCos = new float[LookupSize];
|
|
|
|
for (var i = 0; i < LookupSize; i++)
|
|
{
|
|
getSin[i] = (float) Math.Sin(i * Math.PI / LookupSize * 2);
|
|
getCos[i] = (float) Math.Cos(i * Math.PI / LookupSize * 2);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Fast inaccurate sinus
|
|
/// </summary>
|
|
public static float Sin(float degrees)
|
|
{
|
|
var rot = GetIndex(degrees);
|
|
return getSin[rot];
|
|
}
|
|
|
|
/// <summary>
|
|
/// Fast inaccurate cosinus
|
|
/// </summary>
|
|
public static float Cos(float degrees)
|
|
{
|
|
var rot = GetIndex(degrees);
|
|
return getCos[rot];
|
|
}
|
|
|
|
public static int GetIndex(float degrees)
|
|
{
|
|
return (int) (degrees * (LookupSize / 360f) + 0.5f) & (LookupSize - 1);
|
|
}
|
|
|
|
public static void SinCos(float degrees, out float sin, out float cos)
|
|
{
|
|
var rot = GetIndex(degrees);
|
|
|
|
sin = getSin[rot];
|
|
cos = getCos[rot];
|
|
}
|
|
|
|
public static float Min(float a, float b)
|
|
{
|
|
return Math.Min(a, b);
|
|
}
|
|
|
|
public static float Max(float a, float b)
|
|
{
|
|
return Math.Max(a, b);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns the largest integer smaller to or equal to f.
|
|
/// </summary>
|
|
public static float Floor(float f) => (float)Math.Floor(f);
|
|
|
|
public const float Pi = (float) Math.PI;
|
|
|
|
public static float ToDegrees(float radians)
|
|
{
|
|
return radians / Pi * 180;
|
|
}
|
|
|
|
public static float ToRadians(float degrees)
|
|
{
|
|
return degrees / 180 * Pi;
|
|
}
|
|
|
|
public static T Clamp<T>(this T val, T min, T max)
|
|
where T : IComparable<T>
|
|
{
|
|
if (val.CompareTo(min) < 0) return min;
|
|
if (val.CompareTo(max) > 0) return max;
|
|
return val;
|
|
}
|
|
|
|
public static bool CloseTo(float a, float b, double tolerance = .00001)
|
|
{
|
|
var epsilon = Math.Max(Math.Max(Math.Abs(a), Math.Abs(b)) * tolerance, tolerance); // .001% of the smaller value for the epsilon check as per MSDN reference suggestion
|
|
return Math.Abs(a - b) <= epsilon;
|
|
}
|
|
|
|
public static bool CloseTo(float a, double b, double tolerance = .00001)
|
|
{
|
|
var epsilon = Math.Max(Math.Max(Math.Abs(a), Math.Abs(b)) * tolerance, tolerance); // .001% of the smaller value for the epsilon check as per MSDN reference suggestion
|
|
return Math.Abs(a - b) <= epsilon;
|
|
}
|
|
|
|
public static bool CloseTo(double a, float b, double tolerance = .00001)
|
|
{
|
|
var epsilon = Math.Max(Math.Max(Math.Abs(a), Math.Abs(b)) * tolerance, tolerance); // .001% of the smaller value for the epsilon check as per MSDN reference suggestion
|
|
return Math.Abs(a - b) <= epsilon;
|
|
}
|
|
|
|
public static bool CloseTo(double a, double b, double tolerance = .00001)
|
|
{
|
|
var epsilon = Math.Max(Math.Max(Math.Abs(a), Math.Abs(b)) * tolerance, tolerance); // .001% of the smaller value for the epsilon check as per MSDN reference suggestion
|
|
return Math.Abs(a - b) <= epsilon;
|
|
}
|
|
|
|
/// <summary>
|
|
/// <c>blend</c> 0 means <c>a</c>
|
|
/// </summary>
|
|
public static double Lerp(double a, double b, double blend)
|
|
{
|
|
return a + (b - a) * blend;
|
|
}
|
|
|
|
/// <summary>
|
|
/// <c>blend</c> 0 means <c>a</c>
|
|
/// </summary>
|
|
public static float Lerp(float a, float b, float blend)
|
|
{
|
|
return a + (b - a) * blend;
|
|
}
|
|
|
|
// Clamps value between 0 and 1 and returns value
|
|
public static float Clamp01(float value)
|
|
{
|
|
if (value < 0F)
|
|
return 0F;
|
|
|
|
if (value > 1F)
|
|
return 1F;
|
|
|
|
return value;
|
|
}
|
|
|
|
// Loops the value t, so that it is never larger than length and never smaller than 0.
|
|
public static float Repeat(float t, float length)
|
|
{
|
|
return Clamp(t - Floor(t / length) * length, 0.0f, length);
|
|
}
|
|
}
|
|
}
|