Move math utilities to OpenNest.Math namespace
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
125
OpenNest.Core/Math/Angle.cs
Normal file
125
OpenNest.Core/Math/Angle.cs
Normal file
@@ -0,0 +1,125 @@
|
||||
using System;
|
||||
|
||||
namespace OpenNest.Math
|
||||
{
|
||||
public static class Angle
|
||||
{
|
||||
/// <summary>
|
||||
/// Number of radians equal to 1 degree.
|
||||
/// </summary>
|
||||
public const double RadPerDeg = System.Math.PI / 180.0;
|
||||
|
||||
/// <summary>
|
||||
/// Number of degrees equal to 1 radian.
|
||||
/// </summary>
|
||||
public const double DegPerRad = 180.0 / System.Math.PI;
|
||||
|
||||
/// <summary>
|
||||
/// Half of PI.
|
||||
/// </summary>
|
||||
public const double HalfPI = System.Math.PI * 0.5;
|
||||
|
||||
/// <summary>
|
||||
/// 2 x PI
|
||||
/// </summary>
|
||||
public const double TwoPI = System.Math.PI * 2.0;
|
||||
|
||||
/// <summary>
|
||||
/// Converts radians to degrees.
|
||||
/// </summary>
|
||||
/// <param name="radians"></param>
|
||||
/// <returns></returns>
|
||||
public static double ToDegrees(double radians)
|
||||
{
|
||||
return DegPerRad * radians;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts degrees to radians.
|
||||
/// </summary>
|
||||
/// <param name="degrees"></param>
|
||||
/// <returns></returns>
|
||||
public static double ToRadians(double degrees)
|
||||
{
|
||||
return RadPerDeg * degrees;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Normalizes an angle.
|
||||
/// The normalized angle will be in the range from 0 to TwoPI,
|
||||
/// where TwoPI itself is not included.
|
||||
/// </summary>
|
||||
/// <param name="angle"></param>
|
||||
/// <returns></returns>
|
||||
public static double NormalizeRad(double angle)
|
||||
{
|
||||
double r = angle % Angle.TwoPI;
|
||||
return r < 0 ? Angle.TwoPI + r : r;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Normalizes an angle.
|
||||
/// The normalized angle will be in the range from 0 to 360,
|
||||
/// where 360 itself is not included.
|
||||
/// </summary>
|
||||
/// <param name="angle"></param>
|
||||
/// <returns></returns>
|
||||
public static double NormalizeDeg(double angle)
|
||||
{
|
||||
double r = angle % 360.0;
|
||||
return r < 0 ? 360.0 + r : r;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Whether the given angle is between the two specified angles (a1 & a2).
|
||||
/// </summary>
|
||||
/// <param name="angle"></param>
|
||||
/// <param name="a1"></param>
|
||||
/// <param name="a2"></param>
|
||||
/// <param name="reversed"></param>
|
||||
/// <returns></returns>
|
||||
public static bool IsBetweenRad(double angle, double a1, double a2, bool reversed = false)
|
||||
{
|
||||
if (reversed)
|
||||
Generic.Swap(ref a1, ref a2);
|
||||
|
||||
var diff = Angle.NormalizeRad(a2 - a1);
|
||||
|
||||
// full circle
|
||||
if (a2.IsEqualTo(a1))
|
||||
return true;
|
||||
|
||||
a1 = Angle.NormalizeRad(angle - a1);
|
||||
a2 = Angle.NormalizeRad(a2 - angle);
|
||||
|
||||
return diff >= a1 - Tolerance.Epsilon ||
|
||||
diff >= a2 - Tolerance.Epsilon;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Whether the given angle is between the two specified angles (a1 & a2).
|
||||
/// </summary>
|
||||
/// <param name="angle"></param>
|
||||
/// <param name="a1"></param>
|
||||
/// <param name="a2"></param>
|
||||
/// <param name="reversed"></param>
|
||||
/// <returns></returns>
|
||||
public static bool IsBetweenDeg(double angle, double a1, double a2, bool reversed = false)
|
||||
{
|
||||
if (reversed)
|
||||
Generic.Swap(ref a1, ref a2);
|
||||
|
||||
var diff = Angle.NormalizeRad(a2 - a1);
|
||||
|
||||
// full circle
|
||||
if (a2.IsEqualTo(a1))
|
||||
return true;
|
||||
|
||||
a1 = Angle.NormalizeRad(angle - a1);
|
||||
a2 = Angle.NormalizeRad(a2 - angle);
|
||||
|
||||
return diff >= a1 - Tolerance.Epsilon ||
|
||||
diff >= a2 - Tolerance.Epsilon;
|
||||
}
|
||||
}
|
||||
}
|
||||
15
OpenNest.Core/Math/EvenOdd.cs
Normal file
15
OpenNest.Core/Math/EvenOdd.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
namespace OpenNest.Math
|
||||
{
|
||||
public static class EvenOdd
|
||||
{
|
||||
public static bool IsEven(this int i)
|
||||
{
|
||||
return (i % 2) == 0;
|
||||
}
|
||||
|
||||
public static bool IsOdd(this int i)
|
||||
{
|
||||
return (i % 2) == 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
12
OpenNest.Core/Math/Generic.cs
Normal file
12
OpenNest.Core/Math/Generic.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
namespace OpenNest.Math
|
||||
{
|
||||
public static class Generic
|
||||
{
|
||||
public static void Swap<T>(ref T a, ref T b)
|
||||
{
|
||||
T c = a;
|
||||
a = b;
|
||||
b = c;
|
||||
}
|
||||
}
|
||||
}
|
||||
14
OpenNest.Core/Math/Tolerance.cs
Normal file
14
OpenNest.Core/Math/Tolerance.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using System;
|
||||
|
||||
namespace OpenNest.Math
|
||||
{
|
||||
public static class Tolerance
|
||||
{
|
||||
public const double Epsilon = 0.00001;
|
||||
|
||||
public static bool IsEqualTo(this double a, double b, double tolerance = Epsilon)
|
||||
{
|
||||
return System.Math.Abs(b - a) <= tolerance;
|
||||
}
|
||||
}
|
||||
}
|
||||
40
OpenNest.Core/Math/Trigonometry.cs
Normal file
40
OpenNest.Core/Math/Trigonometry.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
using System;
|
||||
|
||||
namespace OpenNest.Math
|
||||
{
|
||||
public static class Trigonometry
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="height">Height</param>
|
||||
/// <param name="hypotenuse">Hypotenuse</param>
|
||||
/// <returns></returns>
|
||||
public static double Base(double height, double hypotenuse)
|
||||
{
|
||||
return System.Math.Sqrt(hypotenuse * hypotenuse - height * height);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="bottom">Base</param>
|
||||
/// <param name="hypotenuse">Hypotenuse</param>
|
||||
/// <returns></returns>
|
||||
public static double Height(double bottom, double hypotenuse)
|
||||
{
|
||||
return System.Math.Sqrt(hypotenuse * hypotenuse - bottom * bottom);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="height">Height</param>
|
||||
/// <param name="bottom">Base</param>
|
||||
/// <returns></returns>
|
||||
public static double Hypotenuse(double height, double bottom)
|
||||
{
|
||||
return System.Math.Sqrt(height * height + bottom * bottom);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user