2011-07-01 06:17:45 -04:00
|
|
|
/*
|
2013-01-28 21:03:27 -05:00
|
|
|
* Paper.js - The Swiss Army Knife of Vector Graphics Scripting.
|
2011-07-01 06:17:45 -04:00
|
|
|
* http://paperjs.org/
|
|
|
|
*
|
2013-01-28 21:03:27 -05:00
|
|
|
* Copyright (c) 2011 - 2013, Juerg Lehni & Jonathan Puckey
|
2011-07-01 06:17:45 -04:00
|
|
|
* http://lehni.org/ & http://jonathanpuckey.com/
|
|
|
|
*
|
|
|
|
* Distributed under the MIT license. See LICENSE file for details.
|
|
|
|
*
|
|
|
|
* All rights reserved.
|
|
|
|
*/
|
|
|
|
|
2011-02-07 13:28:09 -05:00
|
|
|
module('Rectangle');
|
2011-02-11 09:02:35 -05:00
|
|
|
test('new Rectangle(new Point(10, 20), new Size(30, 40));', function() {
|
2011-02-07 13:28:09 -05:00
|
|
|
var rect = new Rectangle(new Point(10, 20), new Size(30, 40));
|
2011-05-21 15:11:53 -04:00
|
|
|
equals(rect.toString(), '{ x: 10, y: 20, width: 30, height: 40 }');
|
2011-02-07 13:28:09 -05:00
|
|
|
});
|
|
|
|
|
2011-02-11 09:02:35 -05:00
|
|
|
test('new Rectangle([10, 20], [30, 40]);', function() {
|
2011-02-07 13:28:09 -05:00
|
|
|
var rect = new Rectangle([10, 20], [30, 40]);
|
2011-05-21 15:11:53 -04:00
|
|
|
equals(rect.toString(), '{ x: 10, y: 20, width: 30, height: 40 }');
|
2011-02-07 13:28:09 -05:00
|
|
|
});
|
|
|
|
|
2011-02-11 09:02:35 -05:00
|
|
|
test('new Rectangle(new Point(10, 20), new Point(30, 40));', function() {
|
2011-02-07 13:28:09 -05:00
|
|
|
var rect = new Rectangle(new Point(10, 20), new Point(30, 40));
|
2011-05-21 15:11:53 -04:00
|
|
|
equals(rect.toString(), '{ x: 10, y: 20, width: 20, height: 20 }');
|
2011-02-07 13:28:09 -05:00
|
|
|
});
|
|
|
|
|
2011-02-11 09:02:35 -05:00
|
|
|
test('new Rectangle(10, 20, 30, 40);', function() {
|
2011-02-07 13:28:09 -05:00
|
|
|
var rect = new Rectangle(10, 20, 30, 40);
|
2011-05-21 15:11:53 -04:00
|
|
|
equals(rect.toString(), '{ x: 10, y: 20, width: 30, height: 40 }');
|
2011-02-07 13:28:09 -05:00
|
|
|
});
|
|
|
|
|
2011-02-11 09:02:35 -05:00
|
|
|
test('new Rectangle({x: 10, y: 20, width: 30, height: 40});', function() {
|
2011-02-07 13:28:09 -05:00
|
|
|
var rect = new Rectangle({x: 10, y: 20, width: 30, height: 40});
|
2011-05-21 15:11:53 -04:00
|
|
|
equals(rect.toString(), '{ x: 10, y: 20, width: 30, height: 40 }');
|
2011-02-07 13:28:09 -05:00
|
|
|
});
|
|
|
|
|
2011-02-11 09:02:35 -05:00
|
|
|
test('get size', function() {
|
2011-02-07 13:28:09 -05:00
|
|
|
var rect = new Rectangle(10, 10, 20, 30);
|
2011-05-07 12:46:06 -04:00
|
|
|
equals(function() {
|
|
|
|
return rect.size.equals([20, 30]);
|
|
|
|
}, true);
|
2011-02-07 13:28:09 -05:00
|
|
|
});
|
|
|
|
|
2011-02-11 09:02:35 -05:00
|
|
|
test('set size', function() {
|
2011-02-07 13:28:09 -05:00
|
|
|
var rect = new Rectangle(10, 10, 20, 20);
|
2011-02-11 09:02:35 -05:00
|
|
|
rect.size = new Size(30, 30);
|
2011-05-21 15:11:53 -04:00
|
|
|
equals(rect.toString(), '{ x: 10, y: 10, width: 30, height: 30 }');
|
2011-02-07 13:28:09 -05:00
|
|
|
});
|
|
|
|
|
2011-02-11 09:02:35 -05:00
|
|
|
test('topLeft', function() {
|
2011-02-07 13:28:09 -05:00
|
|
|
var rect = new Rectangle(10, 10, 20, 20);
|
2011-02-11 09:02:35 -05:00
|
|
|
var point = rect.topLeft;
|
2011-05-21 15:11:53 -04:00
|
|
|
equals(point.toString(), '{ x: 10, y: 10 }');
|
2011-02-07 13:28:09 -05:00
|
|
|
});
|
|
|
|
|
2011-02-11 09:02:35 -05:00
|
|
|
test('set topLeft', function() {
|
2011-02-07 13:28:09 -05:00
|
|
|
var rect = new Rectangle(10, 10, 20, 20);
|
2011-02-11 09:02:35 -05:00
|
|
|
rect.topLeft = [10, 15];
|
|
|
|
var point = rect.topLeft;
|
2011-05-21 15:11:53 -04:00
|
|
|
equals(point.toString(), '{ x: 10, y: 15 }');
|
2011-02-07 13:28:09 -05:00
|
|
|
});
|
|
|
|
|
2011-02-11 09:02:35 -05:00
|
|
|
test('get topRight', function() {
|
2011-02-07 13:28:09 -05:00
|
|
|
var rect = new Rectangle(10, 10, 20, 20);
|
2011-02-11 09:02:35 -05:00
|
|
|
var point = rect.topRight;
|
2011-05-21 15:11:53 -04:00
|
|
|
equals(point.toString(), '{ x: 30, y: 10 }');
|
2011-02-07 13:28:09 -05:00
|
|
|
});
|
|
|
|
|
2011-02-11 09:02:35 -05:00
|
|
|
test('set topRight', function() {
|
2011-02-07 13:28:09 -05:00
|
|
|
var rect = new Rectangle(10, 10, 20, 20);
|
2011-02-11 09:02:35 -05:00
|
|
|
rect.topRight = [10, 15];
|
|
|
|
var point = rect.topRight;
|
2011-05-21 15:11:53 -04:00
|
|
|
equals(point.toString(), '{ x: 10, y: 15 }');
|
2011-02-07 13:28:09 -05:00
|
|
|
});
|
|
|
|
|
2011-02-11 09:02:35 -05:00
|
|
|
test('get bottomLeft', function() {
|
2011-02-07 13:28:09 -05:00
|
|
|
var rect = new Rectangle(10, 10, 20, 20);
|
2011-02-11 09:02:35 -05:00
|
|
|
var point = rect.bottomLeft;
|
2011-05-21 15:11:53 -04:00
|
|
|
equals(point.toString(), '{ x: 10, y: 30 }');
|
2011-02-07 13:28:09 -05:00
|
|
|
});
|
|
|
|
|
2011-02-11 09:02:35 -05:00
|
|
|
test('set bottomLeft', function() {
|
2011-02-07 13:28:09 -05:00
|
|
|
var rect = new Rectangle(10, 10, 20, 20);
|
2011-02-11 09:02:35 -05:00
|
|
|
rect.bottomLeft = [10, 15];
|
|
|
|
var point = rect.bottomLeft;
|
2011-05-21 15:11:53 -04:00
|
|
|
equals(point.toString(), '{ x: 10, y: 15 }');
|
2011-02-07 13:28:09 -05:00
|
|
|
});
|
|
|
|
|
2011-02-11 09:02:35 -05:00
|
|
|
test('get bottomRight', function() {
|
2011-02-07 13:28:09 -05:00
|
|
|
var rect = new Rectangle(10, 10, 20, 20);
|
2011-02-11 09:02:35 -05:00
|
|
|
var point = rect.bottomRight;
|
2011-05-21 15:11:53 -04:00
|
|
|
equals(point.toString(), '{ x: 30, y: 30 }');
|
2011-02-07 13:28:09 -05:00
|
|
|
});
|
|
|
|
|
2011-02-11 09:02:35 -05:00
|
|
|
test('set bottomRight', function() {
|
2011-02-07 13:28:09 -05:00
|
|
|
var rect = new Rectangle(10, 10, 20, 20);
|
2011-02-11 09:02:35 -05:00
|
|
|
rect.bottomRight = [10, 15];
|
|
|
|
var point = rect.bottomRight;
|
2011-05-21 15:11:53 -04:00
|
|
|
equals(point.toString(), '{ x: 10, y: 15 }');
|
2011-02-07 13:28:09 -05:00
|
|
|
});
|
|
|
|
|
2011-02-11 09:02:35 -05:00
|
|
|
test('get bottomCenter', function() {
|
2011-02-07 13:28:09 -05:00
|
|
|
var rect = new Rectangle(10, 10, 20, 20);
|
2011-02-11 09:02:35 -05:00
|
|
|
var point = rect.bottomCenter;
|
2011-05-21 15:11:53 -04:00
|
|
|
equals(point.toString(), '{ x: 20, y: 30 }');
|
2011-02-07 13:28:09 -05:00
|
|
|
});
|
|
|
|
|
2011-02-11 09:02:35 -05:00
|
|
|
test('set bottomCenter', function() {
|
2011-02-07 13:28:09 -05:00
|
|
|
var rect = new Rectangle(10, 10, 20, 20);
|
2011-02-11 09:02:35 -05:00
|
|
|
rect.bottomCenter = [10, 15];
|
|
|
|
var point = rect.bottomCenter;
|
2011-05-21 15:11:53 -04:00
|
|
|
equals(point.toString(), '{ x: 10, y: 15 }');
|
2011-02-07 13:28:09 -05:00
|
|
|
});
|
|
|
|
|
2011-02-11 09:02:35 -05:00
|
|
|
test('get topCenter', function() {
|
2011-02-07 13:28:09 -05:00
|
|
|
var rect = new Rectangle(10, 10, 20, 20);
|
2011-02-11 09:02:35 -05:00
|
|
|
var point = rect.topCenter;
|
2011-05-21 15:11:53 -04:00
|
|
|
equals(point.toString(), '{ x: 20, y: 10 }');
|
2011-02-07 13:28:09 -05:00
|
|
|
});
|
|
|
|
|
2011-02-11 09:02:35 -05:00
|
|
|
test('set topCenter', function() {
|
2011-02-07 13:28:09 -05:00
|
|
|
var rect = new Rectangle(10, 10, 20, 20);
|
2011-02-11 09:02:35 -05:00
|
|
|
rect.topCenter = [10, 15];
|
|
|
|
var point = rect.topCenter;
|
2011-05-21 15:11:53 -04:00
|
|
|
equals(point.toString(), '{ x: 10, y: 15 }');
|
2011-02-07 13:28:09 -05:00
|
|
|
});
|
|
|
|
|
2011-02-11 09:02:35 -05:00
|
|
|
test('get leftCenter', function() {
|
2011-02-07 13:28:09 -05:00
|
|
|
var rect = new Rectangle(10, 10, 20, 20);
|
2011-02-11 09:02:35 -05:00
|
|
|
var point = rect.leftCenter;
|
2011-05-21 15:11:53 -04:00
|
|
|
equals(point.toString(), '{ x: 10, y: 20 }');
|
2011-02-07 13:28:09 -05:00
|
|
|
});
|
|
|
|
|
2011-02-11 09:02:35 -05:00
|
|
|
test('set leftCenter', function() {
|
2011-02-07 13:28:09 -05:00
|
|
|
var rect = new Rectangle(10, 10, 20, 20);
|
2011-02-11 09:02:35 -05:00
|
|
|
rect.leftCenter = [10, 15];
|
|
|
|
var point = rect.leftCenter;
|
2011-05-21 15:11:53 -04:00
|
|
|
equals(point.toString(), '{ x: 10, y: 15 }');
|
2011-02-07 13:28:09 -05:00
|
|
|
});
|
|
|
|
|
2011-02-11 09:02:35 -05:00
|
|
|
test('get rightCenter', function() {
|
2011-02-07 13:28:09 -05:00
|
|
|
var rect = new Rectangle(10, 10, 20, 20);
|
2011-02-11 09:02:35 -05:00
|
|
|
var point = rect.rightCenter;
|
2011-05-21 15:11:53 -04:00
|
|
|
equals(point.toString(), '{ x: 30, y: 20 }');
|
2011-02-07 13:28:09 -05:00
|
|
|
});
|
|
|
|
|
2011-02-11 09:02:35 -05:00
|
|
|
test('set rightCenter', function() {
|
2011-02-07 13:28:09 -05:00
|
|
|
var rect = new Rectangle(10, 10, 20, 20);
|
2011-02-11 09:02:35 -05:00
|
|
|
rect.rightCenter = [10, 15];
|
|
|
|
var point = rect.rightCenter;
|
2011-05-21 15:11:53 -04:00
|
|
|
equals(point.toString(), '{ x: 10, y: 15 }');
|
2011-02-07 13:28:09 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
test('intersects(rect)', function() {
|
|
|
|
var rect1 = new Rectangle({ x: 160, y: 270, width: 20, height: 20 });
|
|
|
|
var rect2 = { x: 195, y: 301, width: 19, height: 19 };
|
2011-05-07 12:46:06 -04:00
|
|
|
equals(function() {
|
|
|
|
return rect1.intersects(rect2);
|
|
|
|
}, false);
|
2011-02-07 13:28:09 -05:00
|
|
|
rect1 = new Rectangle({ x: 160, y: 270, width: 20, height: 20 });
|
|
|
|
rect2 = { x: 170.5, y: 280.5, width: 19, height: 19 };
|
2011-05-07 12:46:06 -04:00
|
|
|
equals(function() {
|
|
|
|
return rect1.intersects(rect2);
|
|
|
|
}, true);
|
2011-02-07 13:28:09 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
test('contains(rect)', function() {
|
|
|
|
var rect1 = new Rectangle({ x: 160, y: 270, width: 20, height: 20 });
|
|
|
|
var rect2 = { x: 195, y: 301, width: 19, height: 19 };
|
2011-05-07 12:46:06 -04:00
|
|
|
equals(function() {
|
|
|
|
return rect1.contains(rect2);
|
|
|
|
}, false);
|
2011-02-07 13:28:09 -05:00
|
|
|
rect1 = new Rectangle({ x: 160, y: 270, width: 20, height: 20 });
|
|
|
|
rect2 = new Rectangle({ x: 170.5, y: 280.5, width: 19, height: 19 });
|
2011-05-07 12:46:06 -04:00
|
|
|
equals(function() {
|
|
|
|
return rect1.contains(rect2);
|
|
|
|
}, false);
|
|
|
|
|
2011-02-07 13:28:09 -05:00
|
|
|
rect1 = new Rectangle({ x: 299, y: 161, width: 137, height: 129 });
|
|
|
|
rect2 = new Rectangle({ x: 340, y: 197, width: 61, height: 61 });
|
2011-05-07 12:46:06 -04:00
|
|
|
equals(function() {
|
|
|
|
return rect1.contains(rect2);
|
|
|
|
}, true);
|
|
|
|
equals(function() {
|
|
|
|
return rect2.contains(rect1);
|
2011-05-21 15:11:53 -04:00
|
|
|
}, false);
|
|
|
|
});
|
2011-02-07 13:28:09 -05:00
|
|
|
|
|
|
|
test('contains(point)', function() {
|
|
|
|
var rect = new Rectangle({ x: 160, y: 270, width: 20, height: 20 });
|
|
|
|
var point = new Point(166, 280);
|
2011-05-07 12:46:06 -04:00
|
|
|
equals(function() {
|
|
|
|
return rect.contains(point);
|
|
|
|
}, true);
|
2011-02-07 13:28:09 -05:00
|
|
|
var point = new Point(30, 30);
|
2011-05-07 12:46:06 -04:00
|
|
|
equals(function() {
|
|
|
|
return rect.contains(point);
|
2011-05-21 15:11:53 -04:00
|
|
|
}, false);
|
|
|
|
});
|
2011-02-07 13:28:09 -05:00
|
|
|
|
|
|
|
test('intersect(rect)', function() {
|
|
|
|
var rect1 = new Rectangle({ x: 160, y: 270, width: 20, height: 20 });
|
|
|
|
var rect2 = { x: 170.5, y: 280.5, width: 19, height: 19 };
|
|
|
|
var intersected = rect1.intersect(rect2);
|
2011-05-07 12:46:06 -04:00
|
|
|
equals(function() {
|
|
|
|
return intersected.equals({ x: 170.5, y: 280.5, width: 9.5, height: 9.5 });
|
|
|
|
}, true);
|
2011-02-07 13:28:09 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
test('unite(rect)', function() {
|
|
|
|
var rect1 = new Rectangle({ x: 160, y: 270, width: 20, height: 20 });
|
|
|
|
var rect2 = { x: 170.5, y: 280.5, width: 19, height: 19 };
|
|
|
|
var united = rect1.unite(rect2);
|
2011-05-07 12:46:06 -04:00
|
|
|
equals(function() {
|
|
|
|
return united.equals({ x: 160, y: 270, width: 29.5, height: 29.5 });
|
|
|
|
}, true);
|
2011-02-07 13:28:09 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
test('include(point)', function() {
|
|
|
|
var rect1 = new Rectangle({ x: 95, y: 151, width: 20, height: 20 });
|
|
|
|
var included = rect1.include([50, 50]);
|
2011-05-07 12:46:06 -04:00
|
|
|
equals(function() {
|
|
|
|
return included.equals({ x: 50, y: 50, width: 65, height: 121 });
|
|
|
|
}, true);
|
2011-02-07 13:28:09 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
test('toString()', function() {
|
|
|
|
var string = new Rectangle(10, 20, 30, 40).toString();
|
|
|
|
equals(string, '{ x: 10, y: 20, width: 30, height: 40 }');
|
|
|
|
});
|