Files
2019/utilities/vector.go
Parnic dd5ea5ea86 Day 13 solution
This was incredibly cool and I had a really fun time with it. Uncomment the draw and sleep in Part2 to see the game play itself! Note that I'm not seeking around in the terminal window to make the drawing smooth, I'm just outputting each new frame as it happens, so there's some jitter, but it still looks great!

I messed around a bit with control codes to move the cursor around instead of the "draw the buffer over and over again" approach, and they work, mostly, but I'm sticking with this for now.
2022-06-27 00:01:19 -05:00

75 lines
1.2 KiB
Go

package utilities
import "math"
type Vec2[T Number] struct {
X T
Y T
}
type Vec3[T Number] struct {
X T
Y T
Z T
}
func (v Vec2[T]) Dot(other Vec2[T]) T {
return (v.X * other.X) + (v.Y * other.Y)
}
func (v Vec2[T]) Len() T {
return T(math.Sqrt(float64(v.LenSquared())))
}
func (v Vec2[T]) LenSquared() T {
return (v.X * v.X) + (v.Y * v.Y)
}
func (v Vec2[T]) To(other Vec2[T]) Vec2[T] {
return Vec2[T]{
X: v.X - other.X,
Y: v.Y - other.Y,
}
}
func (v Vec2[T]) AngleBetween(other Vec2[T]) float64 {
rad := math.Atan2(float64(other.Y-v.Y), float64(other.X-v.X))
return rad * 180 / math.Pi
}
func (v Vec2[T]) Equals(other Vec2[T]) bool {
return v.X == other.X &&
v.Y == other.Y
}
func VecBetween[T Number](a, b Vec2[T]) Vec2[T] {
return Vec2[T]{
X: a.X - b.X,
Y: a.Y - b.Y,
}
}
func (v Vec3[T]) Dot(other Vec3[T]) T {
return (v.X * other.X) + (v.Y * other.Y) + (v.Z * other.Z)
}
func (v Vec3[T]) Len() T {
return T(math.Sqrt(float64(v.LenSquared())))
}
func (v Vec3[T]) LenSquared() T {
return (v.X * v.X) + (v.Y * v.Y) + (v.Z * v.Z)
}
func (v *Vec3[T]) Add(other Vec3[T]) {
v.X += other.X
v.Y += other.Y
v.Z += other.Z
}
func (v Vec3[T]) Equals(other Vec3[T]) bool {
return v.X == other.X &&
v.Y == other.Y &&
v.Z == other.Z
}