From ebd3bfc092418e263c4ebf6047e0d9f46845e37e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Sat, 9 Jul 2011 11:07:12 +0200 Subject: [PATCH] Introduce faster versions of Rectangle#contains() that do not perform checks. --- src/basic/Rectangle.js | 29 +++++++++++++++++++---------- src/item/Item.js | 2 +- src/path/Path.js | 2 +- 3 files changed, 21 insertions(+), 12 deletions(-) diff --git a/src/basic/Rectangle.js b/src/basic/Rectangle.js index 837d7154..6d5f519a 100644 --- a/src/basic/Rectangle.js +++ b/src/basic/Rectangle.js @@ -456,16 +456,25 @@ var Rectangle = this.Rectangle = Base.extend(/** @lends Rectangle# */{ */ contains: function(rect) { // TODO: improve handling of passed Rectangle or Point - if (rect.width !== undefined) { - return rect.x >= this.x && rect.y >= this.y - && rect.x + rect.width <= this.x + this.width - && rect.y + rect.height <= this.y + this.height; - } else { - var point = Point.read(arguments); - return point.x >= this.x && point.y >= this.y - && point.x <= this.x + this.width - && point.y <= this.y + this.height; - } + return rect.width !== undefined + ? this._containsRectangle(rect) + : this._containsPoint(Point.read(arguments)); + }, + + _containsPoint: function(point) { + var x = point.x, + y = point.y; + return x >= this.x && y >= this.y + && x <= this.x + this.width + && y <= this.y + this.height; + }, + + _containsRectangle: function(rect) { + var x = rect.x, + y = rect.y; + return x >= this.x && y >= this.y + && x + rect.width <= this.x + this.width + && y + rect.height <= this.y + this.height; }, /** diff --git a/src/item/Item.js b/src/item/Item.js index 4b00a0b5..003c0802 100644 --- a/src/item/Item.js +++ b/src/item/Item.js @@ -675,7 +675,7 @@ var Item = this.Item = Base.extend(/** @lends Item# */{ // this item does not have children, since we'd have to travel up the // chain already to determine the rough bounds. if (!this._children && !this.getRoughBounds(matrix) - .expand(options.tolerance).contains(point)) + .expand(options.tolerance)._containsPoint(point)) return null; if (options.center || options.bounds) { // Don't get the transformed bounds, check against transformed diff --git a/src/path/Path.js b/src/path/Path.js index 8349231a..378076ba 100644 --- a/src/path/Path.js +++ b/src/path/Path.js @@ -1184,7 +1184,7 @@ var Path = this.Path = PathItem.extend(/** @lends Path# */{ contains: function(point, matrix) { point = Point.read(arguments); - if (!this._closed || !this.getRoughBounds(matrix).contains(point)) + if (!this._closed || !this.getRoughBounds(matrix)._containsPoint(point)) return false; // Use the crossing number algorithm, by counting the crossings of the // beam in right y-direction with the shape, and see if it's an odd