68 lines
1.3 KiB
Go
68 lines
1.3 KiB
Go
|
package vector
|
||
|
|
||
|
import (
|
||
|
"math"
|
||
|
|
||
|
"github.com/dhconnelly/rtreego"
|
||
|
)
|
||
|
|
||
|
type Vector3 struct {
|
||
|
X, Y, Z float64
|
||
|
}
|
||
|
|
||
|
func (v *Vector3) SetOn(z, x, y float64) *Vector3 {
|
||
|
v.Z = z
|
||
|
v.X = x
|
||
|
v.Y = y
|
||
|
return v
|
||
|
}
|
||
|
|
||
|
func (v *Vector3) Copy() *Vector3 {
|
||
|
return &Vector3{v.X, v.Y, v.Z}
|
||
|
}
|
||
|
|
||
|
func (v *Vector3) Subtract(other *Vector3) *Vector3 {
|
||
|
return &Vector3{
|
||
|
X: v.X - other.X,
|
||
|
Y: v.Y - other.Y,
|
||
|
Z: v.Z - other.Z,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (v *Vector3) Length() float64 {
|
||
|
return math.Sqrt(v.X*v.X + v.Y*v.Y + v.Z*v.Z)
|
||
|
}
|
||
|
|
||
|
func (v *Vector3) RerpVectors(v1, v2 *Vector3, ratio float64) {
|
||
|
v.X = v1.X + ratio*(v2.X-v1.X)
|
||
|
v.Y = v1.Y + ratio*(v2.Y-v1.Y)
|
||
|
v.Z = v1.Z + ratio*(v2.Z-v1.Z)
|
||
|
}
|
||
|
|
||
|
func (v *Vector3) AngleTo(other *Vector3) float64 {
|
||
|
cosAlpha := (v.X*other.X + v.Y*other.Y + v.Z*other.Z) / (v.Length() * other.Length())
|
||
|
return math.Acos(cosAlpha)
|
||
|
}
|
||
|
|
||
|
// Implementing rtreego.Spatial interface for Vector3
|
||
|
func (v *Vector3) Bounds() *rtreego.Rect {
|
||
|
point := rtreego.Point{v.X, v.Y}
|
||
|
rect, _ := rtreego.NewRect(point, []float64{0.001, 0.001})
|
||
|
return rect
|
||
|
}
|
||
|
func (v *Vector3) Scale(factor float64) *Vector3 {
|
||
|
return &Vector3{
|
||
|
X: v.X * factor,
|
||
|
Y: v.Y * factor,
|
||
|
Z: v.Z * factor,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (v *Vector3) Add(other *Vector3) *Vector3 {
|
||
|
return &Vector3{
|
||
|
X: v.X + other.X,
|
||
|
Y: v.Y + other.Y,
|
||
|
Z: v.Z + other.Z,
|
||
|
}
|
||
|
}
|