Introduce faster versions of Rectangle#contains() that do not perform checks.

This commit is contained in:
Jürg Lehni 2011-07-09 11:07:12 +02:00
parent 833d4968ce
commit ebd3bfc092
3 changed files with 21 additions and 12 deletions

View file

@ -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;
},
/**

View file

@ -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

View file

@ -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