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/
|
|
|
|
*
|
2020-05-23 16:24:42 -04:00
|
|
|
* Copyright (c) 2011 - 2020, Jürg Lehni & Jonathan Puckey
|
|
|
|
* http://juerglehni.com/ & https://puckey.studio/
|
2011-07-01 06:17:45 -04:00
|
|
|
*
|
|
|
|
* Distributed under the MIT license. See LICENSE file for details.
|
|
|
|
*
|
|
|
|
* All rights reserved.
|
|
|
|
*/
|
|
|
|
|
2016-01-27 13:57:07 -05:00
|
|
|
QUnit.module('Point');
|
|
|
|
|
2011-02-13 11:26:24 -05:00
|
|
|
test('new Point(10, 20)', function() {
|
2014-08-16 13:24:54 -04:00
|
|
|
var point = new Point(10, 20);
|
2014-12-28 08:59:48 -05:00
|
|
|
equals(point.x, 10, 'point.x');
|
|
|
|
equals(point.y, 20, 'point.y');
|
2011-02-07 13:28:09 -05:00
|
|
|
});
|
|
|
|
|
2011-02-13 11:26:24 -05:00
|
|
|
test('new Point([10, 20])', function() {
|
2014-08-16 13:24:54 -04:00
|
|
|
var point = new Point([10, 20]);
|
2014-12-28 08:59:48 -05:00
|
|
|
equals(point.x, 10, 'point.x');
|
|
|
|
equals(point.y, 20, 'point.y');
|
2011-02-07 13:28:09 -05:00
|
|
|
});
|
|
|
|
|
2011-02-13 11:26:24 -05:00
|
|
|
test('new Point({x: 10, y: 20})', function() {
|
2014-12-28 08:59:48 -05:00
|
|
|
var point = new Point({ x: 10, y: 20 });
|
|
|
|
equals(point.x, 10, 'point.x');
|
|
|
|
equals(point.y, 20, 'point.y');
|
2011-02-07 13:28:09 -05:00
|
|
|
});
|
|
|
|
|
2011-02-13 11:26:24 -05:00
|
|
|
test('new Point(new Size(10, 20))', function() {
|
2016-05-27 05:37:19 -04:00
|
|
|
equals(new Point(new Size(10, 20)), new Point(10, 20));
|
2011-02-07 13:28:09 -05:00
|
|
|
});
|
|
|
|
|
2011-02-13 11:26:24 -05:00
|
|
|
test('new Point({ width: 10, height: 20})', function() {
|
2016-05-27 05:37:19 -04:00
|
|
|
equals(new Point({width: 10, height: 20}), new Point(10, 20));
|
2011-02-07 13:28:09 -05:00
|
|
|
});
|
|
|
|
|
2011-05-21 15:01:01 -04:00
|
|
|
test('new Point({ angle: 45, length: 20})', function() {
|
2016-05-27 05:37:19 -04:00
|
|
|
equals(new Point({ angle: 40, length: 20 }), new Point(15.32089, 12.85575));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('new Point("10, 20")', function() {
|
|
|
|
equals(new Point('10, 20'), new Point(10, 20));
|
|
|
|
equals(new Point('10,20'), new Point(10, 20));
|
|
|
|
equals(new Point('10 20'), new Point(10, 20));
|
2016-07-18 14:11:01 -04:00
|
|
|
// Make sure it's integer values from the string:
|
|
|
|
equals(new Point('10 20').add(10), new Point(20, 30));
|
2011-05-21 15:01:01 -04:00
|
|
|
});
|
|
|
|
|
2011-02-13 11:26:24 -05:00
|
|
|
test('normalize(length)', function() {
|
2014-08-16 13:24:54 -04:00
|
|
|
var point = new Point(0, 10).normalize(20);
|
2014-12-28 08:59:48 -05:00
|
|
|
equals(point, new Point(0, 20));
|
2011-02-07 13:28:09 -05:00
|
|
|
});
|
|
|
|
|
2011-02-13 11:26:24 -05:00
|
|
|
test('set length', function() {
|
2014-08-16 13:24:54 -04:00
|
|
|
var point = new Point(0, 10);
|
|
|
|
point.length = 20;
|
2014-12-28 08:59:48 -05:00
|
|
|
equals(point, new Point(0, 20));
|
2011-02-07 13:28:09 -05:00
|
|
|
});
|
|
|
|
|
2011-02-13 11:26:24 -05:00
|
|
|
test('get angle', function() {
|
2014-08-16 13:24:54 -04:00
|
|
|
var angle = new Point(0, 10).angle;
|
2011-02-07 13:28:09 -05:00
|
|
|
equals(angle, 90);
|
|
|
|
});
|
|
|
|
|
2011-02-13 11:26:24 -05:00
|
|
|
test('getAngle(point)', function() {
|
2014-08-16 13:24:54 -04:00
|
|
|
var angle = new Point(0, 10).getAngle([10, 10]);
|
2011-02-07 13:28:09 -05:00
|
|
|
equals(Math.round(angle), 45);
|
|
|
|
});
|
|
|
|
|
2011-02-13 11:26:24 -05:00
|
|
|
test('rotate(degrees)', function() {
|
2014-08-16 13:24:54 -04:00
|
|
|
var point = new Point(100, 50).rotate(90);
|
2014-12-28 08:59:48 -05:00
|
|
|
equals(point, new Point(-50, 100));
|
2011-02-07 13:28:09 -05:00
|
|
|
});
|
|
|
|
|
2011-02-13 11:26:24 -05:00
|
|
|
test('set angle', function() {
|
2014-08-16 13:24:54 -04:00
|
|
|
var point = new Point(10, 20);
|
|
|
|
point.angle = 92;
|
2011-02-11 09:02:35 -05:00
|
|
|
equals(point.angle, 92);
|
2014-12-28 08:59:48 -05:00
|
|
|
equals(point, new Point({
|
2014-08-16 13:24:54 -04:00
|
|
|
angle: 92,
|
|
|
|
length: Math.sqrt(10 * 10 + 20 * 20)
|
|
|
|
}));
|
2011-02-07 13:28:09 -05:00
|
|
|
});
|
|
|
|
|
2013-06-23 19:55:14 -04:00
|
|
|
test('set angle & length', function() {
|
2014-08-16 13:24:54 -04:00
|
|
|
var point1 = new Point();
|
2016-12-29 03:36:54 -05:00
|
|
|
point1.length = Math.SQRT2;
|
2014-08-16 13:24:54 -04:00
|
|
|
point1.angle = -45;
|
2013-06-23 19:55:14 -04:00
|
|
|
|
2014-08-16 13:24:54 -04:00
|
|
|
var point2 = new Point();
|
|
|
|
point2.angle = -45;
|
2016-12-29 03:36:54 -05:00
|
|
|
point2.length = Math.SQRT2;
|
2013-06-23 19:55:14 -04:00
|
|
|
|
2014-12-28 08:59:48 -05:00
|
|
|
equals(point2, point1);
|
2013-06-23 19:55:14 -04:00
|
|
|
});
|
|
|
|
|
2013-12-07 10:30:40 -05:00
|
|
|
test('getting angle after x / y change', function() {
|
2014-08-16 13:24:54 -04:00
|
|
|
var vector = new Point(1, 0);
|
|
|
|
equals(vector.angle, 0, 'angle before x / y change');
|
|
|
|
vector.x = 0;
|
|
|
|
vector.y = 1;
|
|
|
|
equals(vector.angle, 90, 'angle after x / y change');
|
2013-12-07 10:30:40 -05:00
|
|
|
});
|
|
|
|
|
2011-02-11 09:02:35 -05:00
|
|
|
test('getDirectedAngle(point)', function() {
|
2014-08-16 13:24:54 -04:00
|
|
|
equals(function() {
|
|
|
|
return new Point(10, 10).getDirectedAngle(new Point(1, 0));
|
|
|
|
}, -45);
|
2011-06-14 07:44:56 -04:00
|
|
|
|
2014-08-16 13:24:54 -04:00
|
|
|
equals(function() {
|
|
|
|
return new Point(-10, 10).getDirectedAngle(new Point(1, 0));
|
|
|
|
}, -135);
|
2011-06-14 07:44:56 -04:00
|
|
|
|
2014-08-16 13:24:54 -04:00
|
|
|
equals(function() {
|
|
|
|
return new Point(-10, -10).getDirectedAngle(new Point(1, 0));
|
|
|
|
}, 135);
|
2011-06-14 07:44:56 -04:00
|
|
|
|
2014-08-16 13:24:54 -04:00
|
|
|
equals(function() {
|
|
|
|
return new Point(10, -10).getDirectedAngle(new Point(1, 0));
|
|
|
|
}, 45);
|
2013-06-11 22:52:42 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
test('equals()', function() {
|
2014-08-16 13:24:54 -04:00
|
|
|
equals(function() {
|
|
|
|
return new Point(10, 10).equals([10, 10]);
|
|
|
|
}, true);
|
2013-06-11 22:52:42 -04:00
|
|
|
|
2014-08-16 13:24:54 -04:00
|
|
|
equals(function() {
|
|
|
|
return new Point(0, 0).equals({});
|
|
|
|
}, false);
|
2013-06-11 22:52:42 -04:00
|
|
|
|
2014-08-16 13:24:54 -04:00
|
|
|
equals(function() {
|
|
|
|
return new Point(0, 0).equals(null);
|
|
|
|
}, false);
|
2013-06-11 22:52:42 -04:00
|
|
|
});
|
2014-02-28 11:54:34 -05:00
|
|
|
|
2015-06-16 12:39:52 -04:00
|
|
|
test('isCollinear()', function() {
|
2014-08-16 13:24:54 -04:00
|
|
|
equals(function() {
|
2015-06-16 12:39:52 -04:00
|
|
|
return new Point(10, 5).isCollinear(new Point(20, 10));
|
2014-08-16 13:24:54 -04:00
|
|
|
}, true);
|
|
|
|
equals(function() {
|
2015-06-16 12:39:52 -04:00
|
|
|
return new Point(5, 10).isCollinear(new Point(-5, -10));
|
2014-08-16 13:24:54 -04:00
|
|
|
}, true);
|
|
|
|
equals(function() {
|
2015-06-16 12:39:52 -04:00
|
|
|
return new Point(10, 10).isCollinear(new Point(20, 10));
|
2014-08-16 13:24:54 -04:00
|
|
|
}, false);
|
|
|
|
equals(function() {
|
2015-06-16 12:39:52 -04:00
|
|
|
return new Point(10, 10).isCollinear(new Point(10, -10));
|
2014-08-16 13:24:54 -04:00
|
|
|
}, false);
|
2014-02-28 11:54:34 -05:00
|
|
|
});
|
2014-02-28 11:56:56 -05:00
|
|
|
|
|
|
|
test('isOrthogonal()', function() {
|
2014-08-16 13:24:54 -04:00
|
|
|
equals(function() {
|
|
|
|
return new Point(10, 5).isOrthogonal(new Point(5, -10));
|
|
|
|
}, true);
|
|
|
|
equals(function() {
|
|
|
|
return new Point(5, 10).isOrthogonal(new Point(-10, 5));
|
|
|
|
}, true);
|
|
|
|
equals(function() {
|
|
|
|
return new Point(10, 10).isOrthogonal(new Point(20, 20));
|
|
|
|
}, false);
|
|
|
|
equals(function() {
|
|
|
|
return new Point(10, 10).isOrthogonal(new Point(10, -20));
|
|
|
|
}, false);
|
2014-02-28 11:56:56 -05:00
|
|
|
});
|