affinetransform_test.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. // Copyright 2008 The Closure Library Authors. All Rights Reserved.
  2. // Use of this source code is governed by the Apache License, Version 2.0.
  3. goog.provide('goog.graphics.AffineTransformTest');
  4. goog.setTestOnly('goog.graphics.AffineTransformTest');
  5. goog.require('goog.graphics');
  6. goog.require('goog.graphics.AffineTransform');
  7. goog.require('goog.testing.jsunit');
  8. function testGetTranslateInstance() {
  9. var tx = goog.graphics.AffineTransform.getTranslateInstance(2, 4);
  10. assertEquals(1, tx.getScaleX());
  11. assertEquals(0, tx.getShearY());
  12. assertEquals(0, tx.getShearX());
  13. assertEquals(1, tx.getScaleY());
  14. assertEquals(2, tx.getTranslateX());
  15. assertEquals(4, tx.getTranslateY());
  16. }
  17. function testGetScaleInstance() {
  18. var tx = goog.graphics.AffineTransform.getScaleInstance(2, 4);
  19. assertEquals(2, tx.getScaleX());
  20. assertEquals(0, tx.getShearY());
  21. assertEquals(0, tx.getShearX());
  22. assertEquals(4, tx.getScaleY());
  23. assertEquals(0, tx.getTranslateX());
  24. assertEquals(0, tx.getTranslateY());
  25. }
  26. function testGetRotateInstance() {
  27. var tx = goog.graphics.AffineTransform.getRotateInstance(Math.PI / 2, 1, 2);
  28. assertRoughlyEquals(0, tx.getScaleX(), 1e-9);
  29. assertRoughlyEquals(1, tx.getShearY(), 1e-9);
  30. assertRoughlyEquals(-1, tx.getShearX(), 1e-9);
  31. assertRoughlyEquals(0, tx.getScaleY(), 1e-9);
  32. assertRoughlyEquals(3, tx.getTranslateX(), 1e-9);
  33. assertRoughlyEquals(1, tx.getTranslateY(), 1e-9);
  34. }
  35. function testGetShearInstance() {
  36. var tx = goog.graphics.AffineTransform.getShearInstance(2, 4);
  37. assertEquals(1, tx.getScaleX());
  38. assertEquals(4, tx.getShearY());
  39. assertEquals(2, tx.getShearX());
  40. assertEquals(1, tx.getScaleY());
  41. assertEquals(0, tx.getTranslateX());
  42. assertEquals(0, tx.getTranslateY());
  43. }
  44. function testConstructor() {
  45. assertThrows(function() {
  46. new goog.graphics.AffineTransform([0, 0]);
  47. });
  48. assertThrows(function() {
  49. new goog.graphics.AffineTransform({});
  50. });
  51. assertThrows(function() {
  52. new goog.graphics.AffineTransform(0, 0, 0, 'a', 0, 0);
  53. });
  54. var tx = new goog.graphics.AffineTransform(1, 2, 3, 4, 5, 6);
  55. assertEquals(1, tx.getScaleX());
  56. assertEquals(2, tx.getShearY());
  57. assertEquals(3, tx.getShearX());
  58. assertEquals(4, tx.getScaleY());
  59. assertEquals(5, tx.getTranslateX());
  60. assertEquals(6, tx.getTranslateY());
  61. tx = new goog.graphics.AffineTransform();
  62. assert(tx.isIdentity());
  63. }
  64. function testIsIdentity() {
  65. var tx = new goog.graphics.AffineTransform(1, 2, 3, 4, 5, 6);
  66. assertFalse(tx.isIdentity());
  67. tx.setTransform(1, 0, 0, 1, 0, 0);
  68. assert(tx.isIdentity());
  69. }
  70. function testClone() {
  71. var tx = new goog.graphics.AffineTransform(1, 2, 3, 4, 5, 6);
  72. var copy = tx.clone();
  73. assertEquals(copy.getScaleX(), tx.getScaleX());
  74. assertEquals(copy.getShearY(), tx.getShearY());
  75. assertEquals(copy.getShearX(), tx.getShearX());
  76. assertEquals(copy.getScaleY(), tx.getScaleY());
  77. assertEquals(copy.getTranslateX(), tx.getTranslateX());
  78. assertEquals(copy.getTranslateY(), tx.getTranslateY());
  79. }
  80. function testSetTransform() {
  81. var tx = new goog.graphics.AffineTransform();
  82. assertThrows(function() {
  83. tx.setTransform(1, 2, 3, 4, 6);
  84. });
  85. assertThrows(function() {
  86. tx.setTransform('a', 2, 3, 4, 5, 6);
  87. });
  88. tx.setTransform(1, 2, 3, 4, 5, 6);
  89. assertEquals(1, tx.getScaleX());
  90. assertEquals(2, tx.getShearY());
  91. assertEquals(3, tx.getShearX());
  92. assertEquals(4, tx.getScaleY());
  93. assertEquals(5, tx.getTranslateX());
  94. assertEquals(6, tx.getTranslateY());
  95. }
  96. function testScale() {
  97. var tx = new goog.graphics.AffineTransform(1, 2, 3, 4, 5, 6);
  98. tx.scale(2, 3);
  99. assertEquals(2, tx.getScaleX());
  100. assertEquals(4, tx.getShearY());
  101. assertEquals(9, tx.getShearX());
  102. assertEquals(12, tx.getScaleY());
  103. assertEquals(5, tx.getTranslateX());
  104. assertEquals(6, tx.getTranslateY());
  105. }
  106. function testPreScale() {
  107. var tx = new goog.graphics.AffineTransform(1, 2, 3, 4, 5, 6);
  108. tx.preScale(2, 3);
  109. assertEquals(2, tx.getScaleX());
  110. assertEquals(6, tx.getShearY());
  111. assertEquals(6, tx.getShearX());
  112. assertEquals(12, tx.getScaleY());
  113. assertEquals(10, tx.getTranslateX());
  114. assertEquals(18, tx.getTranslateY());
  115. }
  116. function testTranslate() {
  117. var tx = new goog.graphics.AffineTransform(1, 2, 3, 4, 5, 6);
  118. tx.translate(2, 3);
  119. assertEquals(1, tx.getScaleX());
  120. assertEquals(2, tx.getShearY());
  121. assertEquals(3, tx.getShearX());
  122. assertEquals(4, tx.getScaleY());
  123. assertEquals(16, tx.getTranslateX());
  124. assertEquals(22, tx.getTranslateY());
  125. }
  126. function testPreTranslate() {
  127. var tx = new goog.graphics.AffineTransform(1, 2, 3, 4, 5, 6);
  128. tx.preTranslate(2, 3);
  129. assertEquals(1, tx.getScaleX());
  130. assertEquals(2, tx.getShearY());
  131. assertEquals(3, tx.getShearX());
  132. assertEquals(4, tx.getScaleY());
  133. assertEquals(7, tx.getTranslateX());
  134. assertEquals(9, tx.getTranslateY());
  135. }
  136. function testRotate() {
  137. var tx = new goog.graphics.AffineTransform(1, 2, 3, 4, 5, 6);
  138. tx.rotate(Math.PI / 2, 1, 1);
  139. assertRoughlyEquals(3, tx.getScaleX(), 1e-9);
  140. assertRoughlyEquals(4, tx.getShearY(), 1e-9);
  141. assertRoughlyEquals(-1, tx.getShearX(), 1e-9);
  142. assertRoughlyEquals(-2, tx.getScaleY(), 1e-9);
  143. assertRoughlyEquals(7, tx.getTranslateX(), 1e-9);
  144. assertRoughlyEquals(10, tx.getTranslateY(), 1e-9);
  145. }
  146. function testPreRotate() {
  147. var tx = new goog.graphics.AffineTransform(1, 2, 3, 4, 5, 6);
  148. tx.preRotate(Math.PI / 2, 1, 1);
  149. assertRoughlyEquals(-2, tx.getScaleX(), 1e-9);
  150. assertRoughlyEquals(1, tx.getShearY(), 1e-9);
  151. assertRoughlyEquals(-4, tx.getShearX(), 1e-9);
  152. assertRoughlyEquals(3, tx.getScaleY(), 1e-9);
  153. assertRoughlyEquals(-4, tx.getTranslateX(), 1e-9);
  154. assertRoughlyEquals(5, tx.getTranslateY(), 1e-9);
  155. }
  156. function testShear() {
  157. var tx = new goog.graphics.AffineTransform(1, 2, 3, 4, 5, 6);
  158. tx.shear(2, 3);
  159. assertEquals(10, tx.getScaleX());
  160. assertEquals(14, tx.getShearY());
  161. assertEquals(5, tx.getShearX());
  162. assertEquals(8, tx.getScaleY());
  163. assertEquals(5, tx.getTranslateX());
  164. assertEquals(6, tx.getTranslateY());
  165. }
  166. function testPreShear() {
  167. var tx = new goog.graphics.AffineTransform(1, 2, 3, 4, 5, 6);
  168. tx.preShear(2, 3);
  169. assertEquals(5, tx.getScaleX());
  170. assertEquals(5, tx.getShearY());
  171. assertEquals(11, tx.getShearX());
  172. assertEquals(13, tx.getScaleY());
  173. assertEquals(17, tx.getTranslateX());
  174. assertEquals(21, tx.getTranslateY());
  175. }
  176. function testConcatentate() {
  177. var tx = new goog.graphics.AffineTransform(1, 2, 3, 4, 5, 6);
  178. tx.concatenate(new goog.graphics.AffineTransform(2, 1, 6, 5, 4, 3));
  179. assertEquals(5, tx.getScaleX());
  180. assertEquals(8, tx.getShearY());
  181. assertEquals(21, tx.getShearX());
  182. assertEquals(32, tx.getScaleY());
  183. assertEquals(18, tx.getTranslateX());
  184. assertEquals(26, tx.getTranslateY());
  185. }
  186. function testPreConcatentate() {
  187. var tx = new goog.graphics.AffineTransform(1, 2, 3, 4, 5, 6);
  188. tx.preConcatenate(new goog.graphics.AffineTransform(2, 1, 6, 5, 4, 3));
  189. assertEquals(14, tx.getScaleX());
  190. assertEquals(11, tx.getShearY());
  191. assertEquals(30, tx.getShearX());
  192. assertEquals(23, tx.getScaleY());
  193. assertEquals(50, tx.getTranslateX());
  194. assertEquals(38, tx.getTranslateY());
  195. }
  196. function testAssociativeConcatenate() {
  197. var x = new goog.graphics.AffineTransform(2, 3, 5, 7, 11, 13).concatenate(
  198. new goog.graphics.AffineTransform(17, 19, 23, 29, 31, 37));
  199. var y = new goog.graphics.AffineTransform(17, 19, 23, 29, 31, 37)
  200. .preConcatenate(new goog.graphics.AffineTransform(2, 3, 5, 7, 11, 13));
  201. assertEquals(x.getScaleX(), y.getScaleX());
  202. assertEquals(x.getShearY(), y.getShearY());
  203. assertEquals(x.getShearX(), y.getShearX());
  204. assertEquals(x.getScaleY(), y.getScaleY());
  205. assertEquals(x.getTranslateX(), y.getTranslateX());
  206. assertEquals(x.getTranslateY(), y.getTranslateY());
  207. };
  208. function testTransform() {
  209. var srcPts = [0, 0, 1, 0, 1, 1, 0, 1];
  210. var dstPts = [];
  211. var tx = goog.graphics.AffineTransform.getScaleInstance(2, 3);
  212. tx.translate(5, 10);
  213. tx.rotate(Math.PI / 4, 5, 10);
  214. tx.transform(srcPts, 0, dstPts, 0, 4);
  215. assert(goog.array.equals(
  216. [27.071068, 28.180195, 28.485281, 30.301516,
  217. 27.071068, 32.422836, 25.656855, 30.301516],
  218. dstPts,
  219. goog.math.nearlyEquals));
  220. }
  221. function testGetDeterminant() {
  222. var tx = goog.graphics.AffineTransform.getScaleInstance(2, 3);
  223. tx.translate(5, 10);
  224. tx.rotate(Math.PI / 4, 5, 10);
  225. assertRoughlyEquals(6, tx.getDeterminant(), 0.001);
  226. }
  227. function testIsInvertible() {
  228. assertTrue(new goog.graphics.AffineTransform(2, 3, 4, 5, 6, 7).
  229. isInvertible());
  230. assertTrue(new goog.graphics.AffineTransform(1, 0, 0, 1, 0, 0).
  231. isInvertible());
  232. assertFalse(new goog.graphics.AffineTransform(NaN, 0, 0, 1, 0, 0).
  233. isInvertible());
  234. assertFalse(new goog.graphics.AffineTransform(1, NaN, 0, 1, 0, 0).
  235. isInvertible());
  236. assertFalse(new goog.graphics.AffineTransform(1, 0, NaN, 1, 0, 0).
  237. isInvertible());
  238. assertFalse(new goog.graphics.AffineTransform(1, 0, 0, NaN, 0, 0).
  239. isInvertible());
  240. assertFalse(new goog.graphics.AffineTransform(1, 0, 0, 1, NaN, 0).
  241. isInvertible());
  242. assertFalse(new goog.graphics.AffineTransform(1, 0, 0, 1, 0, NaN).
  243. isInvertible());
  244. assertFalse(new goog.graphics.AffineTransform(Infinity, 0, 0, 1, 0, 0).
  245. isInvertible());
  246. assertFalse(new goog.graphics.AffineTransform(1, Infinity, 0, 1, 0, 0).
  247. isInvertible());
  248. assertFalse(new goog.graphics.AffineTransform(1, 0, Infinity, 1, 0, 0).
  249. isInvertible());
  250. assertFalse(new goog.graphics.AffineTransform(1, 0, 0, Infinity, 0, 0).
  251. isInvertible());
  252. assertFalse(new goog.graphics.AffineTransform(1, 0, 0, 1, Infinity, 0).
  253. isInvertible());
  254. assertFalse(new goog.graphics.AffineTransform(1, 0, 0, 1, 0, Infinity).
  255. isInvertible());
  256. assertFalse(new goog.graphics.AffineTransform(0, 0, 0, 0, 1, 0).
  257. isInvertible());
  258. }
  259. function testCreateInverse() {
  260. var tx = goog.graphics.AffineTransform.getScaleInstance(2, 3);
  261. tx.translate(5, 10);
  262. tx.rotate(Math.PI / 4, 5, 10);
  263. var inverse = tx.createInverse();
  264. assert(goog.math.nearlyEquals(0.353553, inverse.getScaleX()));
  265. assert(goog.math.nearlyEquals(-0.353553, inverse.getShearY()));
  266. assert(goog.math.nearlyEquals(0.235702, inverse.getShearX()));
  267. assert(goog.math.nearlyEquals(0.235702, inverse.getScaleY()));
  268. assert(goog.math.nearlyEquals(-16.213203, inverse.getTranslateX()));
  269. assert(goog.math.nearlyEquals(2.928932, inverse.getTranslateY()));
  270. }
  271. function testCopyFrom() {
  272. var from = new goog.graphics.AffineTransform(1, 2, 3, 4, 5, 6);
  273. var to = new goog.graphics.AffineTransform();
  274. to.copyFrom(from);
  275. assertEquals(from.getScaleX(), to.getScaleX());
  276. assertEquals(from.getShearY(), to.getShearY());
  277. assertEquals(from.getShearX(), to.getShearX());
  278. assertEquals(from.getScaleY(), to.getScaleY());
  279. assertEquals(from.getTranslateX(), to.getTranslateX());
  280. assertEquals(from.getTranslateY(), to.getTranslateY());
  281. }
  282. function testToString() {
  283. var tx = new goog.graphics.AffineTransform(1, 2, 3, 4, 5, 6);
  284. assertEquals("matrix(1,2,3,4,5,6)", tx.toString());
  285. }
  286. function testEquals() {
  287. var tx1 = new goog.graphics.AffineTransform(1, 2, 3, 4, 5, 6);
  288. var tx2 = new goog.graphics.AffineTransform(1, 2, 3, 4, 5, 6);
  289. assertEqualsMethod(tx1, tx2, true);
  290. tx2 = new goog.graphics.AffineTransform(-1, 2, 3, 4, 5, 6);
  291. assertEqualsMethod(tx1, tx2, false);
  292. tx2 = new goog.graphics.AffineTransform(1, -1, 3, 4, 5, 6);
  293. assertEqualsMethod(tx1, tx2, false);
  294. tx2 = new goog.graphics.AffineTransform(1, 2, -3, 4, 5, 6);
  295. assertEqualsMethod(tx1, tx2, false);
  296. tx2 = new goog.graphics.AffineTransform(1, 2, 3, -4, 5, 6);
  297. assertEqualsMethod(tx1, tx2, false);
  298. tx2 = new goog.graphics.AffineTransform(1, 2, 3, 4, -5, 6);
  299. assertEqualsMethod(tx1, tx2, false);
  300. tx2 = new goog.graphics.AffineTransform(1, 2, 3, 4, 5, -6);
  301. assertEqualsMethod(tx1, tx2, false);
  302. }
  303. function assertEqualsMethod(tx1, tx2, expected) {
  304. assertEquals(expected, tx1.equals(tx2));
  305. assertEquals(expected, tx2.equals(tx1));
  306. assertEquals(true, tx1.equals(tx1));
  307. assertEquals(true, tx2.equals(tx2));
  308. }