avocado/packages/math/test/rectangle.js

68 lines
2.5 KiB
JavaScript
Raw Normal View History

2021-01-02 22:01:57 -06:00
import {expect} from 'chai';
2022-03-11 05:08:59 -06:00
import * as Rectangle from '../src/rectangle';
2021-01-02 22:01:57 -06:00
2022-03-11 05:08:59 -06:00
describe('Rectangle', () => {
it('can calculate intersections', () => {
2021-01-05 11:25:37 -06:00
expect(Rectangle.intersects([0, 0, 16, 16], [8, 8, 24, 24])).to.be.true;
expect(Rectangle.intersects([0, 0, 16, 16], [16, 16, 32, 32])).to.be.false;
expect(Rectangle.isTouching([0, 0, 16, 16], [0, 0])).to.be.true;
expect(Rectangle.isTouching([0, 0, 16, 16], [16, 16])).to.be.false;
expect(Rectangle.intersection([0, 0, 16, 16], [8, 8, 24, 24])).to.deep.equal([8, 8, 8, 8]);
expect(Rectangle.united([0, 0, 4, 4], [4, 4, 8, 8])).to.deep.equal([0, 0, 12, 12]);
});
2022-03-11 05:08:59 -06:00
it('can compose and decompose', () => {
const rectangle = Rectangle.compose([0, 0], [16, 16]);
2021-01-05 11:25:37 -06:00
expect(Rectangle.equals(rectangle, [0, 0, 16, 16])).to.be.true;
expect(Rectangle.position(rectangle)).to.deep.equal([0, 0]);
expect(Rectangle.size(rectangle)).to.deep.equal([16, 16]);
});
2022-03-11 05:08:59 -06:00
it('can make a deep copy', () => {
const rectangle = [0, 0, 16, 16];
const rectangle2 = Rectangle.copy(rectangle);
2021-01-05 11:25:37 -06:00
expect(Rectangle.equals(rectangle, rectangle2)).to.be.true;
rectangle[0] = 6;
expect(Rectangle.equals(rectangle, rectangle2)).to.be.false;
});
2022-03-11 05:08:59 -06:00
it('can convert to an object', () => {
const rectangle = [0, 0, 16, 16];
2021-01-05 11:25:37 -06:00
expect(Rectangle.toObject(rectangle)).to.deep.equal({
x: 0,
y: 0,
width: 16,
2022-03-11 05:08:59 -06:00
height: 16,
2021-01-02 22:01:57 -06:00
});
2021-01-05 11:25:37 -06:00
expect(Rectangle.toObject(rectangle, true)).to.deep.equal({
x: 0,
y: 0,
w: 16,
2022-03-11 05:08:59 -06:00
h: 16,
2021-01-02 22:01:57 -06:00
});
});
2022-03-11 05:08:59 -06:00
it('can translate by vector', () => {
2021-01-05 11:25:37 -06:00
expect(Rectangle.translated([0, 0, 16, 16], [8, 8])).to.deep.equal([8, 8, 16, 16]);
});
2022-03-11 05:08:59 -06:00
it('can check for null', () => {
2021-01-05 11:25:37 -06:00
expect(Rectangle.isNull(null)).to.be.true;
expect(Rectangle.isNull(3)).to.be.true;
expect(Rectangle.isNull([1])).to.be.true;
expect(Rectangle.isNull([1, 1])).to.be.true;
expect(Rectangle.isNull([1, 1, 1])).to.be.true;
expect(Rectangle.isNull([1, 1, 1, 1, 1])).to.be.true;
expect(Rectangle.isNull([0, 0, 1, 1])).to.be.false;
expect(Rectangle.isNull([0, 0, 1, 0])).to.be.true;
});
2022-03-11 05:08:59 -06:00
it('can round', () => {
2021-01-05 11:25:37 -06:00
expect(Rectangle.round([3.14, 4.70, 5.32, 1.8])).to.deep.equal([3, 5, 5, 2]);
});
2022-03-11 05:08:59 -06:00
it('can floor', () => {
expect(Rectangle.floor([3.14, 4.70, 5.32, 1.8])).to.deep.equal([3, 4, 5, 1]);
2021-01-05 11:25:37 -06:00
});
2021-02-09 10:16:03 -06:00
it('can test inside', () => {
2021-03-19 09:05:51 -05:00
expect(Rectangle.isInside(
2021-02-09 10:16:03 -06:00
[-22.55753963192675, -36.22420629859356, 64, 64],
[-6.557539631926751, -20.224206298593554, 32, 32],
2021-03-19 09:05:51 -05:00
)).to.be.true;
2021-02-09 10:16:03 -06:00
});
2021-01-02 22:01:57 -06:00
});