Skip to content

Commit

Permalink
Fix vetor.normalizeLength to due what the description says. As I coul…
Browse files Browse the repository at this point in the history
…d only find the bug due to my own application, I also added tests for that case.
  • Loading branch information
Fox32 committed Apr 21, 2014
1 parent a613ddc commit 66b9578
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 18 deletions.
10 changes: 5 additions & 5 deletions lib/src/vector_math/vector2.dart
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,13 @@ class Vector2 implements Vector {

/// Normalize [this]. Returns length of vector before normalization.
double normalizeLength() {
var l = length;
final l = length;
if (l == 0.0) {
return 0.0;
}
l = 1.0 / l;
_storage2[0] *= l;
_storage2[1] *= l;
final d = 1.0 / l;
_storage2[0] *= d;
_storage2[1] *= d;
return l;
}

Expand Down Expand Up @@ -200,7 +200,7 @@ class Vector2 implements Vector {
return _storage2[0] * otherStorage[1] - _storage2[1] * otherStorage[0];
}

/// Rotate [this] by 90 degrees then scale it. Store result in [out]. Return
/// Rotate [this] by 90 degrees then scale it. Store result in [out]. Return
/// [out].
Vector2 scaleOrthogonalInto(double scale, Vector2 out) {
out.setValues(-scale * _storage2[1], scale * _storage2[0]);
Expand Down
12 changes: 6 additions & 6 deletions lib/src/vector_math/vector3.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class Vector3 implements Vector {
Float32List get storage => _storage3;

/// Construct a new vector with the specified values.
factory Vector3(double x, double y, double z) =>
factory Vector3(double x, double y, double z) =>
new Vector3.zero()..setValues(x, y, z);

/// Initialized with values from [array] starting at [offset].
Expand Down Expand Up @@ -158,14 +158,14 @@ class Vector3 implements Vector {

/// Normalize [this]. Returns length of vector before normalization.
double normalizeLength() {
var l = length;
final l = length;
if (l == 0.0) {
return 0.0;
}
l = 1.0 / l;
_storage3[0] *= l;
_storage3[1] *= l;
_storage3[2] *= l;
final d = 1.0 / l;
_storage3[0] *= d;
_storage3[1] *= d;
_storage3[2] *= d;
return l;
}

Expand Down
13 changes: 6 additions & 7 deletions lib/src/vector_math/vector4.dart
Original file line number Diff line number Diff line change
Expand Up @@ -179,15 +179,15 @@ class Vector4 implements Vector {

/// Normalizes [this]. Returns length of vector before normalization.
double normalizeLength() {
var l = length;
final l = length;
if (l == 0.0) {
return 0.0;
}
l = 1.0 / l;
_storage4[0] *= l;
_storage4[1] *= l;
_storage4[2] *= l;
_storage4[3] *= l;
final d = 1.0 / l;
_storage4[0] *= d;
_storage4[1] *= d;
_storage4[2] *= d;
_storage4[3] *= d;
return l;
}

Expand All @@ -196,7 +196,6 @@ class Vector4 implements Vector {

/// Normalize vector into [out].
Vector4 normalizeInto(Vector4 out) {
//TODO (fox32): Remove this method?
out.setFrom(this);
return out..normalize();
}
Expand Down
40 changes: 40 additions & 0 deletions test/test_vector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,42 @@ class VectorTest extends BaseTest {
expect(b.w, equals(8.0));
}

void testVec2Length() {
final Vector2 a = new Vector2(5.0, 7.0);

relativeTest(a.length, 8.6);
relativeTest(a.length2, 74.0);

relativeTest(a.normalizeLength(), 8.6);
relativeTest(a.x, 0.5812);
relativeTest(a.y, 0.8137);
}

void testVec3Length() {
final Vector3 a = new Vector3(5.0, 7.0, 3.0);

relativeTest(a.length, 9.1104);
relativeTest(a.length2, 83.0);

relativeTest(a.normalizeLength(), 9.1104);
relativeTest(a.x, 0.5488);
relativeTest(a.y, 0.7683);
relativeTest(a.z, 0.3292);
}

void testVec4Length() {
final Vector4 a = new Vector4(5.0, 7.0, 3.0, 10.0);

relativeTest(a.length, 13.5277);
relativeTest(a.length2, 183.0);

relativeTest(a.normalizeLength(), 13.5277);
relativeTest(a.x, 0.3696);
relativeTest(a.y, 0.5174);
relativeTest(a.z, 0.2217);
relativeTest(a.w, 0.7392);
}

void testVec2MinMax() {
final Vector2 a = new Vector2(5.0, 7.0);
final Vector2 b = new Vector2(3.0, 8.0);
Expand Down Expand Up @@ -628,6 +664,10 @@ class VectorTest extends BaseTest {
test('3D add', testVec3Add);
test('4D add', testVec4Add);

test('2D length', testVec2Length);
test('3D length', testVec3Length);
test('4D length', testVec4Length);

test('2D min/max', testVec2MinMax);
test('3D min/max', testVec3MinMax);
test('4D min/max', testVec4MinMax);
Expand Down

0 comments on commit 66b9578

Please sign in to comment.