MathUtils.cs 4.5 KB

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