diff --git a/src/core/math/shapes/Rectangle.js b/src/core/math/shapes/Rectangle.js index f6bf916..193b29b 100644 --- a/src/core/math/shapes/Rectangle.js +++ b/src/core/math/shapes/Rectangle.js @@ -179,44 +179,15 @@ */ fit(rectangle) { - if (this.x < rectangle.x) - { - this.width += this.x; - if (this.width < 0) - { - this.width = 0; - } + const x1 = Math.max(this.x, rectangle.x); + const x2 = Math.min(this.x + this.width, rectangle.x + rectangle.width); + const y1 = Math.max(this.y, rectangle.y); + const y2 = Math.min(this.y + this.height, rectangle.y + rectangle.height); - this.x = rectangle.x; - } - - if (this.y < rectangle.y) - { - this.height += this.y; - if (this.height < 0) - { - this.height = 0; - } - this.y = rectangle.y; - } - - if (this.x + this.width > rectangle.x + rectangle.width) - { - this.width = rectangle.width - this.x; - if (this.width < 0) - { - this.width = 0; - } - } - - if (this.y + this.height > rectangle.y + rectangle.height) - { - this.height = rectangle.height - this.y; - if (this.height < 0) - { - this.height = 0; - } - } + this.x = x1; + this.width = Math.max(x2 - x1, 0); + this.y = y1; + this.height = Math.max(y2 - y1, 0); } /** diff --git a/src/core/math/shapes/Rectangle.js b/src/core/math/shapes/Rectangle.js index f6bf916..193b29b 100644 --- a/src/core/math/shapes/Rectangle.js +++ b/src/core/math/shapes/Rectangle.js @@ -179,44 +179,15 @@ */ fit(rectangle) { - if (this.x < rectangle.x) - { - this.width += this.x; - if (this.width < 0) - { - this.width = 0; - } + const x1 = Math.max(this.x, rectangle.x); + const x2 = Math.min(this.x + this.width, rectangle.x + rectangle.width); + const y1 = Math.max(this.y, rectangle.y); + const y2 = Math.min(this.y + this.height, rectangle.y + rectangle.height); - this.x = rectangle.x; - } - - if (this.y < rectangle.y) - { - this.height += this.y; - if (this.height < 0) - { - this.height = 0; - } - this.y = rectangle.y; - } - - if (this.x + this.width > rectangle.x + rectangle.width) - { - this.width = rectangle.width - this.x; - if (this.width < 0) - { - this.width = 0; - } - } - - if (this.y + this.height > rectangle.y + rectangle.height) - { - this.height = rectangle.height - this.y; - if (this.height < 0) - { - this.height = 0; - } - } + this.x = x1; + this.width = Math.max(x2 - x1, 0); + this.y = y1; + this.height = Math.max(y2 - y1, 0); } /** diff --git a/test/core/Rectangle.js b/test/core/Rectangle.js index 3bd6f13..911254d 100644 --- a/test/core/Rectangle.js +++ b/test/core/Rectangle.js @@ -162,8 +162,28 @@ expect(rect3.left).to.equal(10); expect(rect3.top).to.equal(0); - expect(rect3.right).to.equal(30); + expect(rect3.right).to.equal(20); expect(rect3.bottom).to.equal(20); + + const rect5 = new PIXI.Rectangle(10, 10, 20, 25); + const rect6 = new PIXI.Rectangle(22, 24, 20, 20); + + rect5.fit(rect6); + + expect(rect5.left).to.equal(22); + expect(rect5.top).to.equal(24); + expect(rect5.right).to.equal(30); + expect(rect5.bottom).to.equal(35); + + const rect7 = new PIXI.Rectangle(11, 10, 20, 25); + const rect8 = new PIXI.Rectangle(10, 9, 13, 10); + + rect7.fit(rect8); + + expect(rect7.left).to.equal(11); + expect(rect7.top).to.equal(10); + expect(rect7.right).to.equal(23); + expect(rect7.bottom).to.equal(19); }); it('should generate an empty rectangle', function ()