2023-01-23 14:38:49 +00:00
|
|
|
import { Rectangle, Vector2D } from '@/components/rects/rectangles';
|
2023-01-21 02:59:46 +00:00
|
|
|
import { expect } from "chai";
|
|
|
|
|
2023-01-23 15:07:40 +00:00
|
|
|
describe("Vector2D Tests", () => {
|
2023-01-21 02:59:46 +00:00
|
|
|
const v = new Vector2D(1, 2);
|
|
|
|
|
2023-01-31 22:19:30 +00:00
|
|
|
it("should create a default vector", () => {
|
|
|
|
const v0 = new Vector2D();
|
|
|
|
expect(v0.x).to.equal(0);
|
|
|
|
expect(v0.y).to.equal(0);
|
|
|
|
});
|
|
|
|
|
2023-01-21 02:59:46 +00:00
|
|
|
it("should create a vector", () => {
|
|
|
|
expect(v.x).to.equal(1);
|
|
|
|
expect(v.y).to.equal(2);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should add vectors", () => {
|
|
|
|
const v2 = v.plus(new Vector2D(3, 4));
|
|
|
|
expect(v2.x).to.equal(4);
|
|
|
|
expect(v2.y).to.equal(6);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should subtract vectors", () => {
|
|
|
|
const v2 = v.minus(new Vector2D(3, 4));
|
|
|
|
expect(v2.x).to.equal(-2);
|
|
|
|
expect(v2.y).to.equal(-2);
|
|
|
|
});
|
2023-01-24 23:11:01 +00:00
|
|
|
|
|
|
|
it("should scale vectors", () => {
|
|
|
|
const v2 = v.scale(3);
|
|
|
|
expect(v2.x).to.equal(3);
|
|
|
|
expect(v2.y).to.equal(6);
|
|
|
|
});
|
2023-01-21 02:59:46 +00:00
|
|
|
});
|
2023-01-23 14:38:49 +00:00
|
|
|
|
2023-01-23 15:07:40 +00:00
|
|
|
describe("Rectangle Tests", () => {
|
2023-01-23 14:38:49 +00:00
|
|
|
const v1 = new Vector2D(1, 2);
|
|
|
|
const v2 = new Vector2D(4, 6);
|
|
|
|
|
|
|
|
|
|
|
|
const r1 = new Rectangle(v1, v2);
|
|
|
|
const r2 = new Rectangle(v2, v1);
|
|
|
|
|
2023-01-31 22:19:30 +00:00
|
|
|
function check_rectangle(
|
|
|
|
r: Rectangle,
|
|
|
|
left: number, top: number,
|
|
|
|
width: number, height: number,
|
|
|
|
) {
|
|
|
|
expect(r.left).to.equal(left);
|
|
|
|
expect(r.top).to.equal(top);
|
|
|
|
|
|
|
|
expect(r.width).to.equal(width);
|
|
|
|
expect(r.height).to.equal(height);
|
|
|
|
expect(r.area).to.equal(width * height);
|
|
|
|
|
|
|
|
expect(r.middle.x).to.equal(left + 0.5 * width);
|
|
|
|
expect(r.middle.y).to.equal(top + 0.5 * height);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
it("should create a default rectangle", () => {
|
|
|
|
check_rectangle(new Rectangle(), 0, 0, 0, 0);
|
|
|
|
});
|
|
|
|
|
2023-01-23 14:38:49 +00:00
|
|
|
it("should create a rectangle", () => {
|
2023-01-31 22:19:30 +00:00
|
|
|
check_rectangle(r1, 1, 2, 3, 4);
|
2023-01-23 14:38:49 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should create the same rectangle backwards", () => {
|
2023-01-31 22:19:30 +00:00
|
|
|
check_rectangle(r2, 1, 2, 3, 4);
|
2023-01-23 14:38:49 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should create the same rectangle transposed", () => {
|
|
|
|
const v1t = new Vector2D(v1.x, v2.y);
|
|
|
|
const v2t = new Vector2D(v2.x, v1.y);
|
|
|
|
|
2023-01-31 22:19:30 +00:00
|
|
|
check_rectangle(new Rectangle(v1t, v2t), 1, 2, 3, 4);
|
2023-01-23 14:38:49 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should contain itself", () => {
|
2023-01-31 22:19:30 +00:00
|
|
|
expect(r1.contains(v1)).to.be.true;
|
|
|
|
expect(r1.contains(v2)).to.be.true;
|
2023-01-23 14:38:49 +00:00
|
|
|
|
2023-01-23 22:38:57 +00:00
|
|
|
expect(r1.contains(r1.origin)).to.be.true;
|
|
|
|
expect(r1.contains(r1.corner)).to.be.true;
|
2023-01-31 22:19:30 +00:00
|
|
|
expect(r1.contains(r1.middle)).to.be.true;
|
2023-01-23 14:38:49 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should not contain certain points", () => {
|
2023-01-23 22:38:57 +00:00
|
|
|
expect(r1.contains(new Vector2D(0, 0))).to.be.false;
|
|
|
|
expect(r1.contains(new Vector2D(100, 100))).to.be.false;
|
2023-01-23 14:38:49 +00:00
|
|
|
});
|
2023-01-31 22:19:30 +00:00
|
|
|
|
|
|
|
it("should update a rectangle", () => {
|
|
|
|
const v = new Vector2D(1, 1);
|
|
|
|
|
|
|
|
check_rectangle(r1.update(v1.plus(v), undefined), 2, 3, 2, 3);
|
|
|
|
check_rectangle(r1.update(v1.minus(v), undefined), 0, 1, 4, 5);
|
|
|
|
|
|
|
|
check_rectangle(r1.update(undefined, v2.plus(v)), 1, 2, 4, 5);
|
|
|
|
check_rectangle(r1.update(undefined, v2.minus(v)), 1, 2, 2, 3);
|
|
|
|
|
|
|
|
check_rectangle(r1.update(v1.plus(v), v2.plus(v)), 2, 3, 3, 4);
|
|
|
|
check_rectangle(r1.update(v1.minus(v), v2.minus(v)), 0, 1, 3, 4);
|
|
|
|
|
|
|
|
check_rectangle(r1.update(v1.minus(v), v2.plus(v)), 0, 1, 5, 6);
|
|
|
|
check_rectangle(r1.update(v1.plus(v), v2.minus(v)), 2, 3, 1, 2);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should move a rectangle", () => {
|
|
|
|
const v = new Vector2D(1, 1);
|
|
|
|
|
|
|
|
check_rectangle(r1.move(v), 2, 3, 3, 4);
|
|
|
|
});
|
2023-01-23 14:38:49 +00:00
|
|
|
});
|