Vector2.tests.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  1. /* global QUnit */
  2. import { Vector2 } from '../../../../src/math/Vector2.js';
  3. import { Matrix3 } from '../../../../src/math/Matrix3.js';
  4. import { BufferAttribute } from '../../../../src/core/BufferAttribute.js';
  5. import {
  6. x,
  7. y,
  8. eps
  9. } from '../../utils/math-constants.js';
  10. export default QUnit.module( 'Maths', () => {
  11. QUnit.module( 'Vector2', () => {
  12. // INSTANCING
  13. QUnit.test( 'Instancing', ( assert ) => {
  14. let a = new Vector2();
  15. assert.ok( a.x == 0, 'Passed!' );
  16. assert.ok( a.y == 0, 'Passed!' );
  17. a = new Vector2( x, y );
  18. assert.ok( a.x === x, 'Passed!' );
  19. assert.ok( a.y === y, 'Passed!' );
  20. } );
  21. // PROPERTIES // ( [Itee] WHAT ??? o_O )
  22. QUnit.test( 'properties', ( assert ) => {
  23. const a = new Vector2( 0, 0 );
  24. const width = 100;
  25. const height = 200;
  26. assert.ok( a.width = width, 'Set width' );
  27. assert.ok( a.height = height, 'Set height' );
  28. a.set( width, height );
  29. assert.strictEqual( a.width, width, 'Get width' );
  30. assert.strictEqual( a.height, height, 'Get height' );
  31. } );
  32. QUnit.todo( 'width', ( assert ) => {
  33. assert.ok( false, 'everything\'s gonna be alright' );
  34. } );
  35. QUnit.todo( 'height', ( assert ) => {
  36. assert.ok( false, 'everything\'s gonna be alright' );
  37. } );
  38. // PUBLIC STUFF
  39. QUnit.test( 'isVector2', ( assert ) => {
  40. const object = new Vector2();
  41. assert.ok( object.isVector2, 'Vector2.isVector2 should be true' );
  42. } );
  43. QUnit.test( 'set', ( assert ) => {
  44. const a = new Vector2();
  45. assert.ok( a.x == 0, 'Passed!' );
  46. assert.ok( a.y == 0, 'Passed!' );
  47. a.set( x, y );
  48. assert.ok( a.x == x, 'Passed!' );
  49. assert.ok( a.y == y, 'Passed!' );
  50. } );
  51. QUnit.todo( 'setScalar', ( assert ) => {
  52. assert.ok( false, 'everything\'s gonna be alright' );
  53. } );
  54. QUnit.todo( 'setX', ( assert ) => {
  55. assert.ok( false, 'everything\'s gonna be alright' );
  56. } );
  57. QUnit.todo( 'setY', ( assert ) => {
  58. assert.ok( false, 'everything\'s gonna be alright' );
  59. } );
  60. QUnit.todo( 'setComponent', ( assert ) => {
  61. assert.ok( false, 'everything\'s gonna be alright' );
  62. } );
  63. QUnit.todo( 'getComponent', ( assert ) => {
  64. assert.ok( false, 'everything\'s gonna be alright' );
  65. } );
  66. QUnit.todo( 'clone', ( assert ) => {
  67. assert.ok( false, 'everything\'s gonna be alright' );
  68. } );
  69. QUnit.test( 'copy', ( assert ) => {
  70. const a = new Vector2( x, y );
  71. const b = new Vector2().copy( a );
  72. assert.ok( b.x == x, 'Passed!' );
  73. assert.ok( b.y == y, 'Passed!' );
  74. // ensure that it is a true copy
  75. a.x = 0;
  76. a.y = - 1;
  77. assert.ok( b.x == x, 'Passed!' );
  78. assert.ok( b.y == y, 'Passed!' );
  79. } );
  80. QUnit.test( 'add', ( assert ) => {
  81. const a = new Vector2( x, y );
  82. const b = new Vector2( - x, - y );
  83. a.add( b );
  84. assert.ok( a.x == 0, 'Passed!' );
  85. assert.ok( a.y == 0, 'Passed!' );
  86. const c = new Vector2().addVectors( b, b );
  87. assert.ok( c.x == - 2 * x, 'Passed!' );
  88. assert.ok( c.y == - 2 * y, 'Passed!' );
  89. } );
  90. QUnit.todo( 'addScalar', ( assert ) => {
  91. assert.ok( false, 'everything\'s gonna be alright' );
  92. } );
  93. QUnit.todo( 'addVectors', ( assert ) => {
  94. assert.ok( false, 'everything\'s gonna be alright' );
  95. } );
  96. QUnit.test( 'addScaledVector', ( assert ) => {
  97. const a = new Vector2( x, y );
  98. const b = new Vector2( 2, 3 );
  99. const s = 3;
  100. a.addScaledVector( b, s );
  101. assert.strictEqual( a.x, x + b.x * s, 'Check x' );
  102. assert.strictEqual( a.y, y + b.y * s, 'Check y' );
  103. } );
  104. QUnit.test( 'sub', ( assert ) => {
  105. const a = new Vector2( x, y );
  106. const b = new Vector2( - x, - y );
  107. a.sub( b );
  108. assert.ok( a.x == 2 * x, 'Passed!' );
  109. assert.ok( a.y == 2 * y, 'Passed!' );
  110. const c = new Vector2().subVectors( a, a );
  111. assert.ok( c.x == 0, 'Passed!' );
  112. assert.ok( c.y == 0, 'Passed!' );
  113. } );
  114. QUnit.todo( 'subScalar', ( assert ) => {
  115. assert.ok( false, 'everything\'s gonna be alright' );
  116. } );
  117. QUnit.todo( 'subVectors', ( assert ) => {
  118. assert.ok( false, 'everything\'s gonna be alright' );
  119. } );
  120. QUnit.todo( 'multiply', ( assert ) => {
  121. assert.ok( false, 'everything\'s gonna be alright' );
  122. } );
  123. QUnit.todo( 'multiplyScalar', ( assert ) => {
  124. assert.ok( false, 'everything\'s gonna be alright' );
  125. } );
  126. QUnit.todo( 'divide', ( assert ) => {
  127. assert.ok( false, 'everything\'s gonna be alright' );
  128. } );
  129. QUnit.todo( 'divideScalar', ( assert ) => {
  130. assert.ok( false, 'everything\'s gonna be alright' );
  131. } );
  132. QUnit.test( 'applyMatrix3', ( assert ) => {
  133. const a = new Vector2( x, y );
  134. const m = new Matrix3().set( 2, 3, 5, 7, 11, 13, 17, 19, 23 );
  135. a.applyMatrix3( m );
  136. assert.strictEqual( a.x, 18, 'Check x' );
  137. assert.strictEqual( a.y, 60, 'Check y' );
  138. } );
  139. QUnit.todo( 'min', ( assert ) => {
  140. assert.ok( false, 'everything\'s gonna be alright' );
  141. } );
  142. QUnit.todo( 'max', ( assert ) => {
  143. assert.ok( false, 'everything\'s gonna be alright' );
  144. } );
  145. QUnit.todo( 'clamp', ( assert ) => {
  146. assert.ok( false, 'everything\'s gonna be alright' );
  147. } );
  148. QUnit.todo( 'clampScalar', ( assert ) => {
  149. assert.ok( false, 'everything\'s gonna be alright' );
  150. } );
  151. QUnit.todo( 'clampLength', ( assert ) => {
  152. assert.ok( false, 'everything\'s gonna be alright' );
  153. } );
  154. QUnit.todo( 'floor', ( assert ) => {
  155. assert.ok( false, 'everything\'s gonna be alright' );
  156. } );
  157. QUnit.todo( 'ceil', ( assert ) => {
  158. assert.ok( false, 'everything\'s gonna be alright' );
  159. } );
  160. QUnit.todo( 'round', ( assert ) => {
  161. assert.ok( false, 'everything\'s gonna be alright' );
  162. } );
  163. QUnit.todo( 'roundToZero', ( assert ) => {
  164. assert.ok( false, 'everything\'s gonna be alright' );
  165. } );
  166. QUnit.test( 'negate', ( assert ) => {
  167. const a = new Vector2( x, y );
  168. a.negate();
  169. assert.ok( a.x == - x, 'Passed!' );
  170. assert.ok( a.y == - y, 'Passed!' );
  171. } );
  172. QUnit.test( 'dot', ( assert ) => {
  173. const a = new Vector2( x, y );
  174. const b = new Vector2( - x, - y );
  175. const c = new Vector2();
  176. let result = a.dot( b );
  177. assert.ok( result == ( - x * x - y * y ), 'Passed!' );
  178. result = a.dot( c );
  179. assert.ok( result == 0, 'Passed!' );
  180. } );
  181. QUnit.test( 'cross', ( assert ) => {
  182. const a = new Vector2( x, y );
  183. const b = new Vector2( 2 * x, - y );
  184. const answer = - 18;
  185. const crossed = a.cross( b );
  186. assert.ok( Math.abs( answer - crossed ) <= eps, 'Check cross' );
  187. } );
  188. QUnit.todo( 'lengthSq', ( assert ) => {
  189. assert.ok( false, 'everything\'s gonna be alright' );
  190. } );
  191. QUnit.todo( 'length', ( assert ) => {
  192. assert.ok( false, 'everything\'s gonna be alright' );
  193. } );
  194. QUnit.test( 'manhattanLength', ( assert ) => {
  195. const a = new Vector2( x, 0 );
  196. const b = new Vector2( 0, - y );
  197. const c = new Vector2();
  198. assert.strictEqual( a.manhattanLength(), x, 'Positive component' );
  199. assert.strictEqual( b.manhattanLength(), y, 'Negative component' );
  200. assert.strictEqual( c.manhattanLength(), 0, 'Empty component' );
  201. a.set( x, y );
  202. assert.strictEqual( a.manhattanLength(), Math.abs( x ) + Math.abs( y ), 'Two components' );
  203. } );
  204. QUnit.test( 'normalize', ( assert ) => {
  205. const a = new Vector2( x, 0 );
  206. const b = new Vector2( 0, - y );
  207. a.normalize();
  208. assert.ok( a.length() == 1, 'Passed!' );
  209. assert.ok( a.x == 1, 'Passed!' );
  210. b.normalize();
  211. assert.ok( b.length() == 1, 'Passed!' );
  212. assert.ok( b.y == - 1, 'Passed!' );
  213. } );
  214. QUnit.todo( 'angle', ( assert ) => {
  215. assert.ok( false, 'everything\'s gonna be alright' );
  216. } );
  217. QUnit.test( 'angleTo', ( assert ) => {
  218. const a = new Vector2( - 0.18851655680720186, 0.9820700116639124 );
  219. const b = new Vector2( 0.18851655680720186, - 0.9820700116639124 );
  220. assert.equal( a.angleTo( a ), 0 );
  221. assert.equal( a.angleTo( b ), Math.PI );
  222. const x = new Vector2( 1, 0 );
  223. const y = new Vector2( 0, 1 );
  224. assert.equal( x.angleTo( y ), Math.PI / 2 );
  225. assert.equal( y.angleTo( x ), Math.PI / 2 );
  226. assert.ok( Math.abs( x.angleTo( new Vector2( 1, 1 ) ) - ( Math.PI / 4 ) ) < 0.0000001 );
  227. } );
  228. QUnit.todo( 'distanceTo', ( assert ) => {
  229. assert.ok( false, 'everything\'s gonna be alright' );
  230. } );
  231. QUnit.todo( 'distanceToSquared', ( assert ) => {
  232. assert.ok( false, 'everything\'s gonna be alright' );
  233. } );
  234. QUnit.todo( 'manhattanDistanceTo', ( assert ) => {
  235. assert.ok( false, 'everything\'s gonna be alright' );
  236. } );
  237. QUnit.test( 'setLength', ( assert ) => {
  238. let a = new Vector2( x, 0 );
  239. assert.ok( a.length() == x, 'Passed!' );
  240. a.setLength( y );
  241. assert.ok( a.length() == y, 'Passed!' );
  242. a = new Vector2( 0, 0 );
  243. assert.ok( a.length() == 0, 'Passed!' );
  244. a.setLength( y );
  245. assert.ok( a.length() == 0, 'Passed!' );
  246. a.setLength();
  247. assert.ok( isNaN( a.length() ), 'Passed!' );
  248. } );
  249. QUnit.todo( 'lerp', ( assert ) => {
  250. assert.ok( false, 'everything\'s gonna be alright' );
  251. } );
  252. QUnit.todo( 'lerpVectors', ( assert ) => {
  253. assert.ok( false, 'everything\'s gonna be alright' );
  254. } );
  255. QUnit.test( 'equals', ( assert ) => {
  256. const a = new Vector2( x, 0 );
  257. const b = new Vector2( 0, - y );
  258. assert.ok( a.x != b.x, 'Passed!' );
  259. assert.ok( a.y != b.y, 'Passed!' );
  260. assert.ok( ! a.equals( b ), 'Passed!' );
  261. assert.ok( ! b.equals( a ), 'Passed!' );
  262. a.copy( b );
  263. assert.ok( a.x == b.x, 'Passed!' );
  264. assert.ok( a.y == b.y, 'Passed!' );
  265. assert.ok( a.equals( b ), 'Passed!' );
  266. assert.ok( b.equals( a ), 'Passed!' );
  267. } );
  268. QUnit.test( 'fromArray', ( assert ) => {
  269. const a = new Vector2();
  270. const array = [ 1, 2, 3, 4 ];
  271. a.fromArray( array );
  272. assert.strictEqual( a.x, 1, 'No offset: check x' );
  273. assert.strictEqual( a.y, 2, 'No offset: check y' );
  274. a.fromArray( array, 2 );
  275. assert.strictEqual( a.x, 3, 'With offset: check x' );
  276. assert.strictEqual( a.y, 4, 'With offset: check y' );
  277. } );
  278. QUnit.test( 'toArray', ( assert ) => {
  279. const a = new Vector2( x, y );
  280. let array = a.toArray();
  281. assert.strictEqual( array[ 0 ], x, 'No array, no offset: check x' );
  282. assert.strictEqual( array[ 1 ], y, 'No array, no offset: check y' );
  283. array = [];
  284. a.toArray( array );
  285. assert.strictEqual( array[ 0 ], x, 'With array, no offset: check x' );
  286. assert.strictEqual( array[ 1 ], y, 'With array, no offset: check y' );
  287. array = [];
  288. a.toArray( array, 1 );
  289. assert.strictEqual( array[ 0 ], undefined, 'With array and offset: check [0]' );
  290. assert.strictEqual( array[ 1 ], x, 'With array and offset: check x' );
  291. assert.strictEqual( array[ 2 ], y, 'With array and offset: check y' );
  292. } );
  293. QUnit.test( 'fromBufferAttribute', ( assert ) => {
  294. const a = new Vector2();
  295. const attr = new BufferAttribute( new Float32Array( [ 1, 2, 3, 4 ] ), 2 );
  296. a.fromBufferAttribute( attr, 0 );
  297. assert.strictEqual( a.x, 1, 'Offset 0: check x' );
  298. assert.strictEqual( a.y, 2, 'Offset 0: check y' );
  299. a.fromBufferAttribute( attr, 1 );
  300. assert.strictEqual( a.x, 3, 'Offset 1: check x' );
  301. assert.strictEqual( a.y, 4, 'Offset 1: check y' );
  302. } );
  303. QUnit.todo( 'rotateAround', ( assert ) => {
  304. assert.ok( false, 'everything\'s gonna be alright' );
  305. } );
  306. // TODO (Itee) refactor/split
  307. QUnit.test( 'setX,setY', ( assert ) => {
  308. const a = new Vector2();
  309. assert.ok( a.x == 0, 'Passed!' );
  310. assert.ok( a.y == 0, 'Passed!' );
  311. a.setX( x );
  312. a.setY( y );
  313. assert.ok( a.x == x, 'Passed!' );
  314. assert.ok( a.y == y, 'Passed!' );
  315. } );
  316. QUnit.test( 'setComponent,getComponent', ( assert ) => {
  317. const a = new Vector2();
  318. assert.ok( a.x == 0, 'Passed!' );
  319. assert.ok( a.y == 0, 'Passed!' );
  320. a.setComponent( 0, 1 );
  321. a.setComponent( 1, 2 );
  322. assert.ok( a.getComponent( 0 ) == 1, 'Passed!' );
  323. assert.ok( a.getComponent( 1 ) == 2, 'Passed!' );
  324. } );
  325. QUnit.test( 'multiply/divide', ( assert ) => {
  326. const a = new Vector2( x, y );
  327. const b = new Vector2( - x, - y );
  328. a.multiplyScalar( - 2 );
  329. assert.ok( a.x == x * - 2, 'Passed!' );
  330. assert.ok( a.y == y * - 2, 'Passed!' );
  331. b.multiplyScalar( - 2 );
  332. assert.ok( b.x == 2 * x, 'Passed!' );
  333. assert.ok( b.y == 2 * y, 'Passed!' );
  334. a.divideScalar( - 2 );
  335. assert.ok( a.x == x, 'Passed!' );
  336. assert.ok( a.y == y, 'Passed!' );
  337. b.divideScalar( - 2 );
  338. assert.ok( b.x == - x, 'Passed!' );
  339. assert.ok( b.y == - y, 'Passed!' );
  340. } );
  341. QUnit.test( 'min/max/clamp', ( assert ) => {
  342. const a = new Vector2( x, y );
  343. const b = new Vector2( - x, - y );
  344. const c = new Vector2();
  345. c.copy( a ).min( b );
  346. assert.ok( c.x == - x, 'Passed!' );
  347. assert.ok( c.y == - y, 'Passed!' );
  348. c.copy( a ).max( b );
  349. assert.ok( c.x == x, 'Passed!' );
  350. assert.ok( c.y == y, 'Passed!' );
  351. c.set( - 2 * x, 2 * y );
  352. c.clamp( b, a );
  353. assert.ok( c.x == - x, 'Passed!' );
  354. assert.ok( c.y == y, 'Passed!' );
  355. c.set( - 2 * x, 2 * x );
  356. c.clampScalar( - x, x );
  357. assert.equal( c.x, - x, 'scalar clamp x' );
  358. assert.equal( c.y, x, 'scalar clamp y' );
  359. } );
  360. QUnit.test( 'rounding', ( assert ) => {
  361. assert.deepEqual( new Vector2( - 0.1, 0.1 ).floor(), new Vector2( - 1, 0 ), 'floor .1' );
  362. assert.deepEqual( new Vector2( - 0.5, 0.5 ).floor(), new Vector2( - 1, 0 ), 'floor .5' );
  363. assert.deepEqual( new Vector2( - 0.9, 0.9 ).floor(), new Vector2( - 1, 0 ), 'floor .9' );
  364. assert.deepEqual( new Vector2( - 0.1, 0.1 ).ceil(), new Vector2( 0, 1 ), 'ceil .1' );
  365. assert.deepEqual( new Vector2( - 0.5, 0.5 ).ceil(), new Vector2( 0, 1 ), 'ceil .5' );
  366. assert.deepEqual( new Vector2( - 0.9, 0.9 ).ceil(), new Vector2( 0, 1 ), 'ceil .9' );
  367. assert.deepEqual( new Vector2( - 0.1, 0.1 ).round(), new Vector2( 0, 0 ), 'round .1' );
  368. assert.deepEqual( new Vector2( - 0.5, 0.5 ).round(), new Vector2( 0, 1 ), 'round .5' );
  369. assert.deepEqual( new Vector2( - 0.9, 0.9 ).round(), new Vector2( - 1, 1 ), 'round .9' );
  370. assert.deepEqual( new Vector2( - 0.1, 0.1 ).roundToZero(), new Vector2( 0, 0 ), 'roundToZero .1' );
  371. assert.deepEqual( new Vector2( - 0.5, 0.5 ).roundToZero(), new Vector2( 0, 0 ), 'roundToZero .5' );
  372. assert.deepEqual( new Vector2( - 0.9, 0.9 ).roundToZero(), new Vector2( 0, 0 ), 'roundToZero .9' );
  373. assert.deepEqual( new Vector2( - 1.1, 1.1 ).roundToZero(), new Vector2( - 1, 1 ), 'roundToZero 1.1' );
  374. assert.deepEqual( new Vector2( - 1.5, 1.5 ).roundToZero(), new Vector2( - 1, 1 ), 'roundToZero 1.5' );
  375. assert.deepEqual( new Vector2( - 1.9, 1.9 ).roundToZero(), new Vector2( - 1, 1 ), 'roundToZero 1.9' );
  376. } );
  377. QUnit.test( 'length/lengthSq', ( assert ) => {
  378. const a = new Vector2( x, 0 );
  379. const b = new Vector2( 0, - y );
  380. const c = new Vector2();
  381. assert.ok( a.length() == x, 'Passed!' );
  382. assert.ok( a.lengthSq() == x * x, 'Passed!' );
  383. assert.ok( b.length() == y, 'Passed!' );
  384. assert.ok( b.lengthSq() == y * y, 'Passed!' );
  385. assert.ok( c.length() == 0, 'Passed!' );
  386. assert.ok( c.lengthSq() == 0, 'Passed!' );
  387. a.set( x, y );
  388. assert.ok( a.length() == Math.sqrt( x * x + y * y ), 'Passed!' );
  389. assert.ok( a.lengthSq() == ( x * x + y * y ), 'Passed!' );
  390. } );
  391. QUnit.test( 'distanceTo/distanceToSquared', ( assert ) => {
  392. const a = new Vector2( x, 0 );
  393. const b = new Vector2( 0, - y );
  394. const c = new Vector2();
  395. assert.ok( a.distanceTo( c ) == x, 'Passed!' );
  396. assert.ok( a.distanceToSquared( c ) == x * x, 'Passed!' );
  397. assert.ok( b.distanceTo( c ) == y, 'Passed!' );
  398. assert.ok( b.distanceToSquared( c ) == y * y, 'Passed!' );
  399. } );
  400. QUnit.test( 'lerp/clone', ( assert ) => {
  401. const a = new Vector2( x, 0 );
  402. const b = new Vector2( 0, - y );
  403. assert.ok( a.lerp( a, 0 ).equals( a.lerp( a, 0.5 ) ), 'Passed!' );
  404. assert.ok( a.lerp( a, 0 ).equals( a.lerp( a, 1 ) ), 'Passed!' );
  405. assert.ok( a.clone().lerp( b, 0 ).equals( a ), 'Passed!' );
  406. assert.ok( a.clone().lerp( b, 0.5 ).x == x * 0.5, 'Passed!' );
  407. assert.ok( a.clone().lerp( b, 0.5 ).y == - y * 0.5, 'Passed!' );
  408. assert.ok( a.clone().lerp( b, 1 ).equals( b ), 'Passed!' );
  409. } );
  410. QUnit.test( 'setComponent/getComponent exceptions', ( assert ) => {
  411. const a = new Vector2( 0, 0 );
  412. assert.throws(
  413. function () {
  414. a.setComponent( 2, 0 );
  415. },
  416. /index is out of range/,
  417. 'setComponent with an out of range index throws Error'
  418. );
  419. assert.throws(
  420. function () {
  421. a.getComponent( 2 );
  422. },
  423. /index is out of range/,
  424. 'getComponent with an out of range index throws Error'
  425. );
  426. } );
  427. QUnit.test( 'setScalar/addScalar/subScalar', ( assert ) => {
  428. const a = new Vector2( 1, 1 );
  429. const s = 3;
  430. a.setScalar( s );
  431. assert.strictEqual( a.x, s, 'setScalar: check x' );
  432. assert.strictEqual( a.y, s, 'setScalar: check y' );
  433. a.addScalar( s );
  434. assert.strictEqual( a.x, 2 * s, 'addScalar: check x' );
  435. assert.strictEqual( a.y, 2 * s, 'addScalar: check y' );
  436. a.subScalar( 2 * s );
  437. assert.strictEqual( a.x, 0, 'subScalar: check x' );
  438. assert.strictEqual( a.y, 0, 'subScalar: check y' );
  439. } );
  440. QUnit.test( 'multiply/divide', ( assert ) => {
  441. const a = new Vector2( x, y );
  442. const b = new Vector2( 2 * x, 2 * y );
  443. const c = new Vector2( 4 * x, 4 * y );
  444. a.multiply( b );
  445. assert.strictEqual( a.x, x * b.x, 'multiply: check x' );
  446. assert.strictEqual( a.y, y * b.y, 'multiply: check y' );
  447. b.divide( c );
  448. assert.strictEqual( b.x, 0.5, 'divide: check x' );
  449. assert.strictEqual( b.y, 0.5, 'divide: check y' );
  450. } );
  451. // OTHERS
  452. QUnit.test( 'iterable', ( assert ) => {
  453. const v = new Vector2( 0, 1 );
  454. const array = [ ...v ];
  455. assert.strictEqual( array[ 0 ], 0, 'Vector2 is iterable.' );
  456. assert.strictEqual( array[ 1 ], 1, 'Vector2 is iterable.' );
  457. } );
  458. } );
  459. } );