Rev 813 |
Go to most recent revision |
Blame |
Compare with Previous |
Last modification |
View Log
| RSS feed
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BauzoidNET.math
{
public class MathUtil
{
public const float EPSILON = 0.0000001f;
/// <summary>
/// Check if value is in range (inclusive).
/// </summary>
public static bool isInRange(float value, float min, float max)
{
return ((value >= min) && (value <= max));
}
/// <summary>
/// Check if value is in range (inclusive min, exclusive max).
/// </summary>
public static bool isInRangeInEx(float value, float min, float max)
{
return ((value >= min) && (value < max));
}
/// <summary>
/// Check if value is in range (exclusive min, inclusive max).
/// </summary>
public static bool isInRangeExIn(float value, float min, float max)
{
return ((value > min) && (value <= max));
}
/// <summary>
/// Check if value is in range (exclusive).
/// </summary>
public static bool isInRangeEx(float value, float min, float max)
{
return ((value > min) && (value < max));
}
/// <summary>
/// Check if value is in range (inclusive).
/// </summary>
public static bool isInRange(int value, int min, int max)
{
return ((value >= min) && (value <= max));
}
/// <summary>
/// Clamp value to range.
/// </summary>
public static float clamp(float value, float min, float max)
{
if (value < min)
return min;
if (value > max)
return max;
return value;
}
/// <summary>
/// Clamp value to range.
/// </summary>
public static int clamp(int value, int min, int max)
{
if (value < min)
return min;
if (value > max)
return max;
return value;
}
/// <summary>
/// Float-based sine function in degrees.
/// </summary>
public static float sin(float degrees)
{
return (float)Math.Sin(degToRad(degrees));
}
/// <summary>
/// Float-based cosine function in degrees.
/// </summary>
public static float cos(float degrees)
{
return (float)Math.Cos(degToRad(degrees));
}
/// <summary>
/// Float-based tangent function in degrees.
/// </summary>
public static float tan(float degrees)
{
return (float)Math.Tan(degToRad(degrees));
}
/// <summary>
/// Convert from degrees to radians.
/// </summary>
public static float degToRad(float degrees)
{
return (float)(degrees * Math.PI / 180.0f);
}
/// <summary>
/// Convert from radians to degrees.
/// </summary>
public static float radToDeg(float rad)
{
return (float)(rad * 180.0f / Math.PI);
}
/// <summary>
/// Return the angle inside a range of 0 to 360 degrees.
/// </summary>
public static float stayInDegrees0to360(float degrees)
{
float result = degrees;
while (result > 360.0f)
result -= 360.0f;
while (result < 0.0f)
result += 360.0f;
return result;
}
/// <summary>
/// Return the (smaller) angle in degrees between the two angles.
/// </summary>
public static float turnDegrees(float rot1, float rot2)
{
if (Math.Abs(rot1 - rot2) > (180.0f))
{
if (rot1 < rot2)
{
rot1 += 360.0f;
}
else
{
rot1 -= 360.0f;
}
}
return Math.Abs(rot2 - rot1);
}
}
}