using UnityEngine;
namespace Unity.Burst.Editor
{
internal static class BurstMath
{
private const float HitBoxAdjust = 2f;
///
/// Rotates around a pivot point according to angle given in degrees.
///
/// Angle in degrees.
/// Point to rotate.
/// Pivot point to rotate around.
/// The rotated point.
internal static Vector2 AnglePoint(float angle, Vector2 point, Vector2 pivotPoint)
{
// https://matthew-brett.github.io/teaching/rotation_2d.html
// Problem Angle is calculates as angle clockwise, and here we use it as it was counterclockwise!
var s = Mathf.Sin(angle);
var c = Mathf.Cos(angle);
point -= pivotPoint;
return new Vector2(c * point.x - s * point.y, s * point.x + c * point.y) + pivotPoint;
}
///
/// Calculated angle in degrees between two points.
///
/// Starting point.
/// End point.
/// Angle in degrees.
internal static float CalculateAngle(Vector2 start, Vector2 end)
{
var distance = end - start;
var angle = Mathf.Rad2Deg * Mathf.Atan(distance.y / distance.x);
if (distance.x < 0)
{
angle += 180;
}
return angle;
}
///
/// Checks if is within enlarged by .
///
/// Give rect to enlarge and check with.
/// Given point to match in enlarged .
/// Whether is within enlarged .
internal static bool AdjustedContains(Rect rect, Vector2 point)
{
return rect.yMax + HitBoxAdjust >= point.y && rect.yMin - HitBoxAdjust <= point.y
&& rect.xMax + HitBoxAdjust >= point.x && rect.xMin - HitBoxAdjust <= point.x;
}
///
/// Checks if is within the closed interval defined by endPoint1 and endPoint2.
///
/// One side of range.
/// Other side of range.
/// Number to check if it is in between and .
/// Whether is within given range.
internal static bool WithinRange(float endPoint1, float endPoint2, float num)
{
float start, end;
if (endPoint1 < endPoint2)
{
start = endPoint1;
end = endPoint2;
}
else
{
start = endPoint2;
end = endPoint1;
}
return start <= num && num <= end;
}
///
/// Rounds down to nearest amount specified by .
///
/// Number to round down.
/// Specifies what amount to round down to.
/// rounded down to amount .
internal static float RoundDownToNearest(float number, float to)
{
//https://www.programmingnotes.org/7601/cs-how-to-round-a-number-to-the-nearest-x-using-cs/
float inverse = 1 / to;
float dividend = Mathf.Floor(number * inverse);
return dividend / inverse;
}
}
}