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);
|
|
|
|
|
|
|
|
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-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);
|
|
|
|
|
|
|
|
it("should create a rectangle", () => {
|
|
|
|
expect(r1.left).to.equal(1);
|
|
|
|
expect(r1.top).to.equal(2);
|
|
|
|
expect(r1.width).to.equal(3);
|
|
|
|
expect(r1.height).to.equal(4);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should create the same rectangle backwards", () => {
|
|
|
|
expect(r2.left).to.equal(1);
|
|
|
|
expect(r2.top).to.equal(2);
|
|
|
|
expect(r2.width).to.equal(3);
|
|
|
|
expect(r2.height).to.equal(4);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should create the same rectangle transposed", () => {
|
|
|
|
const v1t = new Vector2D(v1.x, v2.y);
|
|
|
|
const v2t = new Vector2D(v2.x, v1.y);
|
|
|
|
const rt = new Rectangle(v1t, v2t);
|
|
|
|
|
|
|
|
expect(rt.left).to.equal(1);
|
|
|
|
expect(rt.top).to.equal(2);
|
|
|
|
expect(rt.width).to.equal(3);
|
|
|
|
expect(rt.height).to.equal(4);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should contain itself", () => {
|
|
|
|
expect(r1.contains(v1)).true;
|
|
|
|
expect(r1.contains(v2)).true;
|
|
|
|
|
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-23 14:38:49 +00:00
|
|
|
|
|
|
|
const vmid = new Vector2D((v1.x + v2.x) / 2, (v1.y + v2.y) / 2);
|
2023-01-23 22:38:57 +00:00
|
|
|
expect(r1.contains(vmid)).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
|
|
|
});
|
|
|
|
});
|