1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-06-13 03:00:42 +00:00
OpenDiablo2/d2common/d2math/math.go
danhale-git 029cb62972
Vector float64 (#565)
* Fixed nil pointer in Copy()

* Position added

Added Floor() and String() methods to Vector.

Also added Position which declares an embedded Vector2 and returns various forms of it.

* d2vector.Vector2 renamed to d2vector.BigFloat

* vector.go renamed to big_float.go

* Float64 stub and more renaming

* Vector value getters

* Separate vector types with initial methods.

* Divide and lint warnings.

* Distance and Length.

* Scale, Abs and Negate.

* CompareFloat64Fuzzy delta direction reversed.

* Refactor vector_test.go.

* Renamed Approx methods.

* Distance and Length.

* Distance and Length.

* Removed BigFloat and Vector, renamed Float64 to Vector, simplified tests.

* Angle, SignedAngle and other small functions.

* Receiver rename.

* SingedAngle and test fixed

* Rotate.

* SetLength.

* Cross.

* NinetyAnti and NinetyClock.

* Lerp and Clamp.

* Reflect and ReflectSurface.

* Cardinal convenience functions.

* Comments.

* Panic on NaN and Inf in Position.

* Lint warnings and comments.
2020-07-09 08:30:55 -04:00

68 lines
1.5 KiB
Go

package d2math
import "math"
const (
// Epsilon is used as the threshold for 'almost equal' operations.
Epsilon float64 = 0.0001
// RadToDeg is used to convert anges in radians to degrees by multiplying the radians by RadToDeg. Similarly,degrees
// are converted to radians when dividing by RadToDeg.
RadToDeg float64 = 57.29578
// RadFull is the radian equivalent of 360 degrees.
RadFull float64 = 6.283185253783088
)
// CompareFloat64Fuzzy returns an integer between -1 and 1 describing
// the comparison of floats a and b. 0 will be returned if the
// absolute difference between a and b is less than Epsilon.
func CompareFloat64Fuzzy(a, b float64) int {
delta := a - b
if math.Abs(delta) < Epsilon {
return 0
}
if delta > 0 {
return 1
}
return -1
}
// ClampFloat64 returns a clamped to min and max.
func ClampFloat64(a, min, max float64) float64 {
if a > max {
return max
} else if a < min {
return min
}
return a
}
// Sign returns the sign of a.
func Sign(a float64) int {
switch {
case a < 0:
return -1
case a > 0:
return +1
}
return 0
}
// Lerp returns the linear interpolation from a to b using interpolator x.
func Lerp(a, b, x float64) float64 {
return a + x*(b-a)
}
// Unlerp returns the intepolator Lerp would require to return x when given
// a and b. The x argument of this function can be thought of as the return
// value of lerp. The return value of this function can be used as x in
// Lerp.
func Unlerp(a, b, x float64) float64 {
return (x - a) / (b - a)
}