vec2f_test.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. // Copyright 2013 The Closure Library Authors. All Rights Reserved.
  2. // Use of this source code is governed by the Apache License, Version 2.0.
  3. ////////////////////////// NOTE ABOUT EDITING THIS FILE ///////////////////////
  4. // //
  5. // Any edits to this file must be applied to vec2d_test.js by running: //
  6. // swap_type.sh vec2f_test.js > vec2d_test.js //
  7. // //
  8. ////////////////////////// NOTE ABOUT EDITING THIS FILE ///////////////////////
  9. goog.provide('goog.vec.vec2fTest');
  10. goog.setTestOnly('goog.vec.vec2fTest');
  11. goog.require('goog.testing.jsunit');
  12. goog.require('goog.vec.vec2f');
  13. function testCreate() {
  14. var v = goog.vec.vec2f.create();
  15. assertElementsEquals([0, 0], v);
  16. }
  17. function testCreateFromArray() {
  18. var v = goog.vec.vec2f.createFromArray([1, 2]);
  19. assertElementsEquals([1, 2], v);
  20. }
  21. function testCreateFromValues() {
  22. var v = goog.vec.vec2f.createFromValues(1, 2);
  23. assertElementsEquals([1, 2], v);
  24. }
  25. function testClone() {
  26. var v0 = goog.vec.vec2f.createFromValues(1, 2);
  27. var v1 = goog.vec.vec2f.clone(v0);
  28. assertElementsEquals([1, 2], v1);
  29. }
  30. function testSet() {
  31. var v = goog.vec.vec2f.create();
  32. goog.vec.vec2f.setFromValues(v, 1, 2);
  33. assertElementsEquals([1, 2], v);
  34. goog.vec.vec2f.setFromArray(v, [4, 5]);
  35. assertElementsEquals([4, 5], v);
  36. var w = goog.vec.vec2f.create();
  37. goog.vec.vec2f.setFromValues(w, 1, 2);
  38. assertElementsEquals([1, 2], w);
  39. goog.vec.vec2f.setFromArray(w, [4, 5]);
  40. assertElementsEquals([4, 5], w);
  41. }
  42. function testAdd() {
  43. var v0 = goog.vec.vec2f.setFromArray(goog.vec.vec2f.create(), [1, 2]);
  44. var v1 = goog.vec.vec2f.setFromArray(goog.vec.vec2f.create(), [4, 5]);
  45. var v2 = goog.vec.vec2f.setFromVec2f(goog.vec.vec2f.create(), v0);
  46. goog.vec.vec2f.add(v2, v1, v2);
  47. assertElementsEquals([1, 2], v0);
  48. assertElementsEquals([4, 5], v1);
  49. assertElementsEquals([5, 7], v2);
  50. goog.vec.vec2f.add(goog.vec.vec2f.add(v0, v1, v2), v0, v2);
  51. assertElementsEquals([6, 9], v2);
  52. }
  53. function testSubtract() {
  54. var v0 = goog.vec.vec2f.setFromArray(goog.vec.vec2f.create(), [1, 2]);
  55. var v1 = goog.vec.vec2f.setFromArray(goog.vec.vec2f.create(), [4, 5]);
  56. var v2 = goog.vec.vec2f.setFromVec2f(goog.vec.vec2f.create(), v0);
  57. goog.vec.vec2f.subtract(v2, v1, v2);
  58. assertElementsEquals([1, 2], v0);
  59. assertElementsEquals([4, 5], v1);
  60. assertElementsEquals([-3, -3], v2);
  61. goog.vec.vec2f.setFromValues(v2, 0, 0);
  62. goog.vec.vec2f.subtract(v1, v0, v2);
  63. assertElementsEquals([3, 3], v2);
  64. v2 = goog.vec.vec2f.setFromVec2f(goog.vec.vec2f.create(), v0);
  65. goog.vec.vec2f.subtract(v2, v1, v2);
  66. assertElementsEquals([-3, -3], v2);
  67. goog.vec.vec2f.subtract(goog.vec.vec2f.subtract(v1, v0, v2), v0, v2);
  68. assertElementsEquals([2, 1], v2);
  69. }
  70. function testMultiply() {
  71. var v0 = goog.vec.vec2f.setFromArray(goog.vec.vec2f.create(), [1, 2]);
  72. var v1 = goog.vec.vec2f.setFromArray(goog.vec.vec2f.create(), [4, 5]);
  73. var v2 = goog.vec.vec2f.setFromVec2f(goog.vec.vec2f.create(), v0);
  74. goog.vec.vec2f.componentMultiply(v2, v1, v2);
  75. assertElementsEquals([1, 2], v0);
  76. assertElementsEquals([4, 5], v1);
  77. assertElementsEquals([4, 10], v2);
  78. goog.vec.vec2f.componentMultiply(goog.vec.vec2f.componentMultiply(v0, v1, v2), v0, v2);
  79. assertElementsEquals([4, 20], v2);
  80. }
  81. function testDivide() {
  82. var v0 = goog.vec.vec2f.setFromArray(goog.vec.vec2f.create(), [1, 2]);
  83. var v1 = goog.vec.vec2f.setFromArray(goog.vec.vec2f.create(), [4, 5]);
  84. var v2 = goog.vec.vec2f.setFromVec2f(goog.vec.vec2f.create(), v0);
  85. goog.vec.vec2f.componentDivide(v2, v1, v2);
  86. assertElementsRoughlyEqual([1, 2], v0, 10e-5);
  87. assertElementsRoughlyEqual([4, 5], v1, 10e-5);
  88. assertElementsRoughlyEqual([.25, .4], v2, 10e-5);
  89. goog.vec.vec2f.setFromValues(v2, 0, 0);
  90. goog.vec.vec2f.componentDivide(v1, v0, v2);
  91. assertElementsRoughlyEqual([4, 2.5], v2, 10e-5);
  92. v2 = goog.vec.vec2f.setFromVec2f(goog.vec.vec2f.create(), v0);
  93. goog.vec.vec2f.componentDivide(v2, v1, v2);
  94. assertElementsRoughlyEqual([.25, .4], v2, 10e-5);
  95. goog.vec.vec2f.componentDivide(goog.vec.vec2f.componentDivide(v1, v0, v2), v0, v2);
  96. assertElementsRoughlyEqual([4, 1.25], v2, 10e-5);
  97. }
  98. function testNegate() {
  99. var v0 = goog.vec.vec2f.setFromArray(goog.vec.vec2f.create(), [1, 2]);
  100. var v1 = goog.vec.vec2f.create();
  101. goog.vec.vec2f.negate(v0, v1);
  102. assertElementsEquals([-1, -2], v1);
  103. assertElementsEquals([1, 2], v0);
  104. goog.vec.vec2f.negate(v0, v0);
  105. assertElementsEquals([-1, -2], v0);
  106. }
  107. function testAbs() {
  108. var v0 = goog.vec.vec2f.setFromArray(goog.vec.vec2f.create(), [-1, -2]);
  109. var v1 = goog.vec.vec2f.create();
  110. goog.vec.vec2f.abs(v0, v1);
  111. assertElementsEquals([1, 2], v1);
  112. assertElementsEquals([-1, -2], v0);
  113. goog.vec.vec2f.abs(v0, v0);
  114. assertElementsEquals([1, 2], v0);
  115. }
  116. function testScale() {
  117. var v0 = goog.vec.vec2f.setFromArray(goog.vec.vec2f.create(), [1, 2]);
  118. var v1 = goog.vec.vec2f.create();
  119. goog.vec.vec2f.scale(v0, 4, v1);
  120. assertElementsEquals([4, 8], v1);
  121. assertElementsEquals([1, 2], v0);
  122. goog.vec.vec2f.setFromArray(v1, v0);
  123. goog.vec.vec2f.scale(v1, 5, v1);
  124. assertElementsEquals([5, 10], v1);
  125. }
  126. function testMagnitudeSquared() {
  127. var v0 = goog.vec.vec2f.setFromArray(goog.vec.vec2f.create(), [1, 2]);
  128. assertEquals(5, goog.vec.vec2f.magnitudeSquared(v0));
  129. }
  130. function testMagnitude() {
  131. var v0 = goog.vec.vec2f.setFromArray(goog.vec.vec2f.create(), [1, 2]);
  132. assertEquals(Math.sqrt(5), goog.vec.vec2f.magnitude(v0));
  133. }
  134. function testNormalize() {
  135. var v0 = goog.vec.vec2f.setFromArray(goog.vec.vec2f.create(), [2, 3]);
  136. var v1 = goog.vec.vec2f.create();
  137. var v2 = goog.vec.vec2f.create();
  138. goog.vec.vec2f.scale(
  139. v0, 1 / goog.vec.vec2f.magnitude(v0), v2);
  140. goog.vec.vec2f.normalize(v0, v1);
  141. assertElementsEquals(v2, v1);
  142. assertElementsEquals([2, 3], v0);
  143. goog.vec.vec2f.setFromArray(v1, v0);
  144. goog.vec.vec2f.normalize(v1, v1);
  145. assertElementsEquals(v2, v1);
  146. }
  147. function testDot() {
  148. var v0 = goog.vec.vec2f.setFromArray(goog.vec.vec2f.create(), [1, 2]);
  149. var v1 = goog.vec.vec2f.setFromArray(goog.vec.vec2f.create(), [4, 5]);
  150. assertEquals(14, goog.vec.vec2f.dot(v0, v1));
  151. assertEquals(14, goog.vec.vec2f.dot(v1, v0));
  152. }
  153. function testDistanceSquared() {
  154. var v0 = goog.vec.vec2f.setFromValues(goog.vec.vec2f.create(), 1, 2);
  155. var v1 = goog.vec.vec2f.setFromValues(goog.vec.vec2f.create(), 1, 2);
  156. assertEquals(0, goog.vec.vec2f.distanceSquared(v0, v1));
  157. goog.vec.vec2f.setFromValues(v0, 1, 2);
  158. goog.vec.vec2f.setFromValues(v1, -1, -2);
  159. assertEquals(20, goog.vec.vec2f.distanceSquared(v0, v1));
  160. }
  161. function testDistance() {
  162. var v0 = goog.vec.vec2f.setFromValues(goog.vec.vec2f.create(), 1, 2);
  163. var v1 = goog.vec.vec2f.setFromValues(goog.vec.vec2f.create(), 1, 2);
  164. assertEquals(0, goog.vec.vec2f.distance(v0, v1));
  165. goog.vec.vec2f.setFromValues(v0, 2, 3);
  166. goog.vec.vec2f.setFromValues(v1, -2, 0);
  167. assertEquals(5, goog.vec.vec2f.distance(v0, v1));
  168. }
  169. function testDirection() {
  170. var v0 = goog.vec.vec2f.setFromValues(goog.vec.vec2f.create(), 1, 2);
  171. var v1 = goog.vec.vec2f.setFromValues(goog.vec.vec2f.create(), 1, 2);
  172. var dirVec = goog.vec.vec2f.setFromValues(goog.vec.vec2f.create(), 4, 5);
  173. goog.vec.vec2f.direction(v0, v1, dirVec);
  174. assertElementsEquals([0, 0], dirVec);
  175. goog.vec.vec2f.setFromValues(v0, 0, 0);
  176. goog.vec.vec2f.setFromValues(v1, 1, 0);
  177. goog.vec.vec2f.direction(v0, v1, dirVec);
  178. assertElementsEquals([1, 0], dirVec);
  179. goog.vec.vec2f.setFromValues(v0, 1, 1);
  180. goog.vec.vec2f.setFromValues(v1, 0, 0);
  181. goog.vec.vec2f.direction(v0, v1, dirVec);
  182. assertElementsRoughlyEqual(
  183. [-0.707106781, -0.707106781],
  184. dirVec, goog.vec.EPSILON);
  185. }
  186. function testLerp() {
  187. var v0 = goog.vec.vec2f.setFromValues(goog.vec.vec2f.create(), 1, 2);
  188. var v1 = goog.vec.vec2f.setFromValues(goog.vec.vec2f.create(), 10, 20);
  189. var v2 = goog.vec.vec2f.setFromVec2f(goog.vec.vec2f.create(), v0);
  190. goog.vec.vec2f.lerp(v2, v1, 0, v2);
  191. assertElementsEquals([1, 2], v2);
  192. goog.vec.vec2f.lerp(v2, v1, 1, v2);
  193. assertElementsEquals([10, 20], v2);
  194. goog.vec.vec2f.lerp(v0, v1, .5, v2);
  195. assertElementsEquals([5.5, 11], v2);
  196. }
  197. function testMax() {
  198. var v0 = goog.vec.vec2f.setFromValues(goog.vec.vec2f.create(), 10, 20);
  199. var v1 = goog.vec.vec2f.setFromValues(goog.vec.vec2f.create(), 5, 25);
  200. var v2 = goog.vec.vec2f.create();
  201. goog.vec.vec2f.max(v0, v1, v2);
  202. assertElementsEquals([10, 25], v2);
  203. goog.vec.vec2f.max(v1, v0, v1);
  204. assertElementsEquals([10, 25], v1);
  205. goog.vec.vec2f.max(v2, 20, v2);
  206. assertElementsEquals([20, 25], v2);
  207. }
  208. function testMin() {
  209. var v0 = goog.vec.vec2f.setFromValues(goog.vec.vec2f.create(), 10, 20);
  210. var v1 = goog.vec.vec2f.setFromValues(goog.vec.vec2f.create(), 5, 25);
  211. var v2 = goog.vec.vec2f.create();
  212. goog.vec.vec2f.min(v0, v1, v2);
  213. assertElementsEquals([5, 20], v2);
  214. goog.vec.vec2f.min(v1, v0, v1);
  215. assertElementsEquals([5, 20], v1);
  216. goog.vec.vec2f.min(v2, 10, v2);
  217. assertElementsEquals([5, 10], v2);
  218. }
  219. function testEquals() {
  220. var v0 = goog.vec.vec2f.setFromValues(goog.vec.vec2f.create(), 1, 2);
  221. var v1 = goog.vec.vec2f.setFromVec2f(goog.vec.vec2f.create(), v0);
  222. assertElementsEquals(v0, v1);
  223. v1[0] = 4;
  224. assertFalse(goog.vec.vec2f.equals(v0, v1));
  225. v1 = goog.vec.vec2f.setFromVec2f(goog.vec.vec2f.create(), v0);
  226. v1[1] = 4;
  227. assertFalse(goog.vec.vec2f.equals(v0, v1));
  228. }