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