Point.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. topSuite("Ext.draw.Point", function() {
  2. var proto = Ext.draw.Point.prototype,
  3. precision = 12; // first 12 decimal points should match
  4. it('should be isPoint', function() {
  5. expect(proto.isPoint).toBeTruthy();
  6. });
  7. it('should default to using degrees', function () {
  8. expect(proto.angleUnits).toBe('degrees');
  9. });
  10. describe('constructor', function () {
  11. it('should take two numbers', function () {
  12. var p = new Ext.draw.Point(3, 4);
  13. expect(p.x).toEqual(3);
  14. expect(p.y).toEqual(4);
  15. });
  16. it('should take a single number', function () {
  17. var p = new Ext.draw.Point(3);
  18. expect(p.x).toEqual(3);
  19. expect(p.y).toEqual(3);
  20. });
  21. it('should take an array', function () {
  22. var p = new Ext.draw.Point([3, 4]);
  23. expect(p.x).toEqual(3);
  24. expect(p.y).toEqual(4);
  25. });
  26. it('should take an object', function () {
  27. var p = new Ext.draw.Point({
  28. x: 3,
  29. y: 4
  30. });
  31. expect(p.x).toEqual(3);
  32. expect(p.y).toEqual(4);
  33. });
  34. it('should take a point', function () {
  35. var p = new Ext.draw.Point(new Ext.draw.Point(3, 4));
  36. expect(p.x).toEqual(3);
  37. expect(p.y).toEqual(4);
  38. });
  39. it('should calculate polar coordinates', function () {
  40. var p = new Ext.draw.Point(5, 5);
  41. expect(p.length).toEqual(Math.sqrt(2 * 5 * 5));
  42. expect(p.angle).toEqual(45);
  43. });
  44. });
  45. describe('set, setX, setY', function () {
  46. it('should recalculate polar coordinates', function () {
  47. var p = new Ext.draw.Point(3, 4);
  48. p.setX(0);
  49. expect(p.length).toEqual(4);
  50. expect(p.angle).toEqual(90);
  51. p.setY(0);
  52. expect(p.length).toEqual(0);
  53. expect(p.angle).toEqual(0);
  54. p.set(5, 5);
  55. expect(p.length).toEqual(Math.sqrt(2 * 5 * 5));
  56. expect(p.angle).toEqual(45);
  57. });
  58. });
  59. describe('setPolar, setLength, setAngle', function () {
  60. it('should recalculate cartesian coordinates', function () {
  61. var p = new Ext.draw.Point();
  62. p.setLength(10);
  63. expect(p.x).toEqual(10);
  64. expect(p.y).toEqual(0);
  65. p.setAngle(90);
  66. expect(p.x).toBeCloseTo(0, precision);
  67. expect(p.y).toBeCloseTo(10, precision);
  68. p.setPolar(45, Math.sqrt(2 * 5 * 5));
  69. expect(p.x).toBeCloseTo(5, precision);
  70. expect(p.y).toBeCloseTo(5, precision);
  71. });
  72. });
  73. describe('clone', function () {
  74. it('should match original point coordinates but not the point itself', function () {
  75. var p = new Ext.draw.Point(2, 3),
  76. clone = p.clone();
  77. expect(clone.x).toEqual(p.x);
  78. expect(clone.y).toEqual(p.y);
  79. expect(clone).not.toBe(p);
  80. });
  81. });
  82. describe('add', function () {
  83. it('should return a new point which x/y values are sums of respective ' +
  84. 'coordinates of this point and the given point', function () {
  85. var p1 = new Ext.draw.Point(2, 3),
  86. p2 = new Ext.draw.Point(-4, 5),
  87. p = p1.add(p2);
  88. expect(p.x).toEqual(-2);
  89. expect(p.y).toEqual(8);
  90. expect(p).not.toBe(p1);
  91. });
  92. });
  93. describe('sub', function () {
  94. it('should return a new point which x/y values are the difference between ' +
  95. 'the respective coordinates of this point (minuend) ' +
  96. 'and the given point (subtrahend)', function () {
  97. var p1 = new Ext.draw.Point(2, 3),
  98. p2 = new Ext.draw.Point(-4, 5),
  99. p = p1.sub(p2);
  100. expect(p.x).toEqual(6);
  101. expect(p.y).toEqual(-2);
  102. expect(p).not.toBe(p1);
  103. });
  104. });
  105. describe('mul', function () {
  106. it('should return a new point which x/y values are the product of multiplication of ' +
  107. 'coordinates of this point by a specified value', function () {
  108. var p = new Ext.draw.Point(2, 3),
  109. mp = p.mul(3);
  110. expect(mp.x).toEqual(6);
  111. expect(mp.y).toEqual(9);
  112. expect(mp).not.toBe(p);
  113. });
  114. });
  115. describe('div', function () {
  116. it('should return a new point which x/y values are the product of division of ' +
  117. 'coordinates of this point by a specified value', function () {
  118. var p = new Ext.draw.Point(2, 3),
  119. dp = p.div(2);
  120. expect(dp.x).toEqual(1);
  121. expect(dp.y).toEqual(1.5);
  122. expect(dp).not.toBe(p);
  123. });
  124. });
  125. describe('dot', function () {
  126. it('should return a dot product (scalar) of two vectors', function () {
  127. var p = new Ext.draw.Point(2, 0),
  128. op = new Ext.draw.Point(0, 3), // vector orthogonal to p
  129. p1 = new Ext.draw.Point(3, 4),
  130. dot_p_op = p.dot(op),
  131. dot_p_p1 = p.dot(p1);
  132. expect(dot_p_op).toEqual(0);
  133. expect(dot_p_p1).toEqual(6);
  134. expect(dot_p_p1).not.toBe(p);
  135. });
  136. });
  137. describe('equals', function () {
  138. it('should check if the respective coordinates of this point ' +
  139. 'and provided point are equal', function () {
  140. var p1 = new Ext.draw.Point(2, 0),
  141. p2 = new Ext.draw.Point({x: 2, y: 0}),
  142. isEqual = p1.equals(p2);
  143. expect(isEqual).toBe(true);
  144. })
  145. });
  146. describe('rotate', function () {
  147. it('should rotate the point (around origin and an arbitrary point) ' +
  148. 'by a specified angle', function () {
  149. var p = new Ext.draw.Point(1, 0),
  150. center = new Ext.draw.Point(0, 1),
  151. degrees = 45,
  152. rads = 45 / 180 * Math.PI,
  153. rp = p.rotate(degrees),
  154. rcp = p.rotate(degrees, center);
  155. expect(rp.x).toEqual(Math.cos(rads));
  156. expect(rp.y).toEqual(Math.sin(rads));
  157. expect(rcp.x).toBeCloseTo(Math.sqrt(2), precision);
  158. expect(rcp.y).toBeCloseTo(1, precision);
  159. });
  160. });
  161. describe('transform', function () {
  162. it('should transform a point from one coordinate system to another ' +
  163. 'given a transformation matrix or its elements', function () {
  164. var p = new Ext.draw.Point(2, 0),
  165. matrix = new Ext.draw.Matrix(),
  166. tp;
  167. matrix.translate(1, 1);
  168. matrix.rotate(Math.PI / 2);
  169. matrix.scale(2);
  170. tp = p.transform(matrix);
  171. expect(tp.x).toBeCloseTo(1, precision);
  172. expect(tp.y).toBeCloseTo(5, precision);
  173. tp = p.transform.apply(p, matrix.elements);
  174. expect(tp.x).toBeCloseTo(1, precision);
  175. expect(tp.y).toBeCloseTo(5, precision);
  176. });
  177. });
  178. describe('normalize', function () {
  179. it('should return a new vector with the length of 1 and the same angle', function () {
  180. var p = new Ext.draw.Point(5, 5),
  181. sin = Math.sin(Math.PI / 4),
  182. cos = Math.cos(Math.PI / 4),
  183. np = p.normalize(),
  184. np5 = p.normalize(5);
  185. expect(np.x).toBeCloseTo(cos, precision);
  186. expect(np.y).toBeCloseTo(sin, precision);
  187. expect(np.length).toBeCloseTo(1, precision);
  188. expect(np.angle).toBeCloseTo(45, precision);
  189. expect(np5.x).toBeCloseTo(5 * cos, precision);
  190. expect(np5.y).toBeCloseTo(5 * sin, precision);
  191. expect(np5.length).toBeCloseTo(5, precision);
  192. expect(np5.angle).toBeCloseTo(45, precision);
  193. p = new Ext.draw.Point(-5, -5);
  194. np = p.normalize();
  195. np5 = p.normalize(5);
  196. sin = Math.sin(-3 * Math.PI / 4);
  197. cos = Math.cos(-3 * Math.PI / 4);
  198. expect(np.x).toBeCloseTo(cos, precision);
  199. expect(np.y).toBeCloseTo(sin, precision);
  200. expect(np.length).toBeCloseTo(1, precision);
  201. expect(np.angle).toBeCloseTo(-135, precision);
  202. expect(np5.x).toBeCloseTo(5 * cos, precision);
  203. expect(np5.y).toBeCloseTo(5 * sin, precision);
  204. expect(np5.length).toBeCloseTo(5, precision);
  205. expect(np5.angle).toBeCloseTo(-135, precision);
  206. p = new Ext.draw.Point(5, -5);
  207. np = p.normalize();
  208. np5 = p.normalize(5);
  209. sin = Math.sin(-Math.PI / 4);
  210. cos = Math.cos(-Math.PI / 4);
  211. expect(np.x).toBeCloseTo(cos, precision);
  212. expect(np.y).toBeCloseTo(sin, precision);
  213. expect(np.length).toBeCloseTo(1, precision);
  214. expect(np.angle).toBeCloseTo(-45, precision);
  215. expect(np5.x).toBeCloseTo(5 * cos, precision);
  216. expect(np5.y).toBeCloseTo(5 * sin, precision);
  217. expect(np5.length).toBeCloseTo(5, precision);
  218. expect(np5.angle).toBeCloseTo(-45, precision);
  219. p = new Ext.draw.Point(-5, 5);
  220. np = p.normalize();
  221. np5 = p.normalize(5);
  222. sin = Math.sin(3 * Math.PI / 4);
  223. cos = Math.cos(3 * Math.PI / 4);
  224. expect(np.x).toBeCloseTo(cos, precision);
  225. expect(np.y).toBeCloseTo(sin, precision);
  226. expect(np.length).toBeCloseTo(1, precision);
  227. expect(np.angle).toBeCloseTo(135, precision);
  228. expect(np5.x).toBeCloseTo(5 * cos, precision);
  229. expect(np5.y).toBeCloseTo(5 * sin, precision);
  230. expect(np5.length).toBeCloseTo(5, precision);
  231. expect(np5.angle).toBeCloseTo(135, precision);
  232. });
  233. });
  234. describe('getDistanceToLine', function () {
  235. it('should return a distance from the point to the line (as a vector)', function () {
  236. var p = new Ext.draw.Point(1, 1),
  237. p1 = new Ext.draw.Point(1, 2),
  238. p2 = new Ext.draw.Point(2, 1),
  239. d1 = p.getDistanceToLine(p1, p2),
  240. d2 = p.getDistanceToLine(1, 2, 2, 1),
  241. d = Math.sqrt(2) / 2;
  242. expect(d1.length).toBeCloseTo(d, precision);
  243. expect(d2.length).toBeCloseTo(d, precision);
  244. });
  245. });
  246. });