vec2d_test.js 9.3 KB

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