MathUtils.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using MeloongCore.Extensions;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. namespace MeloongCore {
  6. public static class MathUtils {
  7. #region 进制转换
  8. /// <summary>
  9. /// 将字符串作为数字,转换为指定进制的形式。
  10. /// 进制数需在 2 到 86 之间,且输入字符串中的每个字符都必须在该进制的字符集中。
  11. /// 进制参考:含大写字母 36,含大小写字母 62,含大小写字母和特殊符号 86
  12. /// </summary>
  13. public static string RadixConvert(this string input, int fromRadix, int toRadix) {
  14. const string digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz/+=!?@#$%^&*()[]{}<>;:',";
  15. if (fromRadix < 2 || fromRadix > digits.Length) throw new ArgumentOutOfRangeException(nameof(fromRadix), $"{nameof(fromRadix)} must be between 2 and 86.");
  16. // 零与负数的预处理
  17. if (string.IsNullOrEmpty(input)) return "0";
  18. bool isNegative = input.StartsWithF("-");
  19. if (isNegative) input = input.TrimStart('-');
  20. // 转换为十进制
  21. long realNum = 0; long scale = 1;
  22. foreach (var digit in input.Reverse().Select(l => digits.IndexOf(l))) {
  23. realNum += digit * scale;
  24. scale *= fromRadix;
  25. }
  26. return RadixConvert(realNum * (isNegative ? -1 : 0), toRadix);
  27. }
  28. /// <summary>
  29. /// 转换为 2 到 86 进制的字符串。
  30. /// 进制参考:含大写字母 36,含大小写字母 62,含大小写字母和特殊符号 86
  31. /// </summary>
  32. public static string RadixConvert(this long input, int toRadix) {
  33. const string digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz/+=!?@#$%^&*()[]{}<>;:',";
  34. if (toRadix < 2 || toRadix > digits.Length) throw new ArgumentOutOfRangeException(nameof(toRadix), $"{nameof(toRadix)} must be between 2 and 86.");
  35. // 零与负数的预处理
  36. if (input == 0) return "0";
  37. bool isNegative = input < 0;
  38. if (isNegative) input = -input;
  39. // 转换为指定进制
  40. var results = new List<char>();
  41. while (input > 0) {
  42. int newNum = (int) (input % toRadix);
  43. input = (input - newNum) / toRadix;
  44. results.Add(digits[newNum]);
  45. }
  46. // 负数的结束处理与返回
  47. if (isNegative) results.Add('-');
  48. return new string(results.ToArray().Reverse().ToArray());
  49. }
  50. /// <summary>
  51. /// 转换为 2 到 86 进制的字符串。
  52. /// 进制参考:含大写字母 36,含大小写字母 62,含大小写字母和特殊符号 86
  53. /// </summary>
  54. public static string RadixConvert(this int input, int toRadix) {
  55. return RadixConvert((long) input, toRadix);
  56. }
  57. /// <summary>
  58. /// 转换为 2 到 86 进制的字符串。
  59. /// 进制参考:含大写字母 36,含大小写字母 62,含大小写字母和特殊符号 86
  60. /// </summary>
  61. public static string RadixConvert(this byte input, int toRadix) {
  62. return RadixConvert((long) input, toRadix);
  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. return Math.Round(start + (end - start) * percentage, 7);
  79. }
  80. /// <summary>
  81. /// 计算二阶贝塞尔曲线。
  82. /// </summary>
  83. public static double Bezier(double t, double x1, double y1, double x2, double y2, double accuracy = 0.01) {
  84. if (t < 0.0000001 || double.IsNaN(t)) return 0;
  85. if (t > 0.9999999) return 1;
  86. double a = t;
  87. double b;
  88. do {
  89. b = 3 * a * ((0.33333333 + x1 - x2) * a * a + (x2 - 2 * x1) * a + x1);
  90. a += (t - b) * 0.5;
  91. } while (!(Math.Abs(b - t) < accuracy)); // 精度
  92. return 3 * a * ((0.33333333 + y1 - y2) * a * a + (y2 - 2 * y1) * a + y1);
  93. }
  94. }
  95. }