r4m0n

MathD

Jul 17th, 2014
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.94 KB | None | 0 0
  1. using System;
  2.  
  3. namespace UnityEngine
  4. {
  5.     public struct MathD
  6.     {
  7.         public const double PI = 3.14159265358979;
  8.         public const double Infinity = double.PositiveInfinity;
  9.         public const double NegativeInfinity = double.NegativeInfinity;
  10.         public const double Deg2Rad = PI / 180;
  11.         public const double Rad2Deg = 180 / PI;
  12.         public const double Epsilon = double.Epsilon;
  13.  
  14.         public static double Sin(double d)
  15.         {
  16.             return Math.Sin(d);
  17.         }
  18.  
  19.         public static double Cos(double d)
  20.         {
  21.             return Math.Cos(d);
  22.         }
  23.  
  24.         public static double Tan(double d)
  25.         {
  26.             return Math.Tan(d);
  27.         }
  28.  
  29.         public static double Asin(double d)
  30.         {
  31.             return Math.Asin(d);
  32.         }
  33.  
  34.         public static double Acos(double d)
  35.         {
  36.             return Math.Acos(d);
  37.         }
  38.  
  39.         public static double Atan(double d)
  40.         {
  41.             return Math.Atan(d);
  42.         }
  43.  
  44.         public static double Atan2(double y, double x)
  45.         {
  46.             return Math.Atan2(y, x);
  47.         }
  48.  
  49.         public static double Sqrt(double d)
  50.         {
  51.             return Math.Sqrt(d);
  52.         }
  53.  
  54.         public static double Abs(double d)
  55.         {
  56.             return Math.Abs(d);
  57.         }
  58.  
  59.         public static int Abs(int value)
  60.         {
  61.             return Math.Abs(value);
  62.         }
  63.  
  64.         public static double Min(double a, double b)
  65.         {
  66.             if (a < b)
  67.                 return a;
  68.             else
  69.                 return b;
  70.         }
  71.  
  72.         public static double Min(params double[] values)
  73.         {
  74.             int length = values.Length;
  75.             if (length == 0)
  76.                 return 0.0d;
  77.             double num = values[0];
  78.             for (int index = 1; index < length; ++index)
  79.             {
  80.                 if (values[index] < num)
  81.                     num = values[index];
  82.             }
  83.             return num;
  84.         }
  85.  
  86.         public static int Min(int a, int b)
  87.         {
  88.             if (a < b)
  89.                 return a;
  90.             else
  91.                 return b;
  92.         }
  93.  
  94.         public static int Min(params int[] values)
  95.         {
  96.             int length = values.Length;
  97.             if (length == 0)
  98.                 return 0;
  99.             int num = values[0];
  100.             for (int index = 1; index < length; ++index)
  101.             {
  102.                 if (values[index] < num)
  103.                     num = values[index];
  104.             }
  105.             return num;
  106.         }
  107.  
  108.         public static double Max(double a, double b)
  109.         {
  110.             if (a > b)
  111.                 return a;
  112.             else
  113.                 return b;
  114.         }
  115.  
  116.         public static double Max(params double[] values)
  117.         {
  118.             int length = values.Length;
  119.             if (length == 0)
  120.                 return 0d;
  121.             double num = values[0];
  122.             for (int index = 1; index < length; ++index)
  123.             {
  124.                 if ((double)values[index] > (double)num)
  125.                     num = values[index];
  126.             }
  127.             return num;
  128.         }
  129.  
  130.         public static int Max(int a, int b)
  131.         {
  132.             if (a > b)
  133.                 return a;
  134.             else
  135.                 return b;
  136.         }
  137.  
  138.         public static int Max(params int[] values)
  139.         {
  140.             int length = values.Length;
  141.             if (length == 0)
  142.                 return 0;
  143.             int num = values[0];
  144.             for (int index = 1; index < length; ++index)
  145.             {
  146.                 if (values[index] > num)
  147.                     num = values[index];
  148.             }
  149.             return num;
  150.         }
  151.  
  152.         public static double Pow(double d, double p)
  153.         {
  154.             return Math.Pow(d, p);
  155.         }
  156.  
  157.         public static double Exp(double power)
  158.         {
  159.             return Math.Exp(power);
  160.         }
  161.  
  162.         public static double Log(double d, double p)
  163.         {
  164.             return Math.Log(d, p);
  165.         }
  166.  
  167.         public static double Log(double d)
  168.         {
  169.             return Math.Log(d);
  170.         }
  171.  
  172.         public static double Log10(double d)
  173.         {
  174.             return Math.Log10(d);
  175.         }
  176.  
  177.         public static double Ceil(double d)
  178.         {
  179.             return Math.Ceiling(d);
  180.         }
  181.  
  182.         public static double Floor(double d)
  183.         {
  184.             return Math.Floor(d);
  185.         }
  186.  
  187.         public static double Round(double d)
  188.         {
  189.             return Math.Round(d);
  190.         }
  191.  
  192.         public static int CeilToInt(double d)
  193.         {
  194.             return (int)Math.Ceiling(d);
  195.         }
  196.  
  197.         public static int FloorToInt(double d)
  198.         {
  199.             return (int)Math.Floor(d);
  200.         }
  201.  
  202.         public static int RoundToInt(double d)
  203.         {
  204.             return (int)Math.Round(d);
  205.         }
  206.  
  207.         public static double Sign(double d)
  208.         {
  209.             return d >= 0.0 ? 1d : -1d;
  210.         }
  211.  
  212.         public static double Clamp(double value, double min, double max)
  213.         {
  214.             if (value < min)
  215.                 value = min;
  216.             else if (value > max)
  217.                 value = max;
  218.             return value;
  219.         }
  220.  
  221.         public static int Clamp(int value, int min, int max)
  222.         {
  223.             if (value < min)
  224.                 value = min;
  225.             else if (value > max)
  226.                 value = max;
  227.             return value;
  228.         }
  229.  
  230.         public static double Clamp01(double value)
  231.         {
  232.             if (value < 0.0)
  233.                 return 0.0d;
  234.             if (value > 1.0)
  235.                 return 1d;
  236.             else
  237.                 return value;
  238.         }
  239.  
  240.         public static double Lerp(double from, double to, double t)
  241.         {
  242.             return from + (to - from) * MathD.Clamp01(t);
  243.         }
  244.  
  245.         public static double LerpAngle(double a, double b, double t)
  246.         {
  247.             double num = MathD.Repeat(b - a, 360d);
  248.             if (num > 180.0d)
  249.                 num -= 360d;
  250.             return a + num * MathD.Clamp01(t);
  251.         }
  252.  
  253.         public static double MoveTowards(double current, double target, double maxDelta)
  254.         {
  255.             if (MathD.Abs(target - current) <= maxDelta)
  256.                 return target;
  257.             else
  258.                 return current + MathD.Sign(target - current) * maxDelta;
  259.         }
  260.  
  261.         public static double MoveTowardsAngle(double current, double target, double maxDelta)
  262.         {
  263.             target = current + MathD.DeltaAngle(current, target);
  264.             return MathD.MoveTowards(current, target, maxDelta);
  265.         }
  266.  
  267.         public static double SmoothStep(double from, double to, double t)
  268.         {
  269.             t = MathD.Clamp01(t);
  270.             t = (-2.0 * t * t * t + 3.0 * t * t);
  271.             return to * t + from * (1.0 - t);
  272.         }
  273.  
  274.         public static double Gamma(double value, double absmax, double gamma)
  275.         {
  276.             bool flag = false;
  277.             if (value < 0.0)
  278.                 flag = true;
  279.             double num1 = MathD.Abs(value);
  280.             if (num1 > absmax)
  281.             {
  282.                 if (flag)
  283.                     return -num1;
  284.                 else
  285.                     return num1;
  286.             }
  287.             else
  288.             {
  289.                 double num2 = MathD.Pow(num1 / absmax, gamma) * absmax;
  290.                 if (flag)
  291.                     return -num2;
  292.                 else
  293.                     return num2;
  294.             }
  295.         }
  296.  
  297.         public static bool Approximately(double a, double b)
  298.         {
  299.             return MathD.Abs(b - a) < MathD.Max(1E-06d * MathD.Max(MathD.Abs(a), MathD.Abs(b)), Epsilon);
  300.         }
  301.  
  302.         public static double SmoothDamp(double current, double target, ref double currentVelocity, double smoothTime, double maxSpeed)
  303.         {
  304.             return MathD.SmoothDamp(current, target, ref currentVelocity, smoothTime, maxSpeed, Time.deltaTime);
  305.         }
  306.  
  307.         public static double SmoothDamp(double current, double target, ref double currentVelocity, double smoothTime)
  308.         {
  309.             return MathD.SmoothDamp(current, target, ref currentVelocity, smoothTime, double.PositiveInfinity, Time.deltaTime);
  310.         }
  311.  
  312.         public static double SmoothDamp(double current, double target, ref double currentVelocity, double smoothTime, double maxSpeed, double deltaTime)
  313.         {
  314.             smoothTime = MathD.Max(0.0001d, smoothTime);
  315.             double num1 = 2d / smoothTime;
  316.             double num2 = num1 * deltaTime;
  317.             double num3 = (1.0d / (1.0d + num2 + 0.479999989271164d * num2 * num2 + 0.234999999403954d * num2 * num2 * num2));
  318.             double num4 = current - target;
  319.             double num5 = target;
  320.             double max = maxSpeed * smoothTime;
  321.             double num6 = MathD.Clamp(num4, -max, max);
  322.             target = current - num6;
  323.             double num7 = (currentVelocity + num1 * num6) * deltaTime;
  324.             currentVelocity = (currentVelocity - num1 * num7) * num3;
  325.             double num8 = target + (num6 + num7) * num3;
  326.             if (num5 - current > 0.0 == num8 > num5)
  327.             {
  328.                 num8 = num5;
  329.                 currentVelocity = (num8 - num5) / deltaTime;
  330.             }
  331.             return num8;
  332.         }
  333.  
  334.         public static double SmoothDampAngle(double current, double target, ref double currentVelocity, double smoothTime, double maxSpeed)
  335.         {
  336.             return MathD.SmoothDampAngle(current, target, ref currentVelocity, smoothTime, maxSpeed, Time.deltaTime);
  337.         }
  338.  
  339.         public static double SmoothDampAngle(double current, double target, ref double currentVelocity, double smoothTime)
  340.         {
  341.             return MathD.SmoothDampAngle(current, target, ref currentVelocity, smoothTime, double.PositiveInfinity, Time.deltaTime);
  342.         }
  343.  
  344.         public static double SmoothDampAngle(double current, double target, ref double currentVelocity, double smoothTime, double maxSpeed, double deltaTime)
  345.         {
  346.             return MathD.SmoothDamp(current, current + MathD.DeltaAngle(current, target), ref currentVelocity, smoothTime, maxSpeed, deltaTime);
  347.         }
  348.  
  349.         public static double Repeat(double t, double length)
  350.         {
  351.             return t - MathD.Floor(t / length) * length;
  352.         }
  353.  
  354.         public static double PingPong(double t, double length)
  355.         {
  356.             t = MathD.Repeat(t, length * 2d);
  357.             return length - MathD.Abs(t - length);
  358.         }
  359.  
  360.         public static double InverseLerp(double from, double to, double value)
  361.         {
  362.             if (from < to)
  363.             {
  364.                 if (value < from)
  365.                     return 0d;
  366.                 if (value > to)
  367.                     return 1d;
  368.                 value -= from;
  369.                 value /= to - from;
  370.                 return value;
  371.             }
  372.             else
  373.             {
  374.                 if (from <= to)
  375.                     return 0d;
  376.                 if (value < to)
  377.                     return 1d;
  378.                 if (value > from)
  379.                     return 0d;
  380.                 else
  381.                     return (1.0d - (value - to) / (from - to));
  382.             }
  383.         }
  384.  
  385.         public static double DeltaAngle(double current, double target)
  386.         {
  387.             double num = MathD.Repeat(target - current, 360d);
  388.             if (num > 180.0d)
  389.                 num -= 360d;
  390.             return num;
  391.         }
  392.  
  393.         internal static bool LineIntersection(Vector2D p1, Vector2D p2, Vector2D p3, Vector2D p4, ref Vector2D result)
  394.         {
  395.             double num1 = p2.x - p1.x;
  396.             double num2 = p2.y - p1.y;
  397.             double num3 = p4.x - p3.x;
  398.             double num4 = p4.y - p3.y;
  399.             double num5 = num1 * num4 - num2 * num3;
  400.             if (num5 == 0.0d)
  401.                 return false;
  402.             double num6 = p3.x - p1.x;
  403.             double num7 = p3.y - p1.y;
  404.             double num8 = (num6 * num4 - num7 * num3) / num5;
  405.             result = new Vector2D(p1.x + num8 * num1, p1.y + num8 * num2);
  406.             return true;
  407.         }
  408.  
  409.         internal static bool LineSegmentIntersection(Vector2D p1, Vector2D p2, Vector2D p3, Vector2D p4, ref Vector2D result)
  410.         {
  411.             double num1 = p2.x - p1.x;
  412.             double num2 = p2.y - p1.y;
  413.             double num3 = p4.x - p3.x;
  414.             double num4 = p4.y - p3.y;
  415.             double num5 = (num1 * num4 - num2 * num3);
  416.             if (num5 == 0.0d)
  417.                 return false;
  418.             double num6 = p3.x - p1.x;
  419.             double num7 = p3.y - p1.y;
  420.             double num8 = (num6 * num4 - num7 * num3) / num5;
  421.             if (num8 < 0.0d || num8 > 1.0d)
  422.                 return false;
  423.             double num9 = (num6 * num2 - num7 * num1) / num5;
  424.             if (num9 < 0.0d || num9 > 1.0d)
  425.                 return false;
  426.             result = new Vector2D(p1.x + num8 * num1, p1.y + num8 * num2);
  427.             return true;
  428.         }
  429.     }
  430. }
Advertisement
Add Comment
Please, Sign In to add comment