diff --git a/src/core/math/Vec2.ts b/src/core/math/Vec2.ts index 2140482b..e352b876 100644 --- a/src/core/math/Vec2.ts +++ b/src/core/math/Vec2.ts @@ -1,7 +1,17 @@ export class Vec2 { - constructor( - public readonly x: number = 0, - public readonly y: number = 0 + public constructor( + public x: number = 0, + public y: number = 0 ) { } + + public negate(): void { + this.x = -this.x; + this.y = -this.y; + } + + public getNegated(): Vec2 { + // avoid reusing negate for better performance + return new Vec2(-this.x, -this.y); + } } \ No newline at end of file diff --git a/test/unit/core/math/Vec2.test.ts b/test/unit/core/math/Vec2.test.ts index a8637a6c..769b314d 100644 --- a/test/unit/core/math/Vec2.test.ts +++ b/test/unit/core/math/Vec2.test.ts @@ -10,4 +10,38 @@ describe('core/math/Vec2', () => { }); }) }) + + describe('.negate()', () => { + it('Should negate the vector in place', () => { + const vec = new Vec2(1, 2); + vec.negate() + expect(vec).toEqual({ + x: -1, + y: -2 + }); + }) + }) + + describe('.getNegated()', () => { + it('Should return a new vector with the negated values', () => { + const vec = new Vec2(1, 2); + const negated = vec.getNegated(); + + expect(negated).toEqual({ + x: -1, + y: -2 + }); + }) + + it('Should not modify the original vector', () => { + const vec = new Vec2(1, 2); + const negated = vec.getNegated(); + + expect(negated).not.toBe(vec); + expect(vec).toEqual({ + x: 1, + y: 2 + }); + }) + }) }) \ No newline at end of file