point.test.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const __1 = require("..");
  4. describe('Point', () => {
  5. describe('#constructor', () => {
  6. it('should create a point instance', () => {
  7. expect(new __1.Point()).toBeInstanceOf(__1.Point);
  8. expect(new __1.Point(1)).toBeInstanceOf(__1.Point);
  9. expect(new __1.Point(1, 2)).toBeInstanceOf(__1.Point);
  10. expect(new __1.Point(1, 2).x).toEqual(1);
  11. expect(new __1.Point(1, 2).y).toEqual(2);
  12. expect(new __1.Point().equals(new __1.Point(0, 0)));
  13. });
  14. });
  15. describe('#Point.random', () => {
  16. it('should create random point', () => {
  17. const p = __1.Point.random(1, 5, 2, 6);
  18. expect(p.x >= 1 && p.x <= 5).toBe(true);
  19. expect(p.y >= 2 && p.y <= 6).toBe(true);
  20. });
  21. });
  22. describe('#Point.isPoint', () => {
  23. it('should return true when the given object a point instance', () => {
  24. expect(__1.Point.isPoint(new __1.Point())).toBeTruthy();
  25. expect(__1.Point.isPoint(new __1.Point(1, 1))).toBeTruthy();
  26. });
  27. it('should return false when the given object is not a point instance', () => {
  28. expect(__1.Point.isPoint({ x: 1, y: 1 })).toBeFalsy();
  29. });
  30. });
  31. describe('#Point.isPointLike', () => {
  32. it('should return true when the given object is a point-like object', () => {
  33. expect(__1.Point.isPointLike({ x: 1, y: 2 })).toBeTruthy();
  34. expect(__1.Point.isPointLike({ x: 1, y: 2, z: 10 })).toBeTruthy();
  35. expect(__1.Point.isPointLike({ x: 1, y: 2, z: 10, s: 's' })).toBeTruthy();
  36. });
  37. it('should return false when the given object is a point-like object', () => {
  38. expect(__1.Point.isPointLike({ x: 1 })).toBeFalsy();
  39. expect(__1.Point.isPointLike({ y: 2 })).toBeFalsy();
  40. expect(__1.Point.isPointLike({})).toBeFalsy();
  41. expect(__1.Point.isPointLike(null)).toBeFalsy();
  42. expect(__1.Point.isPointLike(false)).toBeFalsy();
  43. expect(__1.Point.isPointLike(1)).toBeFalsy();
  44. expect(__1.Point.isPointLike('s')).toBeFalsy();
  45. });
  46. });
  47. describe('#Point.isPointData', () => {
  48. it('should return true when the given object is a point-data array', () => {
  49. expect(__1.Point.isPointData([1, 2])).toBeTruthy();
  50. });
  51. it('should return false when the given object is a point-data array', () => {
  52. expect(__1.Point.isPointData({ x: 1, y: 2 })).toBeFalsy();
  53. expect(__1.Point.isPointData([1])).toBeFalsy();
  54. expect(__1.Point.isPointData([1, 2, 3])).toBeFalsy();
  55. expect(__1.Point.isPointData(null)).toBeFalsy();
  56. expect(__1.Point.isPointData(false)).toBeFalsy();
  57. expect(__1.Point.isPointData(1)).toBeFalsy();
  58. expect(__1.Point.isPointData('s')).toBeFalsy();
  59. });
  60. });
  61. describe('#Point.toJSON', () => {
  62. it('should conver the given point to json', () => {
  63. expect(__1.Point.toJSON([1, 2])).toEqual({ x: 1, y: 2 });
  64. expect(__1.Point.toJSON({ x: 1, y: 2 })).toEqual({ x: 1, y: 2 });
  65. expect(__1.Point.toJSON(new __1.Point(1, 2))).toEqual({ x: 1, y: 2 });
  66. });
  67. });
  68. describe('#Point.equals', () => {
  69. it('should return true when the given two points are equal', () => {
  70. const p1 = new __1.Point(1, 2);
  71. expect(__1.Point.equals(p1, p1)).toBeTruthy();
  72. expect(__1.Point.equals(p1, { x: 1, y: 2 })).toBeTruthy();
  73. });
  74. it('should return false when the given two points are not equal', () => {
  75. const p1 = new __1.Point(1, 2);
  76. const p2 = new __1.Point(2, 2);
  77. expect(__1.Point.equals(p1, p2)).toBeFalsy();
  78. expect(__1.Point.equals(p1, null)).toBeFalsy();
  79. expect(__1.Point.equals(p1, { x: 2, y: 2 })).toBeFalsy();
  80. });
  81. });
  82. describe('#Point.equalPoints', () => {
  83. it('should return true when the given points array are equal', () => {
  84. const p1 = new __1.Point(1, 2);
  85. expect(__1.Point.equalPoints([p1], [p1])).toBeTruthy();
  86. expect(__1.Point.equalPoints([p1], [{ x: 1, y: 2 }])).toBeTruthy();
  87. });
  88. it('should return false when the given points array are not equal', () => {
  89. const p1 = new __1.Point(1, 2);
  90. const p2 = new __1.Point(2, 2);
  91. expect(__1.Point.equalPoints([p1], [p2])).toBeFalsy();
  92. expect(__1.Point.equalPoints(null, [p2])).toBeFalsy();
  93. expect(__1.Point.equalPoints([p1], null)).toBeFalsy();
  94. expect(__1.Point.equalPoints([p1, p2], [p2])).toBeFalsy();
  95. expect(__1.Point.equalPoints([p1, p2], [p2, p1])).toBeFalsy();
  96. expect(__1.Point.equalPoints([p1], [{ x: 2, y: 2 }])).toBeFalsy();
  97. });
  98. });
  99. describe('#round', () => {
  100. it('should round x and y properties to the given precision', () => {
  101. const point = new __1.Point(17.231, 4.01);
  102. point.round(2);
  103. expect(point.serialize()).toEqual('17.23 4.01');
  104. point.round(0);
  105. expect(point.serialize()).toEqual('17 4');
  106. });
  107. });
  108. describe('#add', () => {
  109. it('should add x and y width the given amount', () => {
  110. const source = new __1.Point(4, 17);
  111. const target = new __1.Point(20, 20);
  112. expect(source.clone().add(16, 3)).toEqual(target);
  113. expect(source.clone().add([16, 3])).toEqual(target);
  114. expect(source.clone().add({ x: 16, y: 3 })).toEqual(target);
  115. });
  116. });
  117. describe('#update', () => {
  118. it('should update the values of x and y', () => {
  119. const source = new __1.Point(4, 17);
  120. const target = new __1.Point(16, 24);
  121. expect(source.clone().update(16, 24)).toEqual(target);
  122. expect(source.clone().update([16, 24])).toEqual(target);
  123. expect(source.clone().update({ x: 16, y: 24 })).toEqual(target);
  124. expect(source.clone().update(target)).toEqual(target);
  125. });
  126. });
  127. describe('#translate', () => {
  128. it('should translate x and y by adding the given dx and dy values respectively', () => {
  129. const point = new __1.Point(0, 0);
  130. point.translate(2, 3);
  131. expect(point.toJSON()).toEqual({ x: 2, y: 3 });
  132. point.translate(new __1.Point(-2, 4));
  133. expect(point.toJSON()).toEqual({ x: 0, y: 7 });
  134. });
  135. });
  136. describe('#rotate', () => {
  137. const rotate = (p, angle, center) => p.clone().rotate(angle, center).round(3).serialize();
  138. it('should return a rotated version of self', () => {
  139. const point = new __1.Point(5, 5);
  140. let angle;
  141. const zeroPoint = new __1.Point(0, 0);
  142. const arbitraryPoint = new __1.Point(14, 6);
  143. angle = 0;
  144. expect(rotate(point, angle)).toEqual('5 5');
  145. expect(rotate(point, angle, zeroPoint)).toEqual('5 5');
  146. expect(rotate(point, angle, point)).toEqual('5 5');
  147. expect(rotate(point, angle, arbitraryPoint)).toEqual('5 5');
  148. angle = 154;
  149. expect(rotate(point, angle)).toEqual('-2.302 -6.686');
  150. expect(rotate(point, angle, zeroPoint)).toEqual('-2.302 -6.686');
  151. expect(rotate(point, angle, point)).toEqual('5 5');
  152. expect(rotate(point, angle, arbitraryPoint)).toEqual('21.651 10.844');
  153. });
  154. });
  155. describe('#scale', () => {
  156. it('should scale point with the given amount', () => {
  157. expect(new __1.Point(20, 30).scale(2, 3)).toEqual(new __1.Point(40, 90));
  158. });
  159. it('should scale point with the given amount and center ', () => {
  160. expect(new __1.Point(20, 30).scale(2, 3, new __1.Point(40, 45))).toEqual(new __1.Point(0, 0));
  161. });
  162. });
  163. describe('#closest', () => {
  164. it('should return the closest point', () => {
  165. const a = new __1.Point(10, 10);
  166. const b = { x: 20, y: 20 };
  167. const c = { x: 30, y: 30 };
  168. expect(a.closest([])).toBeNull();
  169. expect(a.closest([b])).toBeInstanceOf(__1.Point);
  170. expect(a.closest([b]).toJSON()).toEqual(b);
  171. expect(a.closest([b, c]).toJSON()).toEqual(b);
  172. expect(a.closest([b, c]).toJSON()).toEqual(b);
  173. });
  174. });
  175. describe('#distance', () => {
  176. it('should return the distance between me and the given point', () => {
  177. const source = new __1.Point(1, 2);
  178. const target = new __1.Point(4, 6);
  179. expect(source.distance(target)).toEqual(5);
  180. });
  181. });
  182. describe('#squaredDistance', () => {
  183. it('should return the squared distance between me and the given point', () => {
  184. const source = new __1.Point(1, 2);
  185. const target = new __1.Point(4, 6);
  186. expect(source.squaredDistance(target)).toEqual(25);
  187. });
  188. });
  189. describe('#manhattanDistance', () => {
  190. it('should return the manhattan distance between me and the given point', () => {
  191. const source = new __1.Point(1, 2);
  192. const target = new __1.Point(4, 6);
  193. expect(source.manhattanDistance(target)).toEqual(7);
  194. });
  195. });
  196. describe('#magnitude', () => {
  197. it('should return the magnitude of the given point', () => {
  198. expect(new __1.Point(3, 4).magnitude()).toEqual(5);
  199. });
  200. it('should return `0.01` when the given point is `{0, 0}`', () => {
  201. expect(new __1.Point(0, 0).magnitude()).toEqual(0.01);
  202. });
  203. });
  204. describe('#theta', () => {
  205. it('should return the angle between me and p and the x-axis.', () => {
  206. const me = new __1.Point(1, 1);
  207. expect(me.theta(me)).toBe(-0);
  208. expect(me.theta(new __1.Point(2, 1))).toBe(-0);
  209. expect(me.theta(new __1.Point(2, 0))).toBe(45);
  210. expect(me.theta(new __1.Point(1, 0))).toBe(90);
  211. expect(me.theta(new __1.Point(0, 0))).toBe(135);
  212. expect(me.theta(new __1.Point(0, 1))).toBe(180);
  213. expect(me.theta(new __1.Point(0, 2))).toBe(225);
  214. expect(me.theta(new __1.Point(1, 2))).toBe(270);
  215. expect(me.theta(new __1.Point(2, 2))).toBe(315);
  216. });
  217. });
  218. describe('#angleBetween', () => {
  219. it('should returns the angle between vector from me to `p1` and the vector from me to `p2`.', () => {
  220. const me = new __1.Point(1, 2);
  221. const p1 = new __1.Point(2, 4);
  222. const p2 = new __1.Point(4, 3);
  223. const PRECISION = 10;
  224. expect(me.angleBetween(me, me)).toBeNaN();
  225. expect(me.angleBetween(p1, me)).toBeNaN();
  226. expect(me.angleBetween(me, p2)).toBeNaN();
  227. expect(me.angleBetween(p1, p2).toFixed(PRECISION)).toBe('45.0000000000');
  228. expect(me.angleBetween(p2, p1).toFixed(PRECISION)).toBe('315.0000000000');
  229. });
  230. });
  231. describe('#changeInAngle', () => {
  232. it('should return the change in angle from my previous position `-dx, -dy` to my new position relative to origin point', () => {
  233. const p = new __1.Point(1, 1);
  234. expect(p.changeInAngle(1, 0)).toEqual(-45);
  235. });
  236. it('should return the change in angle from my previous position `-dx, -dy` to my new position relative to `ref` point', () => {
  237. const p = new __1.Point(2, 2);
  238. expect(p.changeInAngle(1, 0, { x: 1, y: 1 })).toEqual(-45);
  239. });
  240. });
  241. describe('#adhereToRect', () => {
  242. it('should return `p` when `p` is contained in `rect`', () => {
  243. const p = new __1.Point(2, 2);
  244. const rect = { x: 1, y: 1, width: 4, height: 4 };
  245. expect(p.adhereToRect(rect).equals(p)).toBeTruthy();
  246. });
  247. it('should adhere to target `rect` when `p` is outside of `rect`', () => {
  248. const p = new __1.Point(2, 8);
  249. const rect = { x: 1, y: 1, width: 4, height: 4 };
  250. expect(p.adhereToRect(rect).equals({ x: 2, y: 5 })).toBeTruthy();
  251. });
  252. });
  253. describe('#bearing', () => {
  254. it('should return the bearing between me and the given point.', () => {
  255. const p = new __1.Point(2, 8);
  256. expect(p.bearing(new __1.Point())).toEqual('S');
  257. });
  258. });
  259. describe('#vectorAngle', () => {
  260. it('should return the angle between the vector from `0,0` to me and the vector from `0,0` to `p`', () => {
  261. const p0 = new __1.Point(1, 2);
  262. const p = new __1.Point(3, 1);
  263. const zero = new __1.Point(0, 0);
  264. const PRECISION = 10;
  265. expect(zero.vectorAngle(zero)).toBeNaN();
  266. expect(p0.vectorAngle(zero)).toBeNaN();
  267. expect(p.vectorAngle(zero)).toBeNaN();
  268. expect(zero.vectorAngle(p0)).toBeNaN();
  269. expect(zero.vectorAngle(p)).toBeNaN();
  270. expect(p0.vectorAngle(p).toFixed(PRECISION)).toBe('45.0000000000');
  271. expect(p.vectorAngle(p0).toFixed(PRECISION)).toBe('315.0000000000');
  272. });
  273. });
  274. describe('#diff', () => {
  275. it('should return the diff as a point with the given point', () => {
  276. expect(new __1.Point(0, 10).diff(4, 8)).toEqual(new __1.Point(-4, 2));
  277. expect(new __1.Point(5, 8).diff({ x: 5, y: 10 })).toEqual(new __1.Point(0, -2));
  278. });
  279. });
  280. describe('#lerp', () => {
  281. it('should return an interpolation between me and the given point `p`', () => {
  282. expect(new __1.Point(1, 1).lerp({ x: 3, y: 3 }, 0.5)).toEqual(new __1.Point(2, 2));
  283. });
  284. });
  285. describe('#normalize', () => {
  286. it('should scale x and y such that the distance between the point and the origin (0,0) is equal to the given length', () => {
  287. expect(new __1.Point(0, 10).normalize(20).serialize()).toEqual('0 20');
  288. expect(new __1.Point(2, 0).normalize(4).serialize()).toEqual('4 0');
  289. });
  290. });
  291. describe('#move', () => {
  292. it('should move the point on a line that leads to another point `ref` by a certain `distance`.', () => {
  293. expect(new __1.Point(1, 1).move({ x: 1, y: 0 }, 5).round(0)).toEqual(new __1.Point(1, 6));
  294. });
  295. });
  296. describe('#reflection', () => {
  297. it('should return a point that is the reflection of me with the center of inversion in `ref` point.', () => {
  298. expect(new __1.Point(1, 0).reflection({ x: 1, y: 1 }).round(0)).toEqual(new __1.Point(1, 2));
  299. });
  300. });
  301. describe('#cross', () => {
  302. it('should return the cross product of the vector from me to `p1` and the vector from me to `p2`', () => {
  303. const p0 = new __1.Point(3, 15);
  304. const p1 = new __1.Point(4, 17);
  305. const p2 = new __1.Point(2, 10);
  306. expect(p0.cross(p1, p2)).toBe(3);
  307. expect(p0.cross(p2, p1)).toBe(-3);
  308. });
  309. it('shoule return `NAN` when any of the given point is null', () => {
  310. expect(new __1.Point().cross(null, new __1.Point(1, 2))).toBeNaN();
  311. });
  312. });
  313. describe('#dot', () => {
  314. it('should return the dot product of `p`', () => {
  315. const p1 = new __1.Point(4, 17);
  316. const p2 = new __1.Point(2, 10);
  317. expect(p1.dot(p2)).toBe(178);
  318. expect(p2.dot(p1)).toBe(178);
  319. });
  320. });
  321. describe('#equals', () => {
  322. it('should return the true when have same coord', () => {
  323. const p1 = new __1.Point(4, 17);
  324. const p2 = p1.clone();
  325. expect(p1.equals(p2)).toBe(true);
  326. });
  327. });
  328. describe('#toJSON', () => {
  329. it('should return json-object', () => {
  330. const p1 = new __1.Point(4, 17);
  331. const p2 = p1.toJSON();
  332. expect(p1.equals(p2)).toBe(true);
  333. });
  334. });
  335. describe('#toPolar', () => {
  336. it('should convert rectangular to polar coordinates.', () => {
  337. const p = new __1.Point(4, 3).toPolar();
  338. expect(p.x).toBe(5);
  339. });
  340. });
  341. describe('#fromPolar', () => {
  342. it('should convert polar to rectangular coordinates.', () => {
  343. const p1 = new __1.Point(4, 3).toPolar();
  344. const p2 = __1.Point.fromPolar(p1.x, p1.y);
  345. expect(Math.round(p2.x)).toBe(4);
  346. expect(Math.round(p2.y)).toBe(3);
  347. const p3 = new __1.Point(-4, 3).toPolar();
  348. const p4 = __1.Point.fromPolar(p3.x, p3.y);
  349. expect(Math.round(p4.x)).toBe(-4);
  350. expect(Math.round(p4.y)).toBe(3);
  351. const p5 = new __1.Point(4, -3).toPolar();
  352. const p6 = __1.Point.fromPolar(p5.x, p5.y);
  353. expect(Math.round(p6.x)).toBe(4);
  354. expect(Math.round(p6.y)).toBe(-3);
  355. const p7 = new __1.Point(-4, -3).toPolar();
  356. const p8 = __1.Point.fromPolar(p7.x, p7.y);
  357. expect(Math.round(p8.x)).toBe(-4);
  358. expect(Math.round(p8.y)).toBe(-3);
  359. });
  360. });
  361. describe('#snapToGrid', () => {
  362. it('should snap to grid', () => {
  363. const p1 = new __1.Point(4, 17);
  364. const p2 = p1.clone().snapToGrid(10);
  365. const p3 = p1.clone().snapToGrid(3, 5);
  366. expect(p2.x).toBe(0);
  367. expect(p2.y).toBe(20);
  368. expect(p3.x).toBe(3);
  369. expect(p3.y).toBe(15);
  370. });
  371. });
  372. });
  373. //# sourceMappingURL=point.test.js.map