math.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. func DegToRad(degrees float32) float32 {
  17. return degrees * degreeToRadiansFactor
  18. }
  19. func RadToDeg(radians float32) float32 {
  20. return radians * radianToDegreesFactor
  21. }
  22. func Clamp(x, a, b float32) float32 {
  23. // Clamp value to range <a, b>
  24. if x < a {
  25. return a
  26. }
  27. if x > b {
  28. return b
  29. }
  30. return x
  31. }
  32. func ClampInt(x, a, b int) int {
  33. if x < a {
  34. return a
  35. }
  36. if x > b {
  37. return b
  38. }
  39. return x
  40. }
  41. func ClampBotton(x, a float32) float32 {
  42. // Clamp value to range <a, inf)
  43. if x < a {
  44. return a
  45. }
  46. return x
  47. }
  48. func Abs(v float32) float32 {
  49. return float32(math.Abs(float64(v)))
  50. }
  51. func Acos(v float32) float32 {
  52. return float32(math.Acos(float64(v)))
  53. }
  54. func Asin(v float32) float32 {
  55. return float32(math.Asin(float64(v)))
  56. }
  57. func Atan(v float32) float32 {
  58. return float32(math.Atan(float64(v)))
  59. }
  60. func Atan2(y, x float32) float32 {
  61. return float32(math.Atan2(float64(y), float64(x)))
  62. }
  63. func Ceil(v float32) float32 {
  64. return float32(math.Ceil(float64(v)))
  65. }
  66. func Cos(v float32) float32 {
  67. return float32(math.Cos(float64(v)))
  68. }
  69. func Floor(v float32) float32 {
  70. return float32(math.Floor(float64(v)))
  71. }
  72. func Inf(sign int) float32 {
  73. return float32(math.Inf(sign))
  74. }
  75. func Round(v float32) float32 {
  76. return Floor(v + 0.5)
  77. }
  78. func IsNaN(v float32) bool {
  79. return math.IsNaN(float64(v))
  80. }
  81. func Sin(v float32) float32 {
  82. return float32(math.Sin(float64(v)))
  83. }
  84. func Sqrt(v float32) float32 {
  85. return float32(math.Sqrt(float64(v)))
  86. }
  87. func Max(a, b float32) float32 {
  88. return float32(math.Max(float64(a), float64(b)))
  89. }
  90. func Min(a, b float32) float32 {
  91. return float32(math.Min(float64(a), float64(b)))
  92. }
  93. func Mod(a, b float32) float32 {
  94. return float32(math.Mod(float64(a), float64(b)))
  95. }
  96. func NaN() float32 {
  97. return float32(math.NaN())
  98. }
  99. func Pow(a, b float32) float32 {
  100. return float32(math.Pow(float64(a), float64(b)))
  101. }
  102. func Tan(v float32) float32 {
  103. return float32(math.Tan(float64(v)))
  104. }