project-ely/internal/vector/vector.go

39 lines
702 B
Go

package vector
import "math"
// This is a small-scale vector implementation for a Vec2F.
// If you need anything more complicated than normals and dot-products,
// then you should probably just switch to the glmath library instead.
type Vec2F struct {
X float64
Y float64
}
func (v Vec2F) Zero() bool {
return v.X == 0.0 && v.Y == 0.0
}
func (v Vec2F) LengthSquared() float64 {
return v.X*v.X + v.Y*v.Y
}
func (v Vec2F) Normalized() Vec2F {
output := Vec2F{X: v.X, Y: v.Y}
length := math.Hypot(v.X, v.Y)
if length == 0 {
return output
}
output.X = v.X / length
output.Y = v.Y / length
return output
}
func (v Vec2F) Dot(other Vec2F) float64 {
return v.X*other.X + v.Y*other.Y
}