Merge pull request #186 from jackd/master
fixed angleTo implementation, added tests
diff --git a/lib/src/vector_math/vector2.dart b/lib/src/vector_math/vector2.dart
index 25bef11..ec7f889 100644
--- a/lib/src/vector_math/vector2.dart
+++ b/lib/src/vector_math/vector2.dart
@@ -200,7 +200,7 @@
return 0.0;
}
- final double d = dot(other);
+ final double d = dot(other) / (length * other.length);
return math.acos(d.clamp(-1.0, 1.0));
}
diff --git a/lib/src/vector_math/vector3.dart b/lib/src/vector_math/vector3.dart
index fe315d4..432ef4d 100644
--- a/lib/src/vector_math/vector3.dart
+++ b/lib/src/vector_math/vector3.dart
@@ -216,7 +216,7 @@
return 0.0;
}
- final double d = dot(other);
+ final double d = dot(other) / (length * other.length);
return math.acos(d.clamp(-1.0, 1.0));
}
diff --git a/lib/src/vector_math_64/vector2.dart b/lib/src/vector_math_64/vector2.dart
index cc951f5..847ae40 100644
--- a/lib/src/vector_math_64/vector2.dart
+++ b/lib/src/vector_math_64/vector2.dart
@@ -200,7 +200,7 @@
return 0.0;
}
- final double d = dot(other);
+ final double d = dot(other) / (length * other.length);
return math.acos(d.clamp(-1.0, 1.0));
}
diff --git a/lib/src/vector_math_64/vector3.dart b/lib/src/vector_math_64/vector3.dart
index 7d61041..73dea75 100644
--- a/lib/src/vector_math_64/vector3.dart
+++ b/lib/src/vector_math_64/vector3.dart
@@ -216,7 +216,7 @@
return 0.0;
}
- final double d = dot(other);
+ final double d = dot(other) / (length * other.length);
return math.acos(d.clamp(-1.0, 1.0));
}
diff --git a/test/vector2_test.dart b/test/vector2_test.dart
index d5890fe..bff1a31 100644
--- a/test/vector2_test.dart
+++ b/test/vector2_test.dart
@@ -257,9 +257,14 @@
void testVector2AngleTo() {
final v0 = new Vector2(1.0, 0.0);
final v1 = new Vector2(0.0, 1.0);
+ final v2 = new Vector2(1.0, 1.0);
+ final v3 = v2.normalized();
+ final tol = 1e-8;
expect(v0.angleTo(v0), equals(0.0));
expect(v0.angleTo(v1), equals(math.PI / 2.0));
+ expect(v0.angleTo(v2), closeTo(math.PI / 4.0, tol));
+ expect(v0.angleTo(v3), closeTo(math.PI / 4.0, tol));
}
void testVector2AngleToSigned() {
diff --git a/test/vector3_test.dart b/test/vector3_test.dart
index 5e60ee8..1da7a56 100644
--- a/test/vector3_test.dart
+++ b/test/vector3_test.dart
@@ -357,9 +357,14 @@
void testVector3AngleTo() {
final v0 = new Vector3(1.0, 0.0, 0.0);
final v1 = new Vector3(0.0, 1.0, 0.0);
+ final v2 = new Vector3(1.0, 1.0, 0.0);
+ final v3 = v2.normalized();
+ final tol = 1e-8;
expect(v0.angleTo(v0), equals(0.0));
expect(v0.angleTo(v1), equals(math.PI / 2.0));
+ expect(v0.angleTo(v2), closeTo(math.PI / 4.0, tol));
+ expect(v0.angleTo(v3), closeTo(math.PI / 4.0, tol));
}
void testVector3AngleToSigned() {