scaleTo for empty vector

This commit is contained in:
Luan Nico
2019-04-28 17:56:00 -03:00
parent 0303218ebf
commit 4154e66f2d
2 changed files with 12 additions and 1 deletions

View File

@ -92,8 +92,14 @@ class Position {
}
/// Changes the [length] of this vector to the one provided, without chaning direction.
///
/// If you try to scale the zero (empty) vector, it will remain unchanged, and no error will be thrown.
Position scaleTo(double newLength) {
return times(newLength.abs() / length());
final l = length();
if (l == 0) {
return this;
}
return times(newLength.abs() / l);
}
/// Limits the [length] of this vector to the one provided, without changing direction.