fiz-math 0.0.11

Linear algebra in two and three dimensions.
// Radians converts each value in the vector from degrees to radians and
// returns it.
func (a Vec2) Radians() Vec2 {
	return Vec2{
		Radians(a.X),
		Radians(a.Y),
	}
}

// Degrees converts each value in the vector from radians to degrees and
// returns it.
func (a Vec2) Degrees() Vec2 {
	return Vec2{
		Degrees(a.X),
		Degrees(a.Y),
	}
}

// Rounded rounds each value in the vector to the nearest whole number and
// returns it.
func (a Vec2) Rounded() Vec2 {
	return Vec2{
		Rounded(a.X),
		Rounded(a.Y),
	}
}

// Dot returns the dot product of a and b.
func (a Vec2) Dot(b Vec2) float64 {
	return a.X*b.X + a.Y*b.Y
}

// Inverse returns the inverse (negated) vector -a.
func (a Vec2) Inverse() Vec2 {
	return Vec2{-a.X, -a.Y}
}

// LengthSq returns the magnitude squared of this vector, useful for comparing
// distances.
func (a Vec2) LengthSq() float64 {
	return a.X*a.X + a.Y*a.Y
}

// Length returns the magnitude of this vector. To avoid a sqrt call when
// strictly comparing distances, LengthSq can be used instead.
func (a Vec2) Length() float64 {
	return math.Sqrt(a.X*a.X + a.Y*a.Y)
}

// Normalized returns the normalized (i.e. length/magnitude == 1) vector of a.
// If the vector's length is zero (and division by zero would occur) then
// [Vec2Zero, false] is returned.
func (a Vec2) Normalized() (v Vec2, ok bool) {
	length := math.Sqrt(a.X*a.X + a.Y*a.Y)
	if Equal(length, 0) {
		return Vec2Zero, false
	}
	return Vec2{
		a.X / length,
		a.Y / length,
	}, true
}

// Proj returns a vector representing the projection of vector a onto b.
func (a Vec2) Proj(b Vec2) Vec2 {
	return b.MulScalar(a.Dot(b) / b.LengthSq())
}

// Min returns a vector representing the smallest components of both the
// vectors.
func (a Vec2) Min(b Vec2) Vec2 {
	var r Vec2
	if a.X < b.X {
		r.X = a.X
	} else {
		r.X = b.X
	}
	if a.Y < b.Y {
		r.Y = a.Y
	} else {
		r.Y = b.Y
	}
	return r
}

// Max returns a vector representing the largest components of both the
// vectors.
func (a Vec2) Max(b Vec2) Vec2 {
	var r Vec2
	if a.X > b.X {
		r.X = a.X
	} else {
		r.X = b.X
	}
	if a.Y > b.Y {
		r.Y = a.Y
	} else {
		r.Y = b.Y
	}
	return r
}

// Lerp returns a vector representing the linear interpolation between the
// vectors a and b. The t parameter is the amount to interpolate (0.0 - 1.0)
// between the vectors.
func (a Vec2) Lerp(b Vec2, t float64) Vec2 {
	return a.Mul(b.MulScalar(t))
}

// Angle returns the angle in radians between the two vectors.
func (a Vec2) Angle(b Vec2) float64 {
	return math.Atan2(b.Y-a.Y, b.X-a.X)
}

// TransformVec2 transforms a 2-component point vector by the matrix (without
// translation component) and returns the result.
// This function assumes that the matrix is an affine transformation.
func (a Vec2) TransformVec2(b Mat3) Vec2 {
	return Vec2{
		a.X*b[0][0] + a.Y*b[1][0],
		a.X*b[0][1] + a.Y*b[1][1],
	}
}

// TransformPointVec2 transforms a 2-component point vector by the matrix (with
// translation component) and returns the result.
// This function assumes that the matrix is an affine transformation.
func (a Vec2) TransformPointVec2(b Mat3) Vec2 {
	return Vec2{
		a.X*b[0][0] + a.Y*b[1][0] + b[2][0],
		a.X*b[0][1] + a.Y*b[1][1] + b[2][1],
	}
}

var (
	Vec2One   = Vec2{1, 1}
	Vec2XUnit = Vec2{1, 0}
	Vec2YUnit = Vec2{0, 1}
	Vec2Zero  = Vec2{0, 0}
)