math.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Copyright 2016 The G3N Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Package math32 implements basic math functions which operate
  5. // directly on float32 numbers without casting and contains
  6. // types of common entities used in 3D Graphics such as vectors,
  7. // matrices, quaternions and others.
  8. package math32
  9. import (
  10. "math"
  11. )
  12. const Pi = math.Pi
  13. const degreeToRadiansFactor = math.Pi / 180
  14. const radianToDegreesFactor = 180.0 / math.Pi
  15. var Infinity = float32(math.Inf(1))
  16. // DegToRad converts a number from degrees to radians
  17. func DegToRad(degrees float32) float32 {
  18. return degrees * degreeToRadiansFactor
  19. }
  20. // RadToDeg converts a number from radians to degrees
  21. func RadToDeg(radians float32) float32 {
  22. return radians * radianToDegreesFactor
  23. }
  24. // Clamp clamps x to the provided closed interval [a, b]
  25. func Clamp(x, a, b float32) float32 {
  26. if x < a {
  27. return a
  28. }
  29. if x > b {
  30. return b
  31. }
  32. return x
  33. }
  34. // ClampInt clamps x to the provided closed interval [a, b]
  35. func ClampInt(x, a, b int) int {
  36. if x < a {
  37. return a
  38. }
  39. if x > b {
  40. return b
  41. }
  42. return x
  43. }
  44. func Abs(v float32) float32 {
  45. return float32(math.Abs(float64(v)))
  46. }
  47. func Acos(v float32) float32 {
  48. return float32(math.Acos(float64(v)))
  49. }
  50. func Asin(v float32) float32 {
  51. return float32(math.Asin(float64(v)))
  52. }
  53. func Atan(v float32) float32 {
  54. return float32(math.Atan(float64(v)))
  55. }
  56. func Atan2(y, x float32) float32 {
  57. return float32(math.Atan2(float64(y), float64(x)))
  58. }
  59. func Ceil(v float32) float32 {
  60. return float32(math.Ceil(float64(v)))
  61. }
  62. func Cos(v float32) float32 {
  63. return float32(math.Cos(float64(v)))
  64. }
  65. func Floor(v float32) float32 {
  66. return float32(math.Floor(float64(v)))
  67. }
  68. func Inf(sign int) float32 {
  69. return float32(math.Inf(sign))
  70. }
  71. func Round(v float32) float32 {
  72. return Floor(v + 0.5)
  73. }
  74. func IsNaN(v float32) bool {
  75. return math.IsNaN(float64(v))
  76. }
  77. func Sin(v float32) float32 {
  78. return float32(math.Sin(float64(v)))
  79. }
  80. func Sqrt(v float32) float32 {
  81. return float32(math.Sqrt(float64(v)))
  82. }
  83. func Max(a, b float32) float32 {
  84. return float32(math.Max(float64(a), float64(b)))
  85. }
  86. func Min(a, b float32) float32 {
  87. return float32(math.Min(float64(a), float64(b)))
  88. }
  89. func Mod(a, b float32) float32 {
  90. return float32(math.Mod(float64(a), float64(b)))
  91. }
  92. func NaN() float32 {
  93. return float32(math.NaN())
  94. }
  95. func Pow(a, b float32) float32 {
  96. return float32(math.Pow(float64(a), float64(b)))
  97. }
  98. func Tan(v float32) float32 {
  99. return float32(math.Tan(float64(v)))
  100. }