ellipse.test.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const __1 = require("..");
  4. describe('ellipse', () => {
  5. describe('#constructor', () => {
  6. it('should create an ellipse instance', () => {
  7. expect(new __1.Ellipse()).toBeInstanceOf(__1.Ellipse);
  8. expect(new __1.Ellipse(1)).toBeInstanceOf(__1.Ellipse);
  9. expect(new __1.Ellipse(1, 2)).toBeInstanceOf(__1.Ellipse);
  10. expect(new __1.Ellipse(1, 2, 3)).toBeInstanceOf(__1.Ellipse);
  11. expect(new __1.Ellipse(1, 2, 3, 4)).toBeInstanceOf(__1.Ellipse);
  12. expect(new __1.Ellipse(1, 2, 3, 4).x).toEqual(1);
  13. expect(new __1.Ellipse(1, 2, 3, 4).y).toEqual(2);
  14. expect(new __1.Ellipse(1, 2, 3, 4).a).toEqual(3);
  15. expect(new __1.Ellipse(1, 2, 3, 4).b).toEqual(4);
  16. expect(new __1.Ellipse().equals(new __1.Ellipse(0, 0, 0, 0)));
  17. });
  18. it('should return the center point', () => {
  19. const ellipse = new __1.Ellipse(1, 2, 3, 4);
  20. expect(ellipse.getCenter().equals({ x: 1, y: 2 }));
  21. expect(ellipse.center.equals({ x: 1, y: 2 }));
  22. });
  23. });
  24. describe('#Ellipse.create', () => {
  25. it('should create an ellipse from number', () => {
  26. const ellipse = __1.Ellipse.create(1, 2, 3, 4);
  27. expect(ellipse.x).toEqual(1);
  28. expect(ellipse.y).toEqual(2);
  29. expect(ellipse.a).toEqual(3);
  30. expect(ellipse.b).toEqual(4);
  31. });
  32. it('should create an ellipse from Ellipse instance', () => {
  33. const ellipse = __1.Ellipse.create(new __1.Ellipse(1, 2, 3, 4));
  34. expect(ellipse.x).toEqual(1);
  35. expect(ellipse.y).toEqual(2);
  36. expect(ellipse.a).toEqual(3);
  37. expect(ellipse.b).toEqual(4);
  38. });
  39. it('should create an ellipse from EllipseLike', () => {
  40. const ellipse = __1.Ellipse.create({ x: 1, y: 2, a: 3, b: 4 });
  41. expect(ellipse.x).toEqual(1);
  42. expect(ellipse.y).toEqual(2);
  43. expect(ellipse.a).toEqual(3);
  44. expect(ellipse.b).toEqual(4);
  45. });
  46. it('should create an ellipse from EllipseData', () => {
  47. const ellipse = __1.Ellipse.create([1, 2, 3, 4]);
  48. expect(ellipse.x).toEqual(1);
  49. expect(ellipse.y).toEqual(2);
  50. expect(ellipse.a).toEqual(3);
  51. expect(ellipse.b).toEqual(4);
  52. });
  53. });
  54. describe('#Ellipse.fromRect', () => {
  55. it('should create an ellipse from a rectangle instance', () => {
  56. const ellipse = __1.Ellipse.fromRect(new __1.Rectangle(1, 2, 3, 4));
  57. expect(ellipse.x).toEqual(2.5);
  58. expect(ellipse.y).toEqual(4);
  59. expect(ellipse.a).toEqual(1.5);
  60. expect(ellipse.b).toEqual(2);
  61. });
  62. });
  63. describe('#bbox', () => {
  64. it('should return the bbox', () => {
  65. expect(new __1.Ellipse(1, 2, 3, 4).bbox().toJSON()).toEqual({
  66. x: -2,
  67. y: -2,
  68. width: 6,
  69. height: 8,
  70. });
  71. });
  72. });
  73. describe('#inflate', () => {
  74. it('should inflate with the given `amount`', () => {
  75. expect(new __1.Ellipse(1, 2, 3, 4).inflate(2).toJSON()).toEqual({
  76. x: 1,
  77. y: 2,
  78. a: 7,
  79. b: 8,
  80. });
  81. });
  82. it('should inflate with the given `dx` and `dy`', () => {
  83. expect(new __1.Ellipse(1, 2, 3, 4).inflate(2, 3).toJSON()).toEqual({
  84. x: 1,
  85. y: 2,
  86. a: 7,
  87. b: 10,
  88. });
  89. });
  90. });
  91. describe('#normalizedDistance', () => {
  92. it('should return a normalized distance', () => {
  93. const ellipse = new __1.Ellipse(0, 0, 3, 4);
  94. expect(ellipse.normalizedDistance(1, 1) < 1).toBeTruthy();
  95. expect(ellipse.normalizedDistance({ x: 5, y: 5 }) > 1).toBeTruthy();
  96. expect(ellipse.normalizedDistance([0, 4]) === 1).toBeTruthy();
  97. });
  98. });
  99. describe('#containsPoint', () => {
  100. const ellipse = new __1.Ellipse(0, 0, 3, 4);
  101. it('shoule return true when ellipse contains the given point', () => {
  102. expect(ellipse.containsPoint(1, 1)).toBeTruthy();
  103. });
  104. it('shoule return true when the given point is on the boundary of the ellipse', () => {
  105. expect(ellipse.containsPoint([0, 4])).toBeTruthy();
  106. });
  107. it('shoule return true when ellipse not contains the given point', () => {
  108. expect(ellipse.containsPoint({ x: 5, y: 5 })).toBeFalsy();
  109. });
  110. });
  111. describe('#intersectsWithLine', () => {
  112. const ellipse = new __1.Ellipse(0, 0, 3, 4);
  113. it('should return the intersections with line', () => {
  114. expect(__1.Point.equalPoints(ellipse.intersectsWithLine(new __1.Line(0, -5, 0, 5)), [
  115. { x: 0, y: -4 },
  116. { x: 0, y: 4 },
  117. ])).toBeTruthy();
  118. expect(__1.Point.equalPoints(ellipse.intersectsWithLine(new __1.Line(0, 0, 0, 5)), [
  119. { x: 0, y: 4 },
  120. ])).toBeTruthy();
  121. expect(__1.Point.equalPoints(ellipse.intersectsWithLine(new __1.Line(3, 0, 3, 4)), [
  122. { x: 3, y: 0 },
  123. ])).toBeTruthy();
  124. });
  125. it('should return null when not intersections', () => {
  126. expect(ellipse.intersectsWithLine(new __1.Line(0, 0, 1, 1))).toBeNull();
  127. expect(ellipse.intersectsWithLine(new __1.Line(3, 5, 3, 6))).toBeNull();
  128. expect(ellipse.intersectsWithLine(new __1.Line(-6, -6, -6, 100))).toBeNull();
  129. expect(ellipse.intersectsWithLine(new __1.Line(6, 6, 100, 100))).toBeNull();
  130. });
  131. });
  132. describe('#intersectsWithLineFromCenterToPoint', () => {
  133. const ellipse = new __1.Ellipse(0, 0, 3, 4);
  134. it('should return the intersection point when the given point is outside of the ellipse', () => {
  135. expect(ellipse
  136. .intersectsWithLineFromCenterToPoint({ x: 0, y: 6 })
  137. .round()
  138. .toJSON()).toEqual({ x: 0, y: 4 });
  139. expect(ellipse
  140. .intersectsWithLineFromCenterToPoint({ x: 6, y: 0 })
  141. .round()
  142. .toJSON()).toEqual({ x: 3, y: 0 });
  143. expect(ellipse
  144. .intersectsWithLineFromCenterToPoint({ x: 0, y: 6 }, 90)
  145. .round()
  146. .toJSON()).toEqual({ x: 0, y: 3 });
  147. });
  148. it('should return the intersection point when the given point is inside of the ellipse', () => {
  149. expect(ellipse
  150. .intersectsWithLineFromCenterToPoint({ x: 0, y: 0 }, 90)
  151. .round()
  152. .toJSON()).toEqual({ x: -0, y: -3 });
  153. expect(ellipse
  154. .intersectsWithLineFromCenterToPoint({ x: 0, y: 2 })
  155. .round()
  156. .toJSON()).toEqual({ x: 0, y: 4 });
  157. expect(ellipse
  158. .intersectsWithLineFromCenterToPoint({ x: 0, y: 2 }, 90)
  159. .round()
  160. .toJSON()).toEqual({ x: 0, y: 3 });
  161. expect(ellipse
  162. .intersectsWithLineFromCenterToPoint({ x: 2, y: 0 }, 90)
  163. .round()
  164. .toJSON()).toEqual({ x: 4, y: -0 });
  165. });
  166. });
  167. describe('#tangentTheta', () => {
  168. it('should return the tangent theta', () => {
  169. const ellipse = new __1.Ellipse(0, 0, 3, 4);
  170. expect(ellipse.tangentTheta({ x: 3, y: 0 })).toEqual(270);
  171. expect(ellipse.tangentTheta({ x: -3, y: 0 })).toEqual(90);
  172. expect(ellipse.tangentTheta({ x: 0, y: 4 })).toEqual(180);
  173. expect(ellipse.tangentTheta({ x: 0, y: -4 })).toEqual(-0);
  174. });
  175. });
  176. describe('#scale', () => {
  177. it('should scale the ellipse with the given `sx` and `sy`', () => {
  178. const ellipse = new __1.Ellipse(1, 2, 3, 4).scale(3, 4);
  179. expect(ellipse.x).toEqual(1);
  180. expect(ellipse.y).toEqual(2);
  181. expect(ellipse.a).toEqual(9);
  182. expect(ellipse.b).toEqual(16);
  183. });
  184. });
  185. describe('#rotate', () => {
  186. it('should rotate the ellipse', () => {
  187. const ellipse = new __1.Ellipse(0, 0, 3, 4).rotate(90);
  188. expect(ellipse.x).toEqual(0);
  189. expect(ellipse.y).toEqual(0);
  190. expect(ellipse.a).toEqual(4);
  191. expect(Math.round(ellipse.b)).toEqual(3);
  192. });
  193. });
  194. describe('#translate', () => {
  195. it('should translate the ellipse with the given `dx` and `dy`', () => {
  196. const ellipse = new __1.Ellipse(1, 2, 3, 4).translate(3, 4);
  197. expect(ellipse.x).toEqual(4);
  198. expect(ellipse.y).toEqual(6);
  199. expect(ellipse.a).toEqual(3);
  200. expect(ellipse.b).toEqual(4);
  201. });
  202. });
  203. describe('#clone', () => {
  204. it('should return the cloned ellipse', () => {
  205. const ellipse = new __1.Ellipse(1, 2, 3, 4).clone();
  206. expect(ellipse.x).toEqual(1);
  207. expect(ellipse.y).toEqual(2);
  208. expect(ellipse.a).toEqual(3);
  209. expect(ellipse.b).toEqual(4);
  210. });
  211. });
  212. describe('#serialize', () => {
  213. it('should return the serialized string', () => {
  214. expect(new __1.Ellipse(1, 2, 3, 4).serialize()).toEqual('1 2 3 4');
  215. });
  216. });
  217. });
  218. //# sourceMappingURL=ellipse.test.js.map