88 lines
2.1 KiB
JavaScript
88 lines
2.1 KiB
JavaScript
|
import {expect, test} from 'vitest';
|
||
|
|
||
|
import * as MathUtil from './math.js';
|
||
|
|
||
|
test('transforms vertices', async () => {
|
||
|
const expectCloseTo = (l, r) => {
|
||
|
expect(l.length)
|
||
|
.to.equal(r.length);
|
||
|
for (let i = 0; i < l.length; ++i) {
|
||
|
expect(l[i].x)
|
||
|
.to.be.closeTo(r[i].x, 0.0001);
|
||
|
expect(l[i].y)
|
||
|
.to.be.closeTo(r[i].y, 0.0001);
|
||
|
}
|
||
|
}
|
||
|
const vertices = [
|
||
|
{x: -1, y: -1},
|
||
|
{x: 1, y: -1},
|
||
|
{x: 1, y: 1},
|
||
|
{x: -1, y: 1},
|
||
|
];
|
||
|
expect(MathUtil.transform(vertices, {}))
|
||
|
.to.deep.equal(vertices);
|
||
|
expectCloseTo(
|
||
|
MathUtil.transform(vertices, {scale: 2}),
|
||
|
[
|
||
|
{x: -2, y: -2},
|
||
|
{x: 2, y: -2},
|
||
|
{x: 2, y: 2},
|
||
|
{x: -2, y: 2},
|
||
|
],
|
||
|
);
|
||
|
expectCloseTo(
|
||
|
MathUtil.transform(vertices, {rotation: MathUtil.QUARTER_PI}),
|
||
|
[
|
||
|
{x: 0, y: -Math.sqrt(2)},
|
||
|
{x: Math.sqrt(2), y: 0},
|
||
|
{x: 0, y: Math.sqrt(2)},
|
||
|
{x: -Math.sqrt(2), y: 0},
|
||
|
],
|
||
|
);
|
||
|
expectCloseTo(
|
||
|
MathUtil.transform(vertices, {rotation: MathUtil.QUARTER_PI, scale: 2}),
|
||
|
[
|
||
|
{x: 0, y: -Math.sqrt(2) * 2},
|
||
|
{x: Math.sqrt(2) * 2, y: 0},
|
||
|
{x: 0, y: Math.sqrt(2) * 2},
|
||
|
{x: -Math.sqrt(2) * 2, y: 0},
|
||
|
],
|
||
|
);
|
||
|
expectCloseTo(
|
||
|
MathUtil.transform(vertices, {translation: {x: 10, y: 10}}),
|
||
|
[
|
||
|
{x: 9, y: 9},
|
||
|
{x: 11, y: 9},
|
||
|
{x: 11, y: 11},
|
||
|
{x: 9, y: 11},
|
||
|
],
|
||
|
);
|
||
|
expectCloseTo(
|
||
|
MathUtil.transform(vertices, {rotation: MathUtil.QUARTER_PI, translation: {x: 10, y: 10}}),
|
||
|
[
|
||
|
{x: 10, y: 10 - Math.sqrt(2)},
|
||
|
{x: 10 + Math.sqrt(2), y: 10},
|
||
|
{x: 10, y: 10 + Math.sqrt(2)},
|
||
|
{x: 10 - Math.sqrt(2), y: 10},
|
||
|
],
|
||
|
);
|
||
|
expectCloseTo(
|
||
|
MathUtil.transform(vertices, {scale: 2, translation: {x: 10, y: 10}}),
|
||
|
[
|
||
|
{x: 8, y: 8},
|
||
|
{x: 12, y: 8},
|
||
|
{x: 12, y: 12},
|
||
|
{x: 8, y: 12},
|
||
|
],
|
||
|
);
|
||
|
expectCloseTo(
|
||
|
MathUtil.transform(vertices, {rotation: MathUtil.QUARTER_PI, scale: 2, translation: {x: 10, y: 10}}),
|
||
|
[
|
||
|
{x: 10, y: 10 - Math.sqrt(2) * 2},
|
||
|
{x: 10 + Math.sqrt(2) * 2, y: 10},
|
||
|
{x: 10, y: 10 + Math.sqrt(2) * 2},
|
||
|
{x: 10 - Math.sqrt(2) * 2, y: 10},
|
||
|
],
|
||
|
);
|
||
|
});
|