diff --git a/geometry.go b/geometry.go index 00419ada5aae2a08f3d03e53c7ccc7e801c52bea..0cb143665a1f17c5571d376f9d32c52511c9e25f 100644 --- a/geometry.go +++ b/geometry.go @@ -144,6 +144,14 @@ func (u Vec) Cross(v Vec) float64 { return u.X*v.Y - v.X*u.Y } +// Project returns a projection (or component) of vector u in the direction of vector v. +// +// Behaviour is undefined if v is a zero vector. +func (u Vec) Project(v Vec) Vec { + len := u.Dot(v) / v.Len() + return v.Unit().Scaled(len) +} + // Map applies the function f to both x and y components of the vector u and returns the modified // vector. //