From 721a10ef1027d1ad756552b14848d39dd3cc9cea Mon Sep 17 00:00:00 2001 From: faiface Date: Sun, 15 Oct 2017 19:50:41 +0200 Subject: [PATCH] add Vec.Project --- geometry.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/geometry.go b/geometry.go index 00419ad..0cb1436 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. //