-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector_ops_spec.js
More file actions
117 lines (93 loc) · 3.41 KB
/
Copy pathvector_ops_spec.js
File metadata and controls
117 lines (93 loc) · 3.41 KB
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
const { getVectorAngle, getRotationDelta, turnVector, getPossibleTurnRadians } = require('./vector_ops');
describe('getVectorAngle', function () {
it('(1, 0) = 0', function () {
let v = { x: 1, y: 0 };
expect(getVectorAngle(v)).toEqual(0);
});
it('(0, 1) = Math.PI / 2', function () {
let v = { x: 0, y: 1 };
expect(getVectorAngle(v)).toEqual(Math.PI / 2);
});
it('(-1, 0) = Math.PI', function () {
let v = { x: -1, y: 0 };
expect(getVectorAngle(v)).toEqual(Math.PI);
});
it('(0, -1) = Math.PI + Math.PI / 2', function () {
let v = { x: 0, y: -1 };
expect(getVectorAngle(v)).toEqual(Math.PI + (Math.PI / 2));
});
});
describe('getRotationDelta', function () {
it('should be to compute angle between opposite vectors', function () {
v1 = { x: 1, y: 0 };
v2 = { x: -1, y: 0 };
radians = getRotationDelta(v1, v2);
expect(radians).toEqual(Math.PI);
});
it('should be to compute angle between perpendicular vectors', function () {
v1 = { x: 1, y: 0 };
v2 = { x: 0, y: 1 };
radians = getRotationDelta(v1, v2);
expect(radians).toEqual(Math.PI / 2);
// The other way around.
radians = getRotationDelta(v2, v1);
expect(radians).toEqual(-Math.PI / 2);
});
it('should be to compute angle between vectors at beginning and end of angle value range', function () {
v1 = { x: 0.70711, y: 0.70711 };
v2 = { x: 0.70711, y: -0.70711 };
radians = getRotationDelta(v1, v2);
expect(radians).toEqual(-Math.PI / 2);
// The other way around.
radians = getRotationDelta(v2, v1);
expect(radians).toEqual(Math.PI / 2);
});
});
describe('turnVector', function () {
it('(1, 0) + (Math.PI / 2) = (0, 1)', function () {
v = { x: 1, y: 0 };
angle = Math.PI / 2;
radians = turnVector(v, angle);
expect(turnVector(v, angle)).toEqual({ x: 0, y: 1 });
});
it('(0, 1) + (Math.PI / 2) = (-1, 0)', function () {
v = { x: 0, y: 1 };
angle = Math.PI / 2;
radians = turnVector(v, angle);
expect(turnVector(v, angle)).toEqual({ x: -1, y: 0 });
});
it('(-1, 0) + (Math.PI / 2) = (0, -1)', function () {
v = { x: -1, y: 0 };
angle = Math.PI / 2;
radians = turnVector(v, angle);
expect(turnVector(v, angle)).toEqual({ x: 0, y: -1 });
});
it('(0, -1) + (Math.PI / 2) = (1, 0)', function () {
v = { x: 0, y: -1 };
angle = Math.PI / 2;
radians = turnVector(v, angle);
expect(turnVector(v, angle)).toEqual({ x: 1, y: 0 });
});
});
describe('getPossibleTurnRadians', function () {
it('turn exactly turnAcceleration', function () {
currentTurnSpeed = 0;
turnAcceleration = 0.1;
maxTurnSpeed = 1;
requestedTurn = 0.1;
nextTurnSpeed = getPossibleTurnRadians(currentTurnSpeed, turnAcceleration, maxTurnSpeed, requestedTurn);
expect(nextTurnSpeed).toEqual(0.1);
nextTurnSpeed = getPossibleTurnRadians(currentTurnSpeed, turnAcceleration, maxTurnSpeed, -requestedTurn);
expect(nextTurnSpeed).toEqual(-0.1);
});
it('turn more than turnAcceleration', function () {
currentTurnSpeed = 0;
turnAcceleration = 0.1;
maxTurnSpeed = 1;
requestedTurn = 0.2;
nextTurnSpeed = getPossibleTurnRadians(currentTurnSpeed, turnAcceleration, maxTurnSpeed, requestedTurn);
expect(nextTurnSpeed).toEqual(0.1);
nextTurnSpeed = getPossibleTurnRadians(currentTurnSpeed, turnAcceleration, maxTurnSpeed, -requestedTurn);
expect(nextTurnSpeed).toEqual(-0.1);
});
});