add Vec.Project

This commit is contained in:
faiface 2017-10-15 19:50:41 +02:00
parent c87741300d
commit 721a10ef10
1 changed files with 8 additions and 0 deletions

View File

@ -144,6 +144,14 @@ func (u Vec) Cross(v Vec) float64 {
return u.X*v.Y - v.X*u.Y 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 // Map applies the function f to both x and y components of the vector u and returns the modified
// vector. // vector.
// //