From 0372ad6c4d053d7528ea0def129abf79fea732f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= Date: Thu, 2 Feb 2023 14:10:45 +0000 Subject: [PATCH] "equals" methods for Vector2D, Rectangle --- ui/src/components/rects/rectangles.ts | 10 ++++++++++ ui/tests/unit/rectangle.spec.ts | 12 +++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/ui/src/components/rects/rectangles.ts b/ui/src/components/rects/rectangles.ts index 3319604..5dfb8d7 100644 --- a/ui/src/components/rects/rectangles.ts +++ b/ui/src/components/rects/rectangles.ts @@ -18,6 +18,11 @@ export class Vector2D { public scale(other: number): Vector2D { return new Vector2D(this.x * other, this.y * other); } + + public equals(other: Vector2D): boolean { + return this.x === other.x && + this.y === other.y; + } } export class Rectangle { @@ -71,6 +76,11 @@ export class Rectangle { return this.width * this.height; } + public equals(other: Rectangle): boolean { + return this.origin.equals(other.origin) && + this.corner.equals(other.corner); + } + public contains(point: Vector2D): boolean { return point.x >= this.origin.x && point.y >= this.origin.y && diff --git a/ui/tests/unit/rectangle.spec.ts b/ui/tests/unit/rectangle.spec.ts index ddde4d9..abee785 100644 --- a/ui/tests/unit/rectangle.spec.ts +++ b/ui/tests/unit/rectangle.spec.ts @@ -32,6 +32,11 @@ describe("Vector2D Tests", () => { expect(v2.x).to.equal(3); expect(v2.y).to.equal(6); }); + + it("should compare vectors", () => { + expect(v.equals(v.scale(1))).to.be.true; + expect(v.equals(v.scale(2))).to.be.false; + }) }); describe("Rectangle Tests", () => { @@ -71,11 +76,16 @@ describe("Rectangle Tests", () => { check_rectangle(r2, 1, 2, 3, 4); }); + it("should compare rectangles", () => { + expect(r1.equals(r2)).to.be.true; + expect(r1.equals(new Rectangle())).to.be.false; + }) + it("should create the same rectangle transposed", () => { const v1t = new Vector2D(v1.x, v2.y); const v2t = new Vector2D(v2.x, v1.y); - check_rectangle(new Rectangle(v1t, v2t), 1, 2, 3, 4); + expect(r1.equals(new Rectangle(v1t, v2t))).to.be.true; }); it("should contain itself", () => {