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. /// 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) { 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. /// Limits the [length] of this vector to the one provided, without changing direction.

View File

@ -80,6 +80,11 @@ void main() {
expect(p.y, 0.0); expect(p.y, 0.0);
}); });
test('scaleTo the zero vector', () {
final Position p = Position.empty();
expect(p.scaleTo(1.0).length(), 0.0);
});
test('limit', () { test('limit', () {
final Position p1 = Position(1.0, 0.0); final Position p1 = Position(1.0, 0.0);
p1.limit(0.75); p1.limit(0.75);