vec2f_test.js 8.2 KB

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