using System; namespace UnityEngine { public static class Mathf { public const float Epsilon = 0.00001F; /// /// Returns the sine of angle f. /// /// The input angle, in radians. /// /// The return value between -1 and +1. /// public static float Sin(float f) { return (float) Math.Sin((double) f); } /// /// Returns the cosine of angle f. /// /// The input angle, in radians. /// /// The return value between -1 and 1. /// public static float Cos(float f) { return (float) Math.Cos((double) f); } /// /// Returns the tangent of angle f in radians. /// /// public static float Tan(float f) { return (float) Math.Tan((double) f); } /// /// Returns the arc-sine of f - the angle in radians whose sine is f. /// /// public static float Asin(float f) { return (float) Math.Asin((double) f); } /// /// Returns the arc-cosine of f - the angle in radians whose cosine is f. /// /// public static float Acos(float f) { return (float) Math.Acos((double) f); } /// /// Returns the arc-tangent of f - the angle in radians whose tangent is f. /// /// public static float Atan(float f) { return (float) Math.Atan((double) f); } /// /// Returns the angle in radians whose Tan is y/x. /// /// /// public static float Atan2(float y, float x) { return (float) Math.Atan2((double) y, (double) x); } /// /// Returns square root of f. /// /// public static float Sqrt(float f) { return (float) Math.Sqrt((double) f); } /// /// Returns the absolute value of f. /// /// public static float Abs(float f) { return Math.Abs(f); } /// /// Returns the absolute value of value. /// /// public static int Abs(int value) { return Math.Abs(value); } /// /// Returns the smallest of two or more values. /// /// /// /// public static float Min(float a, float b) { return (double) a >= (double) b? b : a; } /// /// Returns the smallest of two or more values. /// /// /// /// public static float Min(params float[] values) { int length = values.Length; if (length == 0) return 0.0f; float num = values[0]; for (int index = 1; index < length; ++index) { if ((double) values[index] < (double) num) num = values[index]; } return num; } /// /// Returns the smallest of two or more values. /// /// /// /// public static int Min(int a, int b) { return a >= b? b : a; } /// /// Returns the smallest of two or more values. /// /// /// /// public static int Min(params int[] values) { int length = values.Length; if (length == 0) return 0; int num = values[0]; for (int index = 1; index < length; ++index) { if (values[index] < num) num = values[index]; } return num; } /// /// Returns largest of two or more values. /// /// /// /// public static float Max(float a, float b) { return (double) a <= (double) b? b : a; } /// /// Returns largest of two or more values. /// /// /// /// public static float Max(params float[] values) { int length = values.Length; if (length == 0) return 0.0f; float num = values[0]; for (int index = 1; index < length; ++index) { if ((double) values[index] > (double) num) num = values[index]; } return num; } /// /// Returns the largest of two or more values. /// /// /// /// public static int Max(int a, int b) { return a <= b? b : a; } /// /// Returns the largest of two or more values. /// /// /// /// public static int Max(params int[] values) { int length = values.Length; if (length == 0) return 0; int num = values[0]; for (int index = 1; index < length; ++index) { if (values[index] > num) num = values[index]; } return num; } /// /// Returns f raised to power p. /// /// /// public static float Pow(float f, float p) { return (float) Math.Pow((double) f, (double) p); } /// /// Returns e raised to the specified power. /// /// public static float Exp(float power) { return (float) Math.Exp((double) power); } /// /// Returns the logarithm of a specified number in a specified base. /// /// /// public static float Log(float f, float p) { return (float) Math.Log((double) f, (double) p); } /// /// Returns the natural (base e) logarithm of a specified number. /// /// public static float Log(float f) { return (float) Math.Log((double) f); } /// /// Returns the base 10 logarithm of a specified number. /// /// public static float Log10(float f) { return (float) Math.Log10((double) f); } /// /// Returns the smallest integer greater to or equal to f. /// /// public static float Ceil(float f) { return (float) Math.Ceiling((double) f); } /// /// Returns the largest integer smaller to or equal to f. /// /// public static float Floor(float f) { return (float) Math.Floor((double) f); } /// /// Returns f rounded to the nearest integer. /// /// public static float Round(float f) { return (float) Math.Round((double) f); } /// /// Returns the smallest integer greater to or equal to f. /// /// public static int CeilToInt(float f) { return (int) Math.Ceiling((double) f); } /// /// Returns the largest integer smaller to or equal to f. /// /// public static int FloorToInt(float f) { return (int) Math.Floor((double) f); } /// /// Returns f rounded to the nearest integer. /// /// public static int RoundToInt(float f) { return (int) Math.Round((double) f); } /// /// Returns the sign of f. /// /// public static float Sign(float f) { return (double) f < 0.0? -1f : 1f; } /// /// Clamps a value between a minimum float and maximum float value. /// /// /// /// public static float Clamp(float value, float min, float max) { if ((double) value < (double) min) value = min; else if ((double) value > (double) max) value = max; return value; } /// /// Clamps value between min and max and returns value. /// /// /// /// public static int Clamp(int value, int min, int max) { if (value < min) value = min; else if (value > max) value = max; return value; } /// /// Clamps value between 0 and 1 and returns value. /// /// public static float Clamp01(float value) { if ((double) value < 0.0) return 0.0f; if ((double) value > 1.0) return 1f; return value; } /// /// Linearly interpolates between a and b by t. /// /// The start value. /// The end value. /// The interpolation value between the two floats. /// /// The interpolated float result between the two float values. /// public static float Lerp(float a, float b, float t) { return a + (b - a) * Mathf.Clamp01(t); } /// /// Linearly interpolates between a and b by t with no limit to t. /// /// The start value. /// The end value. /// The interpolation between the two floats. /// /// The float value as a result from the linear interpolation. /// public static float LerpUnclamped(float a, float b, float t) { return a + (b - a) * t; } /// /// Same as Lerp but makes sure the values interpolate correctly when they wrap around 360 degrees. /// /// /// /// public static float LerpAngle(float a, float b, float t) { float num = Mathf.Repeat(b - a, 360f); if ((double) num > 180.0) num -= 360f; return a + num * Mathf.Clamp01(t); } /// /// Moves a value current towards target. /// /// The current value. /// The value to move towards. /// The maximum change that should be applied to the value. public static float MoveTowards(float current, float target, float maxDelta) { if ((double) Mathf.Abs(target - current) <= (double) maxDelta) return target; return current + Mathf.Sign(target - current) * maxDelta; } /// /// Same as MoveTowards but makes sure the values interpolate correctly when they wrap around 360 degrees. /// /// /// /// public static float MoveTowardsAngle(float current, float target, float maxDelta) { float num = Mathf.DeltaAngle(current, target); if (-(double) maxDelta < (double) num && (double) num < (double) maxDelta) return target; target = current + num; return Mathf.MoveTowards(current, target, maxDelta); } /// /// Interpolates between min and max with smoothing at the limits. /// /// /// /// public static float SmoothStep(float from, float to, float t) { t = Mathf.Clamp01(t); t = (float) (-2.0 * (double) t * (double) t * (double) t + 3.0 * (double) t * (double) t); return (float) ((double) to * (double) t + (double) from * (1.0 - (double) t)); } public static float Gamma(float value, float absmax, float gamma) { bool flag = false; if ((double) value < 0.0) flag = true; float num1 = Mathf.Abs(value); if ((double) num1 > (double) absmax) return !flag? num1 : -num1; float num2 = Mathf.Pow(num1 / absmax, gamma) * absmax; return !flag? num2 : -num2; } /// /// 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 Mathf.Clamp(t - Mathf.Floor(t / length) * length, 0.0f, length); } /// /// PingPongs the value t, so that it is never larger than length and never smaller than 0. /// /// /// public static float PingPong(float t, float length) { t = Mathf.Repeat(t, length * 2f); return length - Mathf.Abs(t - length); } /// /// Calculates the linear parameter t that produces the interpolant value within the range [a, b]. /// /// /// /// public static float InverseLerp(float a, float b, float value) { if ((double) a != (double) b) return Mathf.Clamp01((float) (((double) value - (double) a) / ((double) b - (double) a))); return 0.0f; } /// /// Calculates the shortest difference between two given angles given in degrees. /// /// /// public static float DeltaAngle(float current, float target) { float num = Mathf.Repeat(target - current, 360f); if ((double) num > 180.0) num -= 360f; return num; } internal static bool LineIntersection(Vector2 p1, Vector2 p2, Vector2 p3, Vector2 p4, ref Vector2 result) { float num1 = p2.x - p1.x; float num2 = p2.y - p1.y; float num3 = p4.x - p3.x; float num4 = p4.y - p3.y; float num5 = (float) ((double) num1 * (double) num4 - (double) num2 * (double) num3); if ((double) num5 == 0.0) return false; float num6 = p3.x - p1.x; float num7 = p3.y - p1.y; float num8 = (float) ((double) num6 * (double) num4 - (double) num7 * (double) num3) / num5; result = new Vector2(p1.x + num8 * num1, p1.y + num8 * num2); return true; } internal static bool LineSegmentIntersection(Vector2 p1, Vector2 p2, Vector2 p3, Vector2 p4, ref Vector2 result) { float num1 = p2.x - p1.x; float num2 = p2.y - p1.y; float num3 = p4.x - p3.x; float num4 = p4.y - p3.y; float num5 = (float) ((double) num1 * (double) num4 - (double) num2 * (double) num3); if ((double) num5 == 0.0) return false; float num6 = p3.x - p1.x; float num7 = p3.y - p1.y; float num8 = (float) ((double) num6 * (double) num4 - (double) num7 * (double) num3) / num5; if ((double) num8 < 0.0 || (double) num8 > 1.0) return false; float num9 = (float) ((double) num6 * (double) num2 - (double) num7 * (double) num1) / num5; if ((double) num9 < 0.0 || (double) num9 > 1.0) return false; result = new Vector2(p1.x + num8 * num1, p1.y + num8 * num2); return true; } internal static long RandomToLong(System.Random r) { byte[] buffer = new byte[8]; r.NextBytes(buffer); return (long) BitConverter.ToUInt64(buffer, 0) & long.MaxValue; } public static Vector3 Rad2Deg(Vector3 radians) { return new Vector3( (float)(radians.x * 180 / System.Math.PI), (float)(radians.y * 180 / System.Math.PI), (float)(radians.z * 180 / System.Math.PI)); } public static Vector3 Deg2Rad(Vector3 degrees) { return new Vector3( (float)(degrees.x * System.Math.PI / 180), (float)(degrees.y * System.Math.PI / 180), (float)(degrees.z * System.Math.PI / 180)); } public const float CosAngle20 = 0.9396926208f; public const float CompareEpsilon = 0.000001f; public static bool CompareApproximate(float f0, float f1, float epsilon = CompareEpsilon) { return System.Math.Abs(f0 - f1) < epsilon; } public static bool CompareApproximate(double f0, double f1, float epsilon = CompareEpsilon) { return System.Math.Abs(f0 - f1) < epsilon; } } }