Implement Shape#contains() and define unit tests for it.

This commit is contained in:
Jürg Lehni 2013-04-20 20:54:06 -07:00
parent 2793709935
commit 4b24690207
4 changed files with 21 additions and 6 deletions

View file

@ -68,7 +68,13 @@ var Shape = this.Shape = Item.extend(/** @lends Shape# */{
contains: function(point) {
point = Point.read(arguments);
// TODO: Implement.
switch (this._type) {
case 'rect':
return this.base(point);
case 'circle':
case 'ellipse':
return point.divide(this._size).getLength() <= 0.5;
}
},
_getBounds: function(getter, matrix) {

View file

@ -123,7 +123,6 @@ test('Gray Color', function() {
compareNumbers(color.blue, 1, 'color.blue');
color.red = 0.5;
console.log(color + '');
compareNumbers(color.gray, 0.85045, 'color.gray');
color.green = 0.2;

View file

@ -10,10 +10,10 @@
* All rights reserved.
*/
module('Path Contains');
module('Item Contains');
function testPoint(path, point, inside) {
equals(path.contains(point), inside, 'The point ' + point
function testPoint(item, point, inside) {
equals(item.contains(point), inside, 'The point ' + point
+ ' should be ' + (inside ? 'inside' : 'outside') + '.');
}
@ -70,3 +70,13 @@ test('CompoundPath#contains (Donut)', function() {
equals(path.contains(new Point(-45, 45)), false,
'The near bottom left point of bounding box should be outside the donut.');
});
test('Shape#contains', function() {
var shape = new Shape.Circle([0, 0], 100);
testPoint(shape, new Point(0, 0), true);
testPoint(shape, new Point(0, -100), true);
testPoint(shape, new Point({ length: 99, angle: 45 }), true);
testPoint(shape, new Point({ length: 100, angle: 45 }), true);
testPoint(shape, new Point({ length: 101, angle: 45 }), false);
});

View file

@ -23,6 +23,7 @@
/*#*/ include('Item_Cloning.js');
/*#*/ include('Item_Order.js');
/*#*/ include('Item_Bounds.js');
/*#*/ include('Item_Contains.js');
/*#*/ include('Layer.js');
/*#*/ include('Group.js');
@ -35,7 +36,6 @@
/*#*/ include('Path_Curves.js');
/*#*/ include('Path_Bounds.js');
/*#*/ include('Path_Length.js');
/*#*/ include('Path_Contains.js');
/*#*/ include('CompoundPath.js');
/*#*/ include('PlacedSymbol.js');