gg/mx/math.go

98 lines
1.3 KiB
Go
Raw Permalink Normal View History

2024-05-28 11:24:12 +03:00
package mx
2023-05-27 18:00:27 +03:00
2023-05-28 19:14:02 +03:00
import (
"math"
)
2023-05-27 18:00:27 +03:00
// The type is used in all Engine interactions
2024-05-28 11:24:12 +03:00
// where you need floating values to keep
// it interchangable in future.
// Not much promise for this though.
2023-05-27 18:00:27 +03:00
type Float = float64
const (
2024-06-01 16:07:28 +03:00
MaxFloat Float = math.MaxFloat64
Pi Float = math.Pi
RadDegrees Float = 57.2958
//PiRad = Pi * Rad
2024-06-01 16:07:28 +03:00
EqualityThreshold Float = 1e-9
2023-05-27 18:00:27 +03:00
)
func IsNan(v Float) bool {
return math.IsNaN(v)
}
func IsInf(f Float, sign int) bool {
return math.IsInf(f, sign)
}
func Degree(f Float) Float {
return (f/(2*Pi))*360
}
2023-05-27 18:00:27 +03:00
// Returns square of the value.
func Sqr(v Float) Float {
return v * v
}
func Sqrt(v Float) Float {
return math.Sqrt(v)
}
func Asin(v Float) Float {
return math.Asin(v)
}
func Atan(v Float) Float {
return math.Atan(v)
}
func Sgn(v Float) Float {
if v > 0 {
return 1
}
if v < 0 {
return -1
}
return 0
}
2023-06-03 12:26:31 +03:00
func Max(v1, v2 Float) Float {
if v1 > v2 {
return v1
}
return v2
}
func Min(v1, v2 Float) Float {
if v1 < v2 {
return v1
}
return v2
}
func Abs(v Float) Float {
if v >= 0 {
return v
}
return -v
}
2023-05-28 21:07:05 +03:00
func RadiansToDegrees(v Float) Float {
return v/Pi * 180
2023-05-28 21:07:05 +03:00
}
func DeegresToRadians(v Float) Float {
return v
}
// Returns whether the two floats are nearly equal.
func Neq(a, b Float) bool {
return Abs(a-b) <= EqualityThreshold
}