2
0

MathUtils.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 (realNum * (isNegative ? -1 : 1)).ConvertRadix(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 ulong 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 int input, int toRadix)
  43. => ConvertRadix((BigInteger) input, toRadix);
  44. /// <summary>
  45. /// 转换为 2 到 86 进制的字符串。
  46. /// 进制参考:含大写字母 36,含大小写字母 62,含大小写字母和特殊符号 86。
  47. /// </summary>
  48. public static string ConvertRadix(this byte input, int toRadix)
  49. => ConvertRadix((BigInteger) input, toRadix);
  50. /// <summary>
  51. /// 转换为 2 到 86 进制的字符串。
  52. /// 进制参考:含大写字母 36,含大小写字母 62,含大小写字母和特殊符号 86。
  53. /// </summary>
  54. public static string ConvertRadix(this BigInteger input, int toRadix) {
  55. const string digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz/+=!?@#$%^&*()[]{}<>;:',";
  56. if (toRadix < 2 || toRadix > digits.Length) throw new ArgumentOutOfRangeException(nameof(toRadix), $"{nameof(toRadix)} must be between 2 and 86.");
  57. // 零与负数的预处理
  58. if (input == 0) return "0";
  59. bool isNegative = input < 0;
  60. // 转换为指定进制
  61. var results = new List<char>();
  62. BigInteger remaining = BigInteger.Abs(input);
  63. BigInteger bigRadix = toRadix;
  64. while (remaining > 0) {
  65. remaining = BigInteger.DivRem(remaining, bigRadix, out BigInteger remainder);
  66. results.Add(digits[(int) remainder]);
  67. }
  68. // 负数的结束处理与返回
  69. if (isNegative) results.Add('-');
  70. results.Reverse();
  71. return new([..results]);
  72. }
  73. #endregion
  74. /// <summary>
  75. /// 将值限制在指定范围内。
  76. /// </summary>
  77. public static T Clamp<T>(this T value, T min, T max) where T : IComparable<T> {
  78. if (value.CompareTo(min) < 0) return min;
  79. if (value.CompareTo(max) > 0) return max;
  80. return value;
  81. }
  82. /// <summary>
  83. /// 在一个范围中进行线性插值。
  84. /// 返回值将被舍入到小数点后 7 位,以解决浮点数误差问题。
  85. /// </summary>
  86. public static double Lerp(double start, double end, double percentage)
  87. => Math.Round(start + (end - start) * percentage, 7);
  88. /// <summary>
  89. /// 计算二阶贝塞尔曲线。
  90. /// </summary>
  91. public static double Bezier(double t, double x1, double y1, double x2, double y2, double accuracy = 0.01) {
  92. if (t < 0.0000001 || double.IsNaN(t)) return 0;
  93. if (t > 0.9999999) return 1;
  94. double a = t;
  95. double b;
  96. int maxIterations = 1000;
  97. do {
  98. b = 3 * a * ((0.33333333 + x1 - x2) * a * a + (x2 - 2 * x1) * a + x1);
  99. a += (t - b) * 0.5;
  100. maxIterations--;
  101. } while (!(Math.Abs(b - t) < accuracy) && maxIterations > 0);
  102. return 3 * a * ((0.33333333 + y1 - y2) * a * a + (y2 - 2 * y1) * a + y1);
  103. }
  104. }