1
1
forked from 0ad/0ad

Add another Vector2D rotation test (illustrating the most common rmgen rotation transformation).

TS_ASSERT_EQUALS_APPROX must not silently pass if epsilon wasn't given.
Sort Vector2D.rotate summands by component rather than saving one
character.

This was SVN commit r21062.
This commit is contained in:
elexis 2018-01-29 09:51:20 +00:00
parent 2fa34e129d
commit 30fe33bb0b
3 changed files with 19 additions and 1 deletions

View File

@ -77,7 +77,7 @@ Vector2D.prototype.rotate = function(angle)
return this.set(
this.x * cos + this.y * sin,
this.y * cos - this.x * sin);
-this.x * sin + this.y * cos);
};
/**

View File

@ -74,6 +74,21 @@ var brokenVector = {
}
}
// Test Vector2D rotation further
{
let epsilon = 0.00000001;
for (let i = 0; i <= 128; ++i)
{
let angle = i / 128 * Math.PI;
let vec1 = new Vector2D(Math.cos(angle), Math.sin(angle));
let vec2 = new Vector2D(1, 0).rotate(-angle);
TS_ASSERT_EQUALS_APPROX(vec1.x, vec2.x, epsilon);
TS_ASSERT_EQUALS_APPROX(vec1.y, vec2.y, epsilon);
}
}
// Test Vector2D rotation around a center
{
let epsilon = 0.00000001;

View File

@ -34,6 +34,9 @@ global.TS_ASSERT_EQUALS = function TS_ASSERT_EQUALS(x, y)
global.TS_ASSERT_EQUALS_APPROX = function TS_ASSERT_EQUALS_APPROX(x, y, maxDifference)
{
if (!Number.isInteger(maxDifference))
fail("Test must pass a maximum difference!");
if (Math.abs(x - y) > maxDifference)
fail("Expected almost equal, got " + uneval(x) + " !== " + uneval(y));
}