mirror of
https://github.com/flame-engine/flame.git
synced 2025-11-02 20:13:50 +08:00
scaleTo for empty vector
This commit is contained in:
@ -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.
|
||||||
|
|||||||
@ -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);
|
||||||
|
|||||||
Reference in New Issue
Block a user