MathUtils.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using System.Numerics;
  2. namespace MeloongCore;
  3. public static class MathUtils {
  4. #region 进制转换
  5. /// <summary>
  6. /// 将字符串作为数字,转换为指定进制的形式。
  7. /// 进制数需在 2 到 86 之间,且输入字符串中的每个字符都必须在该进制的字符集中。
  8. /// 进制参考:含大写字母 36,含大小写字母 62,含大小写字母和特殊符号 86。
  9. /// </summary>
  10. public static string ConvertRadix(this string input, int fromRadix, int toRadix) {
  11. const string digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz/+=!?@#$%^&*()[]{}<>;:',";
  12. if (fromRadix < 2 || fromRadix > digits.Length) throw new ArgumentOutOfRangeException(nameof(fromRadix), $"{nameof(fromRadix)} must be between 2 and 86.");
  13. // 零与负数的预处理
  14. if (string.IsNullOrEmpty(input)) return "0";
  15. bool isNegative = input.StartsWithF("-");
  16. if (isNegative) input = input.TrimStart('-');
  17. // 转换为十进制
  18. BigInteger realNum = 0;
  19. foreach (char c in input) {
  20. int digit = digits.IndexOf(c);
  21. if (digit == -1 || digit >= fromRadix) throw new ArgumentException($"Character '{c}' in input '{input}' is not a valid digit for radix {fromRadix}.");
  22. realNum = realNum * fromRadix + digit;
  23. }
  24. return ConvertRadix(realNum * (isNegative ? -1 : 1), toRadix);
  25. }
  26. /// <summary>
  27. /// 转换为 2 到 86 进制的字符串。
  28. /// 进制参考:含大写字母 36,含大小写字母 62,含大小写字母和特殊符号 86。
  29. /// </summary>
  30. public static string ConvertRadix(this long input, int toRadix)
  31. => ConvertRadix((BigInteger) input, toRadix);
  32. /// <summary>
  33. /// 转换为 2 到 86 进制的字符串。
  34. /// 进制参考:含大写字母 36,含大小写字母 62,含大小写字母和特殊符号 86。
  35. /// </summary>
  36. public static string ConvertRadix(this int input, int toRadix)
  37. => ConvertRadix((BigInteger) input, toRadix);
  38. /// <summary>
  39. /// 转换为 2 到 86 进制的字符串。
  40. /// 进制参考:含大写字母 36,含大小写字母 62,含大小写字母和特殊符号 86。
  41. /// </summary>
  42. public static string ConvertRadix(this byte input, int toRadix)
  43. => ConvertRadix((BigInteger) input, toRadix);
  44. private static string ConvertRadix(this BigInteger input, int toRadix) {
  45. const string digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz/+=!?@#$%^&*()[]{}<>;:',";
  46. if (toRadix < 2 || toRadix > digits.Length) throw new ArgumentOutOfRangeException(nameof(toRadix), $"{nameof(toRadix)} must be between 2 and 86.");
  47. // 零与负数的预处理
  48. if (input == 0) return "0";
  49. bool isNegative = input < 0;
  50. // 转换为指定进制
  51. var results = new List<char>();
  52. BigInteger remaining = BigInteger.Abs(input);
  53. BigInteger bigRadix = toRadix;
  54. while (remaining > 0) {
  55. remaining = BigInteger.DivRem(remaining, bigRadix, out BigInteger remainder);
  56. results.Add(digits[(int) remainder]);
  57. }
  58. // 负数的结束处理与返回
  59. if (isNegative) results.Add('-');
  60. results.Reverse();
  61. return new([..results]);
  62. }
  63. #endregion
  64. /// <summary>
  65. /// 将值限制在指定范围内。
  66. /// </summary>
  67. public static T Clamp<T>(this T value, T min, T max) where T : IComparable<T> {
  68. if (value.CompareTo(min) < 0) return min;
  69. if (value.CompareTo(max) > 0) return max;
  70. return value;
  71. }
  72. /// <summary>
  73. /// 在一个范围中进行线性插值。
  74. /// 返回值将被舍入到小数点后 7 位,以解决浮点数误差问题。
  75. /// </summary>
  76. public static double Lerp(double start, double end, double percentage)
  77. => Math.Round(start + (end - start) * percentage, 7);
  78. /// <summary>
  79. /// 计算二阶贝塞尔曲线。
  80. /// </summary>
  81. public static double Bezier(double t, double x1, double y1, double x2, double y2, double accuracy = 0.01) {
  82. if (t < 0.0000001 || double.IsNaN(t)) return 0;
  83. if (t > 0.9999999) return 1;
  84. double a = t;
  85. double b;
  86. do {
  87. b = 3 * a * ((0.33333333 + x1 - x2) * a * a + (x2 - 2 * x1) * a + x1);
  88. a += (t - b) * 0.5;
  89. } while (!(Math.Abs(b - t) < accuracy)); // 精度
  90. return 3 * a * ((0.33333333 + y1 - y2) * a * a + (y2 - 2 * y1) * a + y1);
  91. }
  92. }