Spaces:
Sleeping
Sleeping
File size: 6,571 Bytes
9a14526 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | 'use strict';
var test = require('tape').test;
var Point = require('./');
test('Point', function(t) {
t.test('.convert', function(t) {
t.equal(Point.convert(new Point(20, 30)).equals(new Point(20, 30)), true);
t.equal(Point.convert([20, 30]).equals(new Point(20, 30)), true);
t.equal(Point.convert('somestring'), 'somestring');
t.end();
});
t.test('vector operations', function(t) {
t.test('#mag', function(t) {
t.test('gets the magnitude of a vector', function() {
t.equal(new Point(0, 2).mag(), 2);
t.equal(new Point(0, 0).mag(), 0);
t.equal(new Point(10, 0).mag(), 10);
t.end();
});
});
t.test('#unit', function(t) {
t.test('calculates the unit vector', function() {
t.deepEqual(new Point(0, 1000).unit(), new Point(0, 1));
t.end();
});
});
t.test('#equals', function(t) {
t.equals((new Point(0, 0).equals(new Point(0, 0))), true, 'equal');
t.equals((new Point(0, 0).equals(new Point(0, 10))), false, 'not equal');
t.end();
});
t.test('#dist', function(t) {
t.equals((new Point(0, 10).dist(new Point(0, 0))), 10);
t.equals((new Point(Math.sqrt(2), Math.sqrt(2)).dist(new Point(0, 0))), 2);
t.equals((new Point(0, 0).dist(new Point(0, 0))), 0);
t.end();
});
t.test('#mult', function(t) {
t.equals((new Point(0, 0).mult(5)).equals(new Point(0, 0)), true);
t.equals((new Point(0, 1).mult(5)).equals(new Point(0, 5)), true);
t.equals((new Point(1, 1).mult(5)).equals(new Point(5, 5)), true);
t.end();
});
t.test('#div', function(t) {
t.equals((new Point(0, 0).div(5)).equals(new Point(0, 0)), true);
t.equals((new Point(0, 1).div(5)).equals(new Point(0, 1/5)), true);
t.equals((new Point(1, 1).div(5)).equals(new Point(1/5, 1/5)), true);
t.end();
});
t.test('#multByPoint', function(t) {
t.equals((new Point(0, 0).multByPoint(new Point(5,5))).equals(new Point(0, 0)), true);
t.equals((new Point(0, 1).multByPoint(new Point(5,5))).equals(new Point(0, 5)), true);
t.equals((new Point(1, 1).multByPoint(new Point(4,5))).equals(new Point(4, 5)), true);
t.end();
});
t.test('#divByPoint', function(t) {
t.equals((new Point(0, 0).divByPoint(new Point(5,5))).equals(new Point(0, 0)), true);
t.equals((new Point(0, 1).divByPoint(new Point(5,5))).equals(new Point(0, 1/5)), true);
t.equals((new Point(1, 1).divByPoint(new Point(4,5))).equals(new Point(1/4, 1/5)), true);
t.end();
});
t.test('#rotate', function(t) {
t.equals((new Point(0, 0).rotate(0)).equals(new Point(0, 0)), true);
t.deepEquals((new Point(0, 1).rotate(Math.PI/2)).round(), new Point(-1, 0));
t.deepEquals((new Point(0, 1).rotate(Math.PI)).round(), new Point(0, -1));
t.end();
});
t.test('#rotateAround', function(t) {
t.deepEquals((new Point(2, 3).rotateAround(Math.PI/2, new Point(2,2))).round(), new Point(1, 2));
t.deepEquals((new Point(2, 3).rotateAround(Math.PI, new Point(2,2))).round(), new Point(2, 1));
t.end();
});
t.test('#round', function(t) {
t.equals((new Point(0, 0).round()).equals(new Point(0, 0)), true);
t.equals((new Point(0, 0.5).round()).equals(new Point(0, 1)), true);
t.equals((new Point(0.2, 0.2).round()).equals(new Point(0, 0)), true);
t.end();
});
t.test('#angle', function(t) {
t.equals((new Point(0, 0).angle()), 0);
t.equals((new Point(10, 10).angle()), Math.PI / 4);
t.equals((new Point(0, 10).angle()), Math.PI / 2);
t.equals((new Point(10, 0).angle()), 0);
t.end();
});
t.test('#angleTo', function(t) {
var b = new Point(0, 0);
t.equals((new Point(0, 0).angleTo(b)), 0);
t.equals((new Point(10, 10).angleTo(b)), Math.PI / 4);
t.equals((new Point(0, 10).angleTo(b)), Math.PI / 2);
t.equals((new Point(10, 0).angleTo(b)), 0);
t.end();
});
t.test('#angleWith', function(t) {
var b = new Point(0, 0);
t.equals((new Point(0, 0).angleWith(b)), 0);
t.equals((new Point(10, 10).angleWith(b)), 0);
t.equals((new Point(0, 10).angleWith(b)), 0);
t.equals((new Point(10, 0).angleWith(b)), 0);
t.end();
});
t.test('#angleWithSep', function(t) {
t.equals((new Point(0, 0).angleWithSep(0, 0)), 0);
t.equals((new Point(10, 10).angleWithSep(0, 0)), 0);
t.equals((new Point(0, 10).angleWithSep(0, 0)), 0);
t.equals((new Point(10, 0).angleWithSep(0, 0)), 0);
t.end();
});
t.test('#matMult', function(t) {
t.equals((new Point(0, 0).matMult([0, 0, 0, 0])).equals(new Point(0, 0)), true);
t.deepEquals((new Point(1, 1).matMult([0, 0, 0, 0])), new Point(0, 0));
t.deepEquals((new Point(1, 1).matMult([1, 0, 1, 0])), new Point(1, 1));
t.deepEquals((new Point(1, 1).matMult([1, 0, 0, 0])), new Point(1, 0));
t.deepEquals((new Point(1, 1).matMult([0, 0, 1, 0])), new Point(0, 1));
t.deepEquals((new Point(1, 1).matMult([0, 0, 1, 2])), new Point(0, 3));
t.deepEquals((new Point(1, 1).matMult([1, 1, 1, 1])), new Point(2, 2));
t.end();
});
t.test('#perp', function(t) {
t.test('calculates a vector perpendicular to the given vector', function() {
t.deepEqual(new Point(0, 1000).perp(), new Point(-1000, 0));
t.end();
});
});
t.test('#add', function(t) {
t.test('adds two vectors', function() {
t.deepEqual(new Point(0, 0).add(new Point(10, 10)), new Point(10, 10));
t.end();
});
});
t.test('#sub', function(t) {
t.test('adds subtracts a vector from another', function() {
t.deepEqual(new Point(0, 0).sub(new Point(10, 10)), new Point(-10, -10));
t.end();
});
});
});
});
|