MathUtils.cs 4.4 KB

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