"equals" methods for Vector2D, Rectangle
This commit is contained in:
parent
8efe6ee17c
commit
0372ad6c4d
2 changed files with 21 additions and 1 deletions
|
@ -18,6 +18,11 @@ export class Vector2D {
|
||||||
public scale(other: number): Vector2D {
|
public scale(other: number): Vector2D {
|
||||||
return new Vector2D(this.x * other, this.y * other);
|
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 {
|
export class Rectangle {
|
||||||
|
@ -71,6 +76,11 @@ export class Rectangle {
|
||||||
return this.width * this.height;
|
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 {
|
public contains(point: Vector2D): boolean {
|
||||||
return point.x >= this.origin.x &&
|
return point.x >= this.origin.x &&
|
||||||
point.y >= this.origin.y &&
|
point.y >= this.origin.y &&
|
||||||
|
|
|
@ -32,6 +32,11 @@ describe("Vector2D Tests", () => {
|
||||||
expect(v2.x).to.equal(3);
|
expect(v2.x).to.equal(3);
|
||||||
expect(v2.y).to.equal(6);
|
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", () => {
|
describe("Rectangle Tests", () => {
|
||||||
|
@ -71,11 +76,16 @@ describe("Rectangle Tests", () => {
|
||||||
check_rectangle(r2, 1, 2, 3, 4);
|
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", () => {
|
it("should create the same rectangle transposed", () => {
|
||||||
const v1t = new Vector2D(v1.x, v2.y);
|
const v1t = new Vector2D(v1.x, v2.y);
|
||||||
const v2t = new Vector2D(v2.x, v1.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", () => {
|
it("should contain itself", () => {
|
||||||
|
|
Loading…
Reference in a new issue