-
Notifications
You must be signed in to change notification settings - Fork 31
/
test-vector-ops.js
105 lines (88 loc) · 3.74 KB
/
test-vector-ops.js
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
require(["jquery", "VectorOps"], function ($, VectorOps) {
$(document).ready(function () {
// Setup test variables
module("VectorOps", {
setup: function () {},
teardown: function () {},
});
// tests the constructor of bp tree
test("Test getAngle", function () {
// point on the positive x-axis
deepEqual(VectorOps.getAngle([1, 0]), { cos: 1, sin: 0 });
deepEqual(VectorOps.getAngle([100, 0]), { cos: 1, sin: 0 });
//point on the negative x-axis
deepEqual(VectorOps.getAngle([-1, 0]), { cos: -1, sin: 0 });
// point on the positive y-axis
deepEqual(VectorOps.getAngle([0, 1]), { cos: 0, sin: 1 });
// point on the negative y-axis
deepEqual(VectorOps.getAngle([0, -1]), { cos: 0, sin: -1 });
// arbitrary point
var angle = VectorOps.getAngle([1, 1]);
ok(Math.abs(angle.cos - Math.sqrt(2) / 2 < 1.0e-15));
ok(Math.abs(angle.sin - Math.sqrt(2) / 2 < 1.0e-15));
// arbitrary point
angle = VectorOps.getAngle([(-5 * Math.sqrt(3)) / 2, -5 / 2]);
ok(Math.abs(angle.cos - (-1 * Math.sqrt(3)) / 2) < 1.0e-15);
// prettier-ignore
ok(Math.abs(angle.sin - (-1 / 2)) < 1.0e-15);
});
test("Test magnitude", function () {
equal(VectorOps.magnitude([0, 0]), 0);
equal(VectorOps.magnitude([1, 1]), Math.sqrt(2));
equal(VectorOps.magnitude([1, -1]), Math.sqrt(2));
equal(VectorOps.magnitude([-5, -2]), Math.sqrt(29));
});
test("Test rotate", function () {
// rotate [0,1] by 0 or 360 degrees
deepEqual(VectorOps.rotate([0, 1], { cos: 1, sin: 0 }), [0, 1]);
// rotate [-1, -1] by 90 degress
deepEqual(VectorOps.rotate([-1, -1], { cos: 0, sin: 1 }), [1, -1]);
// rotate [1,1] by 240 degrees
var angle = {
cos: -1 / 2,
sin: (-1 * Math.sqrt(3)) / 2,
};
var rPoint = VectorOps.rotate([1, 1], angle);
ok(Math.abs(rPoint[0] - 0.3660254037844386) < 1.0e-15);
// prettier-ignore
ok(Math.abs(rPoint[1] - (-1.3660254037844386)) < 1.0e-15);
});
test("Test translate", function () {
deepEqual(VectorOps.translate([0, 0], 1, 2), [1, 2]);
deepEqual(VectorOps.translate([1, 2], -1, -2), [0, 0]);
});
test("Test computeBoxCorners", function () {
var box = VectorOps.computeBoxCorners(-2, 0, 3, 0, 2);
deepEqual(box, {
bL: [-2, 2],
bR: [-2, -2],
tL: [3, 2],
tR: [3, -2],
});
box = VectorOps.computeBoxCorners(0, -2, 0, 3, 2);
deepEqual(box, {
bL: [-2, -2],
bR: [2, -2],
tL: [-2, 3],
tR: [2, 3],
});
});
test("Test triangleArea", function () {
// these three tests use the same triangle but each test will
// move/rotate the triangle.
equal(VectorOps.triangleArea([0, 0], [20, 0], [0, 12]), 120);
equal(VectorOps.triangleArea([0, 0], [-20, 0], [0, -12]), 120);
equal(VectorOps.triangleArea([1, -1], [-19, -1], [1, -13]), 120);
equal(
VectorOps.triangleArea([0, 0], [0, 0], [0, 12]),
0,
"Area of a line should be zero"
);
equal(
VectorOps.triangleArea([0, 12], [0, 12], [0, 12]),
0,
"Area of a point should be zero"
);
});
});
});