置顶公告:【置顶】关于临时开启评论区所有功能的公告(2022.10.22) | 【置顶】关于本站Widget恢复使用的公告
  • 你好~!欢迎来到萌娘百科镜像站!如需查看或编辑,请联系本站管理员注册账号。
  • 本镜像站和其他萌娘百科的镜像站无关,请注意分别。

Module:Hct/MathUtils

贴贴♀百科,万娘皆可贴的百科全书!转载请标注来源页面的网页链接,并声明引自贴贴百科。内容不可商用。
跳到导航 跳到搜索
Template-info.svg 模块文档  [创建] [刷新]
  1. --[[
  2. Copyright 2021 Google LLC
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ]]
  13. --[[
  14. This file has been modified. The original version is at
  15. https://github.com/material-foundation/material-color-utilities
  16. ]]
  17. local MathUtils = {}
  18. --[[
  19. The signum function.
  20. @return 1 if num > 0, -1 if num < 0, and 0 if num = 0
  21. ]]
  22. function MathUtils.signum(num)
  23. return num < 0 and -1 or (num == 0 and 0 or 1);
  24. end
  25. --[[
  26. The linear interpolation function.
  27. @return start if amount = 0 and stop if amount = 1
  28. ]]
  29. function MathUtils.lerp(start, stop, amount)
  30. return (1.0 - amount) * start + amount * stop;
  31. end
  32. --[[
  33. Clamps an integer between two integers.
  34. @return input when min <= input <= max, and either min or max otherwise.
  35. ]]
  36. function MathUtils.clampInt(min, max, input)
  37. if input < min then
  38. return min;
  39. elseif input > max then
  40. return max;
  41. end
  42. return input;
  43. end
  44. --[[
  45. Clamps an integer between two floating-point numbers.
  46. @return input when min <= input <= max, and either min or max otherwise.
  47. ]]
  48. MathUtils.clampDouble = MathUtils.clampInt;
  49. --[[
  50. Sanitizes a degree measure as an integer.
  51. @return a degree measure between 0 (inclusive) and 360 (exclusive).
  52. ]]
  53. function MathUtils.sanitizeDegreesInt(degrees)
  54. --[[ Lua中degrees % 360不会出现负数……?
  55. degrees = degrees % 360;
  56. if degrees < 0 then
  57. degrees = degrees + 360;
  58. end
  59. return degrees;
  60. ]]
  61. return degrees % 360;
  62. end
  63. --[[
  64. Sanitizes a degree measure as a floating-point number.
  65. @return a degree measure between 0.0 (inclusive) and 360.0 (exclusive).
  66. ]]
  67. MathUtils.sanitizeDegreesDouble = MathUtils.sanitizeDegreesInt
  68. --[[
  69. Sign of direction change needed to travel from one angle to another.
  70. For angles that are 180 degrees apart from each other, both directions have the same travel
  71. distance, so either direction is shortest. The value 1.0 is returned in this case.
  72. @param from The angle travel starts from, in degrees.
  73. @param to The angle travel ends at, in degrees.
  74. @return -1 if decreasing from leads to the shortest travel distance, 1 if increasing from leads
  75. to the shortest travel distance.
  76. ]]
  77. function MathUtils.rotationDirection(from, to)
  78. local increasingDifference = MathUtils.sanitizeDegreesDouble(to - from);
  79. return (increasingDifference <= 180.0) and 1.0 or -1.0;
  80. end
  81. --[[ Distance of two points on a circle, represented using degrees. ]]
  82. function MathUtils.differenceDegrees(a, b)
  83. return 180.0 - math.abs(math.abs(a - b) - 180.0);
  84. end
  85. --[[ Multiplies a 1x3 row vector with a 3x3 matrix. ]]
  86. function MathUtils.matrixMultiply(row, matrix)
  87. local a = row[1] * matrix[1][1] + row[2] * matrix[1][2] + row[3] * matrix[1][3];
  88. local b = row[1] * matrix[2][1] + row[2] * matrix[2][2] + row[3] * matrix[2][3];
  89. local c = row[1] * matrix[3][1] + row[2] * matrix[3][2] + row[3] * matrix[3][3];
  90. return {a, b, c};
  91. end
  92. --[[ Cube root of num. ]]
  93. function MathUtils.cbrt(num)
  94. return MathUtils.signum(num) * math.abs(num) ^ (1 / 3);
  95. end
  96. return MathUtils