35 lines
633 B
Go
35 lines
633 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) NonZero() bool {
|
|
return v.LengthSquared() > 0
|
|
}
|
|
|
|
func (v *Vec2F) LengthSquared() float64 {
|
|
return v.X*v.X + v.Y*v.Y
|
|
}
|
|
|
|
func (v *Vec2F) Normalize() {
|
|
length := math.Hypot(v.X, v.Y)
|
|
if length == 0 {
|
|
return
|
|
}
|
|
|
|
v.X = v.X / length
|
|
v.Y = v.Y / length
|
|
}
|
|
|
|
func (v *Vec2F) Dot(other *Vec2F) float64 {
|
|
return v.X*other.X + v.Y*other.Y
|
|
}
|