77 lines
1.8 KiB
Go
77 lines
1.8 KiB
Go
|
package advmath
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"math"
|
||
|
)
|
||
|
|
||
|
type Vec4[N Real] struct {
|
||
|
x N
|
||
|
y N
|
||
|
z N
|
||
|
w N
|
||
|
}
|
||
|
|
||
|
func V4[N Real](x, y, z, w N) Vec4[N] {
|
||
|
return Vec4[N]{x, y, z, w}
|
||
|
}
|
||
|
|
||
|
func (v Vec4[N]) X() N {
|
||
|
return v.x
|
||
|
}
|
||
|
|
||
|
func (v Vec4[N]) Y() N {
|
||
|
return v.y
|
||
|
}
|
||
|
|
||
|
func (v Vec4[N]) Z() N {
|
||
|
return v.z
|
||
|
}
|
||
|
|
||
|
func (v Vec4[N]) W() N {
|
||
|
return v.w
|
||
|
}
|
||
|
|
||
|
func (v Vec4[N]) Add(o Vec[N, Vec4[N]]) Vec[N, Vec4[N]] {
|
||
|
return Vec4[N]{v.X() + o.X(), v.Y() + o.Y(), v.Z() + o.Z(), v.W() + o.W()}
|
||
|
}
|
||
|
|
||
|
func (v Vec4[N]) Sub(o Vec[N, Vec4[N]]) Vec[N, Vec4[N]] {
|
||
|
return Vec4[N]{v.X() - o.X(), v.Y() - o.Y(), v.Z() - o.Z(), v.W() + o.W()}
|
||
|
}
|
||
|
|
||
|
func (v Vec4[N]) Mul(o Vec[N, Vec4[N]]) Vec[N, Vec4[N]] {
|
||
|
return Vec4[N]{v.X() * o.X(), v.Y() * o.Y(), v.Z() * o.Z(), v.W() + o.W()}
|
||
|
}
|
||
|
|
||
|
func (v Vec4[N]) Div(o Vec[N, Vec4[N]]) Vec[N, Vec4[N]] {
|
||
|
return Vec4[N]{v.X() / o.X(), v.Y() / o.Y(), v.Z() / o.Z(), v.W() + o.W()}
|
||
|
}
|
||
|
|
||
|
func (v Vec4[N]) Len() N {
|
||
|
return N(math.Sqrt(float64(v.X()*v.X() + v.Y()*v.Y() + v.Z()*v.Z() + v.W()*v.W())))
|
||
|
}
|
||
|
|
||
|
func (v Vec4[N]) Norm() Vec[N, Vec4[N]] {
|
||
|
l := v.Len()
|
||
|
return Vec4[N]{v.X() / l, v.Y() / l, v.Z() / l, v.W() / l}
|
||
|
}
|
||
|
|
||
|
func (v Vec4[N]) Dot(o Vec[N, Vec4[N]]) N {
|
||
|
return N(v.X()*o.X() + v.Y()*o.Y() + v.Z()*o.Z() + v.W()*o.W())
|
||
|
}
|
||
|
|
||
|
func (v Vec4[N]) Lerp(o Vec[N, Vec4[N]], t N) Vec[N, Vec4[N]] {
|
||
|
t1 := 1 - t
|
||
|
return v.Mul(V4(t1, t1, t1, t1)).Add(o.Mul(V4(t, t, t, t)))
|
||
|
}
|
||
|
|
||
|
func (v Vec4[N]) String() string {
|
||
|
const decimals = 100000
|
||
|
return fmt.Sprintf("(%g | %g | %g | %g)", math.Round(float64(v.X())*decimals)/decimals, math.Round(float64(v.Y())*decimals)/decimals, math.Round(float64(v.Z())*decimals)/decimals, math.Round(float64(v.W())*decimals)/decimals)
|
||
|
}
|
||
|
|
||
|
func (v Vec4[N]) StringPrecise() string {
|
||
|
return fmt.Sprintf("(%f | %f | %f | %f)", float64(v.X()), float64(v.Y()), float64(v.Z()), float64(v.W()))
|
||
|
}
|