aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/math/vec2_test.html
blob: daa1335d566167b87549e915ba1315b2e2e6aa0f (plain)
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<!DOCTYPE html>
<html>
<!--
Copyright 2006 The Closure Library Authors. All Rights Reserved.

Use of this source code is governed by the Apache License, Version 2.0.
See the COPYING file for details.
-->
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Closure Unit Tests - goog.math.Vec2</title>
<script src="../base.js"></script>
<script>
  goog.require('goog.math.Coordinate');
  goog.require('goog.math.Vec2');
  goog.require('goog.testing.jsunit');
</script>
</head>
<body>
<script>

function assertVectorEquals(a, b) {
  assertTrue(b + ' should be equal to ' + a, goog.math.Vec2.equals(a, b));
}


function testVec2() {
  var v = new goog.math.Vec2(3.14, 2.78);
  assertEquals(3.14, v.x);
  assertEquals(2.78, v.y);
}


function testRandomUnit() {
  var a = goog.math.Vec2.randomUnit();
  assertRoughlyEquals(1.0, a.magnitude(), 1e-10);
}


function testRandom() {
  var a = goog.math.Vec2.random();
  assertTrue(a.magnitude() <= 1.0);
}


function testClone() {
  var a = new goog.math.Vec2(1, 2);
  var b = a.clone();

  assertEquals(a.x, b.x);
  assertEquals(a.y, b.y);
}


function testMagnitude() {
  var a = new goog.math.Vec2(0, 10);
  var b = new goog.math.Vec2(3, 4);

  assertEquals(10, a.magnitude());
  assertEquals(5, b.magnitude());
}


function testSquaredMagnitude() {
  var a = new goog.math.Vec2(-3, -4);
  assertEquals(25, a.squaredMagnitude());
}


function testScale() {
  var a = new goog.math.Vec2(1, 2);
  a.scale(0.5);

  assertEquals(0.5, a.x);
  assertEquals(1, a.y);
}


function testInvert() {
  var a = new goog.math.Vec2(3, 4);
  a.invert();

  assertEquals(-3, a.x);
  assertEquals(-4, a.y);
}


function testNormalize() {
  var a = new goog.math.Vec2(5, 5);
  a.normalize();
  assertRoughlyEquals(1.0, a.magnitude(), 1e-10);
}


function testAdd() {
  var a = new goog.math.Vec2(1, -1);
  a.add(new goog.math.Vec2(3, 3));
  assertVectorEquals(new goog.math.Vec2(4, 2), a);
}


function testSubtract() {
  var a = new goog.math.Vec2(1, -1);
  a.subtract(new goog.math.Vec2(3, 3));
  assertVectorEquals(new goog.math.Vec2(-2, -4), a);
}


function testRotate() {
  var a = new goog.math.Vec2(1, -1);
  a.rotate(Math.PI / 2);
  assertRoughlyEquals(1, a.x, 0.000001);
  assertRoughlyEquals(1, a.y, 0.000001);
  a.rotate(-Math.PI);
  assertRoughlyEquals(-1, a.x, 0.000001);
  assertRoughlyEquals(-1, a.y, 0.000001);
}


function testRotateAroundPoint() {
  var a = goog.math.Vec2.rotateAroundPoint(
      new goog.math.Vec2(1, -1), new goog.math.Vec2(1, 0), Math.PI / 2);
  assertRoughlyEquals(2, a.x, 0.000001);
  assertRoughlyEquals(0, a.y, 0.000001);
}


function testEquals() {
  var a = new goog.math.Vec2(1, 2);

  assertFalse(a.equals(null));
  assertFalse(a.equals(new goog.math.Vec2(1, 3)));
  assertFalse(a.equals(new goog.math.Vec2(2, 2)));

  assertTrue(a.equals(a));
  assertTrue(a.equals(new goog.math.Vec2(1, 2)));
}


function testSum() {
  var a = new goog.math.Vec2(0.5, 0.25);
  var b = new goog.math.Vec2(0.5, 0.75);

  var c = goog.math.Vec2.sum(a, b);
  assertVectorEquals(new goog.math.Vec2(1, 1), c);
}


function testDifference() {
  var a = new goog.math.Vec2(0.5, 0.25);
  var b = new goog.math.Vec2(0.5, 0.75);

  var c = goog.math.Vec2.difference(a, b);
  assertVectorEquals(new goog.math.Vec2(0, -0.5), c);
}


function testDistance() {
  var a = new goog.math.Vec2(3, 4);
  var b = new goog.math.Vec2(-3, -4);

  assertEquals(10, goog.math.Vec2.distance(a, b));
}


function testSquaredDistance() {
  var a = new goog.math.Vec2(3, 4);
  var b = new goog.math.Vec2(-3, -4);

  assertEquals(100, goog.math.Vec2.squaredDistance(a, b));
}


function testVec2Equals() {
  assertTrue(goog.math.Vec2.equals(null, null));
  assertFalse(goog.math.Vec2.equals(null, new goog.math.Vec2()));

  var a = new goog.math.Vec2(1, 3);
  assertTrue(goog.math.Vec2.equals(a, a));
  assertTrue(goog.math.Vec2.equals(a, new goog.math.Vec2(1, 3)));
  assertFalse(goog.math.Vec2.equals(1, new goog.math.Vec2(3, 1)));
}


function testDot() {
  var a = new goog.math.Vec2(0, 5);
  var b = new goog.math.Vec2(3, 0);
  assertEquals(0, goog.math.Vec2.dot(a, b));

  var c = new goog.math.Vec2(-5, -5);
  var d = new goog.math.Vec2(0, 7);
  assertEquals(-35, goog.math.Vec2.dot(c, d));
}


function testLerp() {
  var a = new goog.math.Vec2(0, 0);
  var b = new goog.math.Vec2(10, 10);

  for (var i = 0; i <= 10; i++) {
    var c = goog.math.Vec2.lerp(a, b, i / 10);
    assertEquals(i, c.x);
    assertEquals(i, c.y);
  }
}


function testToString() {
  testEquals('(0, 0)', new goog.math.Vec2(0, 0).toString());
}

</script>
</body>
</html>