projection method

Vector2 projection(
  1. Vector2 other, {
  2. Vector2? out,
})

Project this onto other.

other needs to have a length > 0; If out is specified, it will be used to provide the result.

Implementation

Vector2 projection(Vector2 other, {Vector2? out}) {
  assert(other.length2 > 0, 'other needs to have a length > 0');
  final dotProduct = dot(other);
  final result = (out?..setFrom(other)) ?? other.clone();
  return result..scale(dotProduct / other.length2);
}