From 5ddbc5b07f9baf7a575eb47a3a60788faff12c7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Mon, 20 Oct 2014 17:00:18 +0200 Subject: [PATCH] Implement Item#intersects(item) --- src/item/Item.js | 18 ++++++++++++++++++ src/path/PathItem.js | 16 ++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/src/item/Item.js b/src/item/Item.js index b3d8e16e..8f6cece2 100644 --- a/src/item/Item.js +++ b/src/item/Item.js @@ -1626,6 +1626,7 @@ var Item = Base.extend(Emitter, /** @lends Item# */{ }, // DOCS: + // TEST: /** * @param {Rectangle} rect the rectangle to check against * @returns {Boolean} @@ -1634,6 +1635,23 @@ var Item = Base.extend(Emitter, /** @lends Item# */{ return Rectangle.read(arguments).contains(this.getBounds()); }, + // DOCS: + // TEST: + /** + * @param {Item} item the item to check against + * @returns {Boolean} + */ + intersects: function(item, _matrix) { + if (!(item instanceof Item)) + return false; + // Create a temporary rectangular path item with this item's bounds, and + // delegate the call to it. + return new Path.Rectangle({ + rectangle: this.getInternalBounds(), + matrix: this._matrix, + insert: false, + }).intersects(item, _matrix); + }, /** * Perform a hit test on the item (and its children, if it is a * {@link Group} or {@link Layer}) at the location of the specified point. diff --git a/src/path/PathItem.js b/src/path/PathItem.js index 49365208..6948b641 100644 --- a/src/path/PathItem.js +++ b/src/path/PathItem.js @@ -181,6 +181,22 @@ var PathItem = Item.extend(/** @lends PathItem# */{ return locations; }, + intersects: function(item, _matrix) { + if (!(item instanceof Item)) + return false; + var other = item instanceof PathItem + ? item + // Create a temporary rectangular path item to check against. + : new Path.Rectangle({ + rectangle: item.getInternalBounds(), + insert: false + }); + // TODO: Optimize getIntersections(): We don't need all intersections + // when we're just curious about whether they intersect or not. Pass on + // an argument that let's it bail out after the first intersection. + return this.getIntersections(other, _matrix || item._matrix).length > 0; + }, + /** * The path's geometry, formatted as SVG style path data. *