MathUtil.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /************************************************/
  2. /* */
  3. /* Copyright (c) 2018 - 2021 monitor1394 */
  4. /* https://github.com/monitor1394 */
  5. /* */
  6. /************************************************/
  7. using System.Text;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Text.RegularExpressions;
  11. using UnityEngine;
  12. using UnityEngine.EventSystems;
  13. using UnityEngine.UI;
  14. namespace XCharts
  15. {
  16. public static class MathUtil
  17. {
  18. public static double Abs(double d)
  19. {
  20. return d > 0 ? d : -d;
  21. }
  22. public static double Clamp(double d, double min, double max)
  23. {
  24. if (d >= min && d <= max) return d;
  25. else if (d < min) return min;
  26. else return max;
  27. }
  28. public static bool Approximately(double a, double b)
  29. {
  30. return Math.Abs(b - a) < Math.Max(0.000001f * Math.Max(Math.Abs(a), Math.Abs(b)), Mathf.Epsilon * 8);
  31. }
  32. public static double Clamp01(double value)
  33. {
  34. if (value < 0F)
  35. return 0F;
  36. else if (value > 1F)
  37. return 1F;
  38. else
  39. return value;
  40. }
  41. public static double Lerp(double a, double b, double t)
  42. {
  43. return a + (b - a) * Clamp01(t);
  44. }
  45. }
  46. }