math.js 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601
  1. /*!
  2. * @pixi/math - v6.5.10
  3. * Compiled Thu, 06 Jul 2023 15:25:11 UTC
  4. *
  5. * @pixi/math is licensed under the MIT License.
  6. * http://www.opensource.org/licenses/mit-license
  7. */
  8. 'use strict';
  9. Object.defineProperty(exports, '__esModule', { value: true });
  10. /**
  11. * Two Pi.
  12. * @static
  13. * @member {number}
  14. * @memberof PIXI
  15. */
  16. var PI_2 = Math.PI * 2;
  17. /**
  18. * Conversion factor for converting radians to degrees.
  19. * @static
  20. * @member {number} RAD_TO_DEG
  21. * @memberof PIXI
  22. */
  23. var RAD_TO_DEG = 180 / Math.PI;
  24. /**
  25. * Conversion factor for converting degrees to radians.
  26. * @static
  27. * @member {number}
  28. * @memberof PIXI
  29. */
  30. var DEG_TO_RAD = Math.PI / 180;
  31. /**
  32. * Constants that identify shapes, mainly to prevent `instanceof` calls.
  33. * @static
  34. * @memberof PIXI
  35. * @enum {number}
  36. * @property {number} POLY Polygon
  37. * @property {number} RECT Rectangle
  38. * @property {number} CIRC Circle
  39. * @property {number} ELIP Ellipse
  40. * @property {number} RREC Rounded Rectangle
  41. */
  42. exports.SHAPES = void 0;
  43. (function (SHAPES) {
  44. SHAPES[SHAPES["POLY"] = 0] = "POLY";
  45. SHAPES[SHAPES["RECT"] = 1] = "RECT";
  46. SHAPES[SHAPES["CIRC"] = 2] = "CIRC";
  47. SHAPES[SHAPES["ELIP"] = 3] = "ELIP";
  48. SHAPES[SHAPES["RREC"] = 4] = "RREC";
  49. })(exports.SHAPES || (exports.SHAPES = {}));
  50. /**
  51. * The Point object represents a location in a two-dimensional coordinate system, where `x` represents
  52. * the position on the horizontal axis and `y` represents the position on the vertical axis
  53. * @class
  54. * @memberof PIXI
  55. * @implements {IPoint}
  56. */
  57. var Point = /** @class */ (function () {
  58. /**
  59. * Creates a new `Point`
  60. * @param {number} [x=0] - position of the point on the x axis
  61. * @param {number} [y=0] - position of the point on the y axis
  62. */
  63. function Point(x, y) {
  64. if (x === void 0) { x = 0; }
  65. if (y === void 0) { y = 0; }
  66. /** Position of the point on the x axis */
  67. this.x = 0;
  68. /** Position of the point on the y axis */
  69. this.y = 0;
  70. this.x = x;
  71. this.y = y;
  72. }
  73. /**
  74. * Creates a clone of this point
  75. * @returns A clone of this point
  76. */
  77. Point.prototype.clone = function () {
  78. return new Point(this.x, this.y);
  79. };
  80. /**
  81. * Copies `x` and `y` from the given point into this point
  82. * @param p - The point to copy from
  83. * @returns The point instance itself
  84. */
  85. Point.prototype.copyFrom = function (p) {
  86. this.set(p.x, p.y);
  87. return this;
  88. };
  89. /**
  90. * Copies this point's x and y into the given point (`p`).
  91. * @param p - The point to copy to. Can be any of type that is or extends `IPointData`
  92. * @returns The point (`p`) with values updated
  93. */
  94. Point.prototype.copyTo = function (p) {
  95. p.set(this.x, this.y);
  96. return p;
  97. };
  98. /**
  99. * Accepts another point (`p`) and returns `true` if the given point is equal to this point
  100. * @param p - The point to check
  101. * @returns Returns `true` if both `x` and `y` are equal
  102. */
  103. Point.prototype.equals = function (p) {
  104. return (p.x === this.x) && (p.y === this.y);
  105. };
  106. /**
  107. * Sets the point to a new `x` and `y` position.
  108. * If `y` is omitted, both `x` and `y` will be set to `x`.
  109. * @param {number} [x=0] - position of the point on the `x` axis
  110. * @param {number} [y=x] - position of the point on the `y` axis
  111. * @returns The point instance itself
  112. */
  113. Point.prototype.set = function (x, y) {
  114. if (x === void 0) { x = 0; }
  115. if (y === void 0) { y = x; }
  116. this.x = x;
  117. this.y = y;
  118. return this;
  119. };
  120. Point.prototype.toString = function () {
  121. return "[@pixi/math:Point x=" + this.x + " y=" + this.y + "]";
  122. };
  123. return Point;
  124. }());
  125. var tempPoints = [new Point(), new Point(), new Point(), new Point()];
  126. /**
  127. * Size object, contains width and height
  128. * @memberof PIXI
  129. * @typedef {object} ISize
  130. * @property {number} width - Width component
  131. * @property {number} height - Height component
  132. */
  133. /**
  134. * Rectangle object is an area defined by its position, as indicated by its top-left corner
  135. * point (x, y) and by its width and its height.
  136. * @memberof PIXI
  137. */
  138. var Rectangle = /** @class */ (function () {
  139. /**
  140. * @param x - The X coordinate of the upper-left corner of the rectangle
  141. * @param y - The Y coordinate of the upper-left corner of the rectangle
  142. * @param width - The overall width of the rectangle
  143. * @param height - The overall height of the rectangle
  144. */
  145. function Rectangle(x, y, width, height) {
  146. if (x === void 0) { x = 0; }
  147. if (y === void 0) { y = 0; }
  148. if (width === void 0) { width = 0; }
  149. if (height === void 0) { height = 0; }
  150. this.x = Number(x);
  151. this.y = Number(y);
  152. this.width = Number(width);
  153. this.height = Number(height);
  154. this.type = exports.SHAPES.RECT;
  155. }
  156. Object.defineProperty(Rectangle.prototype, "left", {
  157. /** Returns the left edge of the rectangle. */
  158. get: function () {
  159. return this.x;
  160. },
  161. enumerable: false,
  162. configurable: true
  163. });
  164. Object.defineProperty(Rectangle.prototype, "right", {
  165. /** Returns the right edge of the rectangle. */
  166. get: function () {
  167. return this.x + this.width;
  168. },
  169. enumerable: false,
  170. configurable: true
  171. });
  172. Object.defineProperty(Rectangle.prototype, "top", {
  173. /** Returns the top edge of the rectangle. */
  174. get: function () {
  175. return this.y;
  176. },
  177. enumerable: false,
  178. configurable: true
  179. });
  180. Object.defineProperty(Rectangle.prototype, "bottom", {
  181. /** Returns the bottom edge of the rectangle. */
  182. get: function () {
  183. return this.y + this.height;
  184. },
  185. enumerable: false,
  186. configurable: true
  187. });
  188. Object.defineProperty(Rectangle, "EMPTY", {
  189. /** A constant empty rectangle. */
  190. get: function () {
  191. return new Rectangle(0, 0, 0, 0);
  192. },
  193. enumerable: false,
  194. configurable: true
  195. });
  196. /**
  197. * Creates a clone of this Rectangle
  198. * @returns a copy of the rectangle
  199. */
  200. Rectangle.prototype.clone = function () {
  201. return new Rectangle(this.x, this.y, this.width, this.height);
  202. };
  203. /**
  204. * Copies another rectangle to this one.
  205. * @param rectangle - The rectangle to copy from.
  206. * @returns Returns itself.
  207. */
  208. Rectangle.prototype.copyFrom = function (rectangle) {
  209. this.x = rectangle.x;
  210. this.y = rectangle.y;
  211. this.width = rectangle.width;
  212. this.height = rectangle.height;
  213. return this;
  214. };
  215. /**
  216. * Copies this rectangle to another one.
  217. * @param rectangle - The rectangle to copy to.
  218. * @returns Returns given parameter.
  219. */
  220. Rectangle.prototype.copyTo = function (rectangle) {
  221. rectangle.x = this.x;
  222. rectangle.y = this.y;
  223. rectangle.width = this.width;
  224. rectangle.height = this.height;
  225. return rectangle;
  226. };
  227. /**
  228. * Checks whether the x and y coordinates given are contained within this Rectangle
  229. * @param x - The X coordinate of the point to test
  230. * @param y - The Y coordinate of the point to test
  231. * @returns Whether the x/y coordinates are within this Rectangle
  232. */
  233. Rectangle.prototype.contains = function (x, y) {
  234. if (this.width <= 0 || this.height <= 0) {
  235. return false;
  236. }
  237. if (x >= this.x && x < this.x + this.width) {
  238. if (y >= this.y && y < this.y + this.height) {
  239. return true;
  240. }
  241. }
  242. return false;
  243. };
  244. /**
  245. * Determines whether the `other` Rectangle transformed by `transform` intersects with `this` Rectangle object.
  246. * Returns true only if the area of the intersection is >0, this means that Rectangles
  247. * sharing a side are not overlapping. Another side effect is that an arealess rectangle
  248. * (width or height equal to zero) can't intersect any other rectangle.
  249. * @param {Rectangle} other - The Rectangle to intersect with `this`.
  250. * @param {Matrix} transform - The transformation matrix of `other`.
  251. * @returns {boolean} A value of `true` if the transformed `other` Rectangle intersects with `this`; otherwise `false`.
  252. */
  253. Rectangle.prototype.intersects = function (other, transform) {
  254. if (!transform) {
  255. var x0_1 = this.x < other.x ? other.x : this.x;
  256. var x1_1 = this.right > other.right ? other.right : this.right;
  257. if (x1_1 <= x0_1) {
  258. return false;
  259. }
  260. var y0_1 = this.y < other.y ? other.y : this.y;
  261. var y1_1 = this.bottom > other.bottom ? other.bottom : this.bottom;
  262. return y1_1 > y0_1;
  263. }
  264. var x0 = this.left;
  265. var x1 = this.right;
  266. var y0 = this.top;
  267. var y1 = this.bottom;
  268. if (x1 <= x0 || y1 <= y0) {
  269. return false;
  270. }
  271. var lt = tempPoints[0].set(other.left, other.top);
  272. var lb = tempPoints[1].set(other.left, other.bottom);
  273. var rt = tempPoints[2].set(other.right, other.top);
  274. var rb = tempPoints[3].set(other.right, other.bottom);
  275. if (rt.x <= lt.x || lb.y <= lt.y) {
  276. return false;
  277. }
  278. var s = Math.sign((transform.a * transform.d) - (transform.b * transform.c));
  279. if (s === 0) {
  280. return false;
  281. }
  282. transform.apply(lt, lt);
  283. transform.apply(lb, lb);
  284. transform.apply(rt, rt);
  285. transform.apply(rb, rb);
  286. if (Math.max(lt.x, lb.x, rt.x, rb.x) <= x0
  287. || Math.min(lt.x, lb.x, rt.x, rb.x) >= x1
  288. || Math.max(lt.y, lb.y, rt.y, rb.y) <= y0
  289. || Math.min(lt.y, lb.y, rt.y, rb.y) >= y1) {
  290. return false;
  291. }
  292. var nx = s * (lb.y - lt.y);
  293. var ny = s * (lt.x - lb.x);
  294. var n00 = (nx * x0) + (ny * y0);
  295. var n10 = (nx * x1) + (ny * y0);
  296. var n01 = (nx * x0) + (ny * y1);
  297. var n11 = (nx * x1) + (ny * y1);
  298. if (Math.max(n00, n10, n01, n11) <= (nx * lt.x) + (ny * lt.y)
  299. || Math.min(n00, n10, n01, n11) >= (nx * rb.x) + (ny * rb.y)) {
  300. return false;
  301. }
  302. var mx = s * (lt.y - rt.y);
  303. var my = s * (rt.x - lt.x);
  304. var m00 = (mx * x0) + (my * y0);
  305. var m10 = (mx * x1) + (my * y0);
  306. var m01 = (mx * x0) + (my * y1);
  307. var m11 = (mx * x1) + (my * y1);
  308. if (Math.max(m00, m10, m01, m11) <= (mx * lt.x) + (my * lt.y)
  309. || Math.min(m00, m10, m01, m11) >= (mx * rb.x) + (my * rb.y)) {
  310. return false;
  311. }
  312. return true;
  313. };
  314. /**
  315. * Pads the rectangle making it grow in all directions.
  316. * If paddingY is omitted, both paddingX and paddingY will be set to paddingX.
  317. * @param paddingX - The horizontal padding amount.
  318. * @param paddingY - The vertical padding amount.
  319. * @returns Returns itself.
  320. */
  321. Rectangle.prototype.pad = function (paddingX, paddingY) {
  322. if (paddingX === void 0) { paddingX = 0; }
  323. if (paddingY === void 0) { paddingY = paddingX; }
  324. this.x -= paddingX;
  325. this.y -= paddingY;
  326. this.width += paddingX * 2;
  327. this.height += paddingY * 2;
  328. return this;
  329. };
  330. /**
  331. * Fits this rectangle around the passed one.
  332. * @param rectangle - The rectangle to fit.
  333. * @returns Returns itself.
  334. */
  335. Rectangle.prototype.fit = function (rectangle) {
  336. var x1 = Math.max(this.x, rectangle.x);
  337. var x2 = Math.min(this.x + this.width, rectangle.x + rectangle.width);
  338. var y1 = Math.max(this.y, rectangle.y);
  339. var y2 = Math.min(this.y + this.height, rectangle.y + rectangle.height);
  340. this.x = x1;
  341. this.width = Math.max(x2 - x1, 0);
  342. this.y = y1;
  343. this.height = Math.max(y2 - y1, 0);
  344. return this;
  345. };
  346. /**
  347. * Enlarges rectangle that way its corners lie on grid
  348. * @param resolution - resolution
  349. * @param eps - precision
  350. * @returns Returns itself.
  351. */
  352. Rectangle.prototype.ceil = function (resolution, eps) {
  353. if (resolution === void 0) { resolution = 1; }
  354. if (eps === void 0) { eps = 0.001; }
  355. var x2 = Math.ceil((this.x + this.width - eps) * resolution) / resolution;
  356. var y2 = Math.ceil((this.y + this.height - eps) * resolution) / resolution;
  357. this.x = Math.floor((this.x + eps) * resolution) / resolution;
  358. this.y = Math.floor((this.y + eps) * resolution) / resolution;
  359. this.width = x2 - this.x;
  360. this.height = y2 - this.y;
  361. return this;
  362. };
  363. /**
  364. * Enlarges this rectangle to include the passed rectangle.
  365. * @param rectangle - The rectangle to include.
  366. * @returns Returns itself.
  367. */
  368. Rectangle.prototype.enlarge = function (rectangle) {
  369. var x1 = Math.min(this.x, rectangle.x);
  370. var x2 = Math.max(this.x + this.width, rectangle.x + rectangle.width);
  371. var y1 = Math.min(this.y, rectangle.y);
  372. var y2 = Math.max(this.y + this.height, rectangle.y + rectangle.height);
  373. this.x = x1;
  374. this.width = x2 - x1;
  375. this.y = y1;
  376. this.height = y2 - y1;
  377. return this;
  378. };
  379. Rectangle.prototype.toString = function () {
  380. return "[@pixi/math:Rectangle x=" + this.x + " y=" + this.y + " width=" + this.width + " height=" + this.height + "]";
  381. };
  382. return Rectangle;
  383. }());
  384. /**
  385. * The Circle object is used to help draw graphics and can also be used to specify a hit area for displayObjects.
  386. * @memberof PIXI
  387. */
  388. var Circle = /** @class */ (function () {
  389. /**
  390. * @param x - The X coordinate of the center of this circle
  391. * @param y - The Y coordinate of the center of this circle
  392. * @param radius - The radius of the circle
  393. */
  394. function Circle(x, y, radius) {
  395. if (x === void 0) { x = 0; }
  396. if (y === void 0) { y = 0; }
  397. if (radius === void 0) { radius = 0; }
  398. this.x = x;
  399. this.y = y;
  400. this.radius = radius;
  401. this.type = exports.SHAPES.CIRC;
  402. }
  403. /**
  404. * Creates a clone of this Circle instance
  405. * @returns A copy of the Circle
  406. */
  407. Circle.prototype.clone = function () {
  408. return new Circle(this.x, this.y, this.radius);
  409. };
  410. /**
  411. * Checks whether the x and y coordinates given are contained within this circle
  412. * @param x - The X coordinate of the point to test
  413. * @param y - The Y coordinate of the point to test
  414. * @returns Whether the x/y coordinates are within this Circle
  415. */
  416. Circle.prototype.contains = function (x, y) {
  417. if (this.radius <= 0) {
  418. return false;
  419. }
  420. var r2 = this.radius * this.radius;
  421. var dx = (this.x - x);
  422. var dy = (this.y - y);
  423. dx *= dx;
  424. dy *= dy;
  425. return (dx + dy <= r2);
  426. };
  427. /**
  428. * Returns the framing rectangle of the circle as a Rectangle object
  429. * @returns The framing rectangle
  430. */
  431. Circle.prototype.getBounds = function () {
  432. return new Rectangle(this.x - this.radius, this.y - this.radius, this.radius * 2, this.radius * 2);
  433. };
  434. Circle.prototype.toString = function () {
  435. return "[@pixi/math:Circle x=" + this.x + " y=" + this.y + " radius=" + this.radius + "]";
  436. };
  437. return Circle;
  438. }());
  439. /**
  440. * The Ellipse object is used to help draw graphics and can also be used to specify a hit area for displayObjects.
  441. * @memberof PIXI
  442. */
  443. var Ellipse = /** @class */ (function () {
  444. /**
  445. * @param x - The X coordinate of the center of this ellipse
  446. * @param y - The Y coordinate of the center of this ellipse
  447. * @param halfWidth - The half width of this ellipse
  448. * @param halfHeight - The half height of this ellipse
  449. */
  450. function Ellipse(x, y, halfWidth, halfHeight) {
  451. if (x === void 0) { x = 0; }
  452. if (y === void 0) { y = 0; }
  453. if (halfWidth === void 0) { halfWidth = 0; }
  454. if (halfHeight === void 0) { halfHeight = 0; }
  455. this.x = x;
  456. this.y = y;
  457. this.width = halfWidth;
  458. this.height = halfHeight;
  459. this.type = exports.SHAPES.ELIP;
  460. }
  461. /**
  462. * Creates a clone of this Ellipse instance
  463. * @returns {PIXI.Ellipse} A copy of the ellipse
  464. */
  465. Ellipse.prototype.clone = function () {
  466. return new Ellipse(this.x, this.y, this.width, this.height);
  467. };
  468. /**
  469. * Checks whether the x and y coordinates given are contained within this ellipse
  470. * @param x - The X coordinate of the point to test
  471. * @param y - The Y coordinate of the point to test
  472. * @returns Whether the x/y coords are within this ellipse
  473. */
  474. Ellipse.prototype.contains = function (x, y) {
  475. if (this.width <= 0 || this.height <= 0) {
  476. return false;
  477. }
  478. // normalize the coords to an ellipse with center 0,0
  479. var normx = ((x - this.x) / this.width);
  480. var normy = ((y - this.y) / this.height);
  481. normx *= normx;
  482. normy *= normy;
  483. return (normx + normy <= 1);
  484. };
  485. /**
  486. * Returns the framing rectangle of the ellipse as a Rectangle object
  487. * @returns The framing rectangle
  488. */
  489. Ellipse.prototype.getBounds = function () {
  490. return new Rectangle(this.x - this.width, this.y - this.height, this.width, this.height);
  491. };
  492. Ellipse.prototype.toString = function () {
  493. return "[@pixi/math:Ellipse x=" + this.x + " y=" + this.y + " width=" + this.width + " height=" + this.height + "]";
  494. };
  495. return Ellipse;
  496. }());
  497. /**
  498. * A class to define a shape via user defined coordinates.
  499. * @memberof PIXI
  500. */
  501. var Polygon = /** @class */ (function () {
  502. /**
  503. * @param {PIXI.IPointData[]|number[]} points - This can be an array of Points
  504. * that form the polygon, a flat array of numbers that will be interpreted as [x,y, x,y, ...], or
  505. * the arguments passed can be all the points of the polygon e.g.
  506. * `new PIXI.Polygon(new PIXI.Point(), new PIXI.Point(), ...)`, or the arguments passed can be flat
  507. * x,y values e.g. `new Polygon(x,y, x,y, x,y, ...)` where `x` and `y` are Numbers.
  508. */
  509. function Polygon() {
  510. var arguments$1 = arguments;
  511. var points = [];
  512. for (var _i = 0; _i < arguments.length; _i++) {
  513. points[_i] = arguments$1[_i];
  514. }
  515. var flat = Array.isArray(points[0]) ? points[0] : points;
  516. // if this is an array of points, convert it to a flat array of numbers
  517. if (typeof flat[0] !== 'number') {
  518. var p = [];
  519. for (var i = 0, il = flat.length; i < il; i++) {
  520. p.push(flat[i].x, flat[i].y);
  521. }
  522. flat = p;
  523. }
  524. this.points = flat;
  525. this.type = exports.SHAPES.POLY;
  526. this.closeStroke = true;
  527. }
  528. /**
  529. * Creates a clone of this polygon.
  530. * @returns - A copy of the polygon.
  531. */
  532. Polygon.prototype.clone = function () {
  533. var points = this.points.slice();
  534. var polygon = new Polygon(points);
  535. polygon.closeStroke = this.closeStroke;
  536. return polygon;
  537. };
  538. /**
  539. * Checks whether the x and y coordinates passed to this function are contained within this polygon.
  540. * @param x - The X coordinate of the point to test.
  541. * @param y - The Y coordinate of the point to test.
  542. * @returns - Whether the x/y coordinates are within this polygon.
  543. */
  544. Polygon.prototype.contains = function (x, y) {
  545. var inside = false;
  546. // use some raycasting to test hits
  547. // https://github.com/substack/point-in-polygon/blob/master/index.js
  548. var length = this.points.length / 2;
  549. for (var i = 0, j = length - 1; i < length; j = i++) {
  550. var xi = this.points[i * 2];
  551. var yi = this.points[(i * 2) + 1];
  552. var xj = this.points[j * 2];
  553. var yj = this.points[(j * 2) + 1];
  554. var intersect = ((yi > y) !== (yj > y)) && (x < ((xj - xi) * ((y - yi) / (yj - yi))) + xi);
  555. if (intersect) {
  556. inside = !inside;
  557. }
  558. }
  559. return inside;
  560. };
  561. Polygon.prototype.toString = function () {
  562. return "[@pixi/math:Polygon"
  563. + ("closeStroke=" + this.closeStroke)
  564. + ("points=" + this.points.reduce(function (pointsDesc, currentPoint) { return pointsDesc + ", " + currentPoint; }, '') + "]");
  565. };
  566. return Polygon;
  567. }());
  568. /**
  569. * The Rounded Rectangle object is an area that has nice rounded corners, as indicated by its
  570. * top-left corner point (x, y) and by its width and its height and its radius.
  571. * @memberof PIXI
  572. */
  573. var RoundedRectangle = /** @class */ (function () {
  574. /**
  575. * @param x - The X coordinate of the upper-left corner of the rounded rectangle
  576. * @param y - The Y coordinate of the upper-left corner of the rounded rectangle
  577. * @param width - The overall width of this rounded rectangle
  578. * @param height - The overall height of this rounded rectangle
  579. * @param radius - Controls the radius of the rounded corners
  580. */
  581. function RoundedRectangle(x, y, width, height, radius) {
  582. if (x === void 0) { x = 0; }
  583. if (y === void 0) { y = 0; }
  584. if (width === void 0) { width = 0; }
  585. if (height === void 0) { height = 0; }
  586. if (radius === void 0) { radius = 20; }
  587. this.x = x;
  588. this.y = y;
  589. this.width = width;
  590. this.height = height;
  591. this.radius = radius;
  592. this.type = exports.SHAPES.RREC;
  593. }
  594. /**
  595. * Creates a clone of this Rounded Rectangle.
  596. * @returns - A copy of the rounded rectangle.
  597. */
  598. RoundedRectangle.prototype.clone = function () {
  599. return new RoundedRectangle(this.x, this.y, this.width, this.height, this.radius);
  600. };
  601. /**
  602. * Checks whether the x and y coordinates given are contained within this Rounded Rectangle
  603. * @param x - The X coordinate of the point to test.
  604. * @param y - The Y coordinate of the point to test.
  605. * @returns - Whether the x/y coordinates are within this Rounded Rectangle.
  606. */
  607. RoundedRectangle.prototype.contains = function (x, y) {
  608. if (this.width <= 0 || this.height <= 0) {
  609. return false;
  610. }
  611. if (x >= this.x && x <= this.x + this.width) {
  612. if (y >= this.y && y <= this.y + this.height) {
  613. var radius = Math.max(0, Math.min(this.radius, Math.min(this.width, this.height) / 2));
  614. if ((y >= this.y + radius && y <= this.y + this.height - radius)
  615. || (x >= this.x + radius && x <= this.x + this.width - radius)) {
  616. return true;
  617. }
  618. var dx = x - (this.x + radius);
  619. var dy = y - (this.y + radius);
  620. var radius2 = radius * radius;
  621. if ((dx * dx) + (dy * dy) <= radius2) {
  622. return true;
  623. }
  624. dx = x - (this.x + this.width - radius);
  625. if ((dx * dx) + (dy * dy) <= radius2) {
  626. return true;
  627. }
  628. dy = y - (this.y + this.height - radius);
  629. if ((dx * dx) + (dy * dy) <= radius2) {
  630. return true;
  631. }
  632. dx = x - (this.x + radius);
  633. if ((dx * dx) + (dy * dy) <= radius2) {
  634. return true;
  635. }
  636. }
  637. }
  638. return false;
  639. };
  640. RoundedRectangle.prototype.toString = function () {
  641. return "[@pixi/math:RoundedRectangle x=" + this.x + " y=" + this.y
  642. + ("width=" + this.width + " height=" + this.height + " radius=" + this.radius + "]");
  643. };
  644. return RoundedRectangle;
  645. }());
  646. /**
  647. * The ObservablePoint object represents a location in a two-dimensional coordinate system, where `x` represents
  648. * the position on the horizontal axis and `y` represents the position on the vertical axis.
  649. *
  650. * An `ObservablePoint` is a point that triggers a callback when the point's position is changed.
  651. * @memberof PIXI
  652. */
  653. var ObservablePoint = /** @class */ (function () {
  654. /**
  655. * Creates a new `ObservablePoint`
  656. * @param cb - callback function triggered when `x` and/or `y` are changed
  657. * @param scope - owner of callback
  658. * @param {number} [x=0] - position of the point on the x axis
  659. * @param {number} [y=0] - position of the point on the y axis
  660. */
  661. function ObservablePoint(cb, scope, x, y) {
  662. if (x === void 0) { x = 0; }
  663. if (y === void 0) { y = 0; }
  664. this._x = x;
  665. this._y = y;
  666. this.cb = cb;
  667. this.scope = scope;
  668. }
  669. /**
  670. * Creates a clone of this point.
  671. * The callback and scope params can be overridden otherwise they will default
  672. * to the clone object's values.
  673. * @override
  674. * @param cb - The callback function triggered when `x` and/or `y` are changed
  675. * @param scope - The owner of the callback
  676. * @returns a copy of this observable point
  677. */
  678. ObservablePoint.prototype.clone = function (cb, scope) {
  679. if (cb === void 0) { cb = this.cb; }
  680. if (scope === void 0) { scope = this.scope; }
  681. return new ObservablePoint(cb, scope, this._x, this._y);
  682. };
  683. /**
  684. * Sets the point to a new `x` and `y` position.
  685. * If `y` is omitted, both `x` and `y` will be set to `x`.
  686. * @param {number} [x=0] - position of the point on the x axis
  687. * @param {number} [y=x] - position of the point on the y axis
  688. * @returns The observable point instance itself
  689. */
  690. ObservablePoint.prototype.set = function (x, y) {
  691. if (x === void 0) { x = 0; }
  692. if (y === void 0) { y = x; }
  693. if (this._x !== x || this._y !== y) {
  694. this._x = x;
  695. this._y = y;
  696. this.cb.call(this.scope);
  697. }
  698. return this;
  699. };
  700. /**
  701. * Copies x and y from the given point (`p`)
  702. * @param p - The point to copy from. Can be any of type that is or extends `IPointData`
  703. * @returns The observable point instance itself
  704. */
  705. ObservablePoint.prototype.copyFrom = function (p) {
  706. if (this._x !== p.x || this._y !== p.y) {
  707. this._x = p.x;
  708. this._y = p.y;
  709. this.cb.call(this.scope);
  710. }
  711. return this;
  712. };
  713. /**
  714. * Copies this point's x and y into that of the given point (`p`)
  715. * @param p - The point to copy to. Can be any of type that is or extends `IPointData`
  716. * @returns The point (`p`) with values updated
  717. */
  718. ObservablePoint.prototype.copyTo = function (p) {
  719. p.set(this._x, this._y);
  720. return p;
  721. };
  722. /**
  723. * Accepts another point (`p`) and returns `true` if the given point is equal to this point
  724. * @param p - The point to check
  725. * @returns Returns `true` if both `x` and `y` are equal
  726. */
  727. ObservablePoint.prototype.equals = function (p) {
  728. return (p.x === this._x) && (p.y === this._y);
  729. };
  730. ObservablePoint.prototype.toString = function () {
  731. return "[@pixi/math:ObservablePoint x=" + 0 + " y=" + 0 + " scope=" + this.scope + "]";
  732. };
  733. Object.defineProperty(ObservablePoint.prototype, "x", {
  734. /** Position of the observable point on the x axis. */
  735. get: function () {
  736. return this._x;
  737. },
  738. set: function (value) {
  739. if (this._x !== value) {
  740. this._x = value;
  741. this.cb.call(this.scope);
  742. }
  743. },
  744. enumerable: false,
  745. configurable: true
  746. });
  747. Object.defineProperty(ObservablePoint.prototype, "y", {
  748. /** Position of the observable point on the y axis. */
  749. get: function () {
  750. return this._y;
  751. },
  752. set: function (value) {
  753. if (this._y !== value) {
  754. this._y = value;
  755. this.cb.call(this.scope);
  756. }
  757. },
  758. enumerable: false,
  759. configurable: true
  760. });
  761. return ObservablePoint;
  762. }());
  763. /**
  764. * The PixiJS Matrix as a class makes it a lot faster.
  765. *
  766. * Here is a representation of it:
  767. * ```js
  768. * | a | c | tx|
  769. * | b | d | ty|
  770. * | 0 | 0 | 1 |
  771. * ```
  772. * @memberof PIXI
  773. */
  774. var Matrix = /** @class */ (function () {
  775. /**
  776. * @param a - x scale
  777. * @param b - y skew
  778. * @param c - x skew
  779. * @param d - y scale
  780. * @param tx - x translation
  781. * @param ty - y translation
  782. */
  783. function Matrix(a, b, c, d, tx, ty) {
  784. if (a === void 0) { a = 1; }
  785. if (b === void 0) { b = 0; }
  786. if (c === void 0) { c = 0; }
  787. if (d === void 0) { d = 1; }
  788. if (tx === void 0) { tx = 0; }
  789. if (ty === void 0) { ty = 0; }
  790. this.array = null;
  791. this.a = a;
  792. this.b = b;
  793. this.c = c;
  794. this.d = d;
  795. this.tx = tx;
  796. this.ty = ty;
  797. }
  798. /**
  799. * Creates a Matrix object based on the given array. The Element to Matrix mapping order is as follows:
  800. *
  801. * a = array[0]
  802. * b = array[1]
  803. * c = array[3]
  804. * d = array[4]
  805. * tx = array[2]
  806. * ty = array[5]
  807. * @param array - The array that the matrix will be populated from.
  808. */
  809. Matrix.prototype.fromArray = function (array) {
  810. this.a = array[0];
  811. this.b = array[1];
  812. this.c = array[3];
  813. this.d = array[4];
  814. this.tx = array[2];
  815. this.ty = array[5];
  816. };
  817. /**
  818. * Sets the matrix properties.
  819. * @param a - Matrix component
  820. * @param b - Matrix component
  821. * @param c - Matrix component
  822. * @param d - Matrix component
  823. * @param tx - Matrix component
  824. * @param ty - Matrix component
  825. * @returns This matrix. Good for chaining method calls.
  826. */
  827. Matrix.prototype.set = function (a, b, c, d, tx, ty) {
  828. this.a = a;
  829. this.b = b;
  830. this.c = c;
  831. this.d = d;
  832. this.tx = tx;
  833. this.ty = ty;
  834. return this;
  835. };
  836. /**
  837. * Creates an array from the current Matrix object.
  838. * @param transpose - Whether we need to transpose the matrix or not
  839. * @param [out=new Float32Array(9)] - If provided the array will be assigned to out
  840. * @returns The newly created array which contains the matrix
  841. */
  842. Matrix.prototype.toArray = function (transpose, out) {
  843. if (!this.array) {
  844. this.array = new Float32Array(9);
  845. }
  846. var array = out || this.array;
  847. if (transpose) {
  848. array[0] = this.a;
  849. array[1] = this.b;
  850. array[2] = 0;
  851. array[3] = this.c;
  852. array[4] = this.d;
  853. array[5] = 0;
  854. array[6] = this.tx;
  855. array[7] = this.ty;
  856. array[8] = 1;
  857. }
  858. else {
  859. array[0] = this.a;
  860. array[1] = this.c;
  861. array[2] = this.tx;
  862. array[3] = this.b;
  863. array[4] = this.d;
  864. array[5] = this.ty;
  865. array[6] = 0;
  866. array[7] = 0;
  867. array[8] = 1;
  868. }
  869. return array;
  870. };
  871. /**
  872. * Get a new position with the current transformation applied.
  873. * Can be used to go from a child's coordinate space to the world coordinate space. (e.g. rendering)
  874. * @param pos - The origin
  875. * @param {PIXI.Point} [newPos] - The point that the new position is assigned to (allowed to be same as input)
  876. * @returns {PIXI.Point} The new point, transformed through this matrix
  877. */
  878. Matrix.prototype.apply = function (pos, newPos) {
  879. newPos = (newPos || new Point());
  880. var x = pos.x;
  881. var y = pos.y;
  882. newPos.x = (this.a * x) + (this.c * y) + this.tx;
  883. newPos.y = (this.b * x) + (this.d * y) + this.ty;
  884. return newPos;
  885. };
  886. /**
  887. * Get a new position with the inverse of the current transformation applied.
  888. * Can be used to go from the world coordinate space to a child's coordinate space. (e.g. input)
  889. * @param pos - The origin
  890. * @param {PIXI.Point} [newPos] - The point that the new position is assigned to (allowed to be same as input)
  891. * @returns {PIXI.Point} The new point, inverse-transformed through this matrix
  892. */
  893. Matrix.prototype.applyInverse = function (pos, newPos) {
  894. newPos = (newPos || new Point());
  895. var id = 1 / ((this.a * this.d) + (this.c * -this.b));
  896. var x = pos.x;
  897. var y = pos.y;
  898. newPos.x = (this.d * id * x) + (-this.c * id * y) + (((this.ty * this.c) - (this.tx * this.d)) * id);
  899. newPos.y = (this.a * id * y) + (-this.b * id * x) + (((-this.ty * this.a) + (this.tx * this.b)) * id);
  900. return newPos;
  901. };
  902. /**
  903. * Translates the matrix on the x and y.
  904. * @param x - How much to translate x by
  905. * @param y - How much to translate y by
  906. * @returns This matrix. Good for chaining method calls.
  907. */
  908. Matrix.prototype.translate = function (x, y) {
  909. this.tx += x;
  910. this.ty += y;
  911. return this;
  912. };
  913. /**
  914. * Applies a scale transformation to the matrix.
  915. * @param x - The amount to scale horizontally
  916. * @param y - The amount to scale vertically
  917. * @returns This matrix. Good for chaining method calls.
  918. */
  919. Matrix.prototype.scale = function (x, y) {
  920. this.a *= x;
  921. this.d *= y;
  922. this.c *= x;
  923. this.b *= y;
  924. this.tx *= x;
  925. this.ty *= y;
  926. return this;
  927. };
  928. /**
  929. * Applies a rotation transformation to the matrix.
  930. * @param angle - The angle in radians.
  931. * @returns This matrix. Good for chaining method calls.
  932. */
  933. Matrix.prototype.rotate = function (angle) {
  934. var cos = Math.cos(angle);
  935. var sin = Math.sin(angle);
  936. var a1 = this.a;
  937. var c1 = this.c;
  938. var tx1 = this.tx;
  939. this.a = (a1 * cos) - (this.b * sin);
  940. this.b = (a1 * sin) + (this.b * cos);
  941. this.c = (c1 * cos) - (this.d * sin);
  942. this.d = (c1 * sin) + (this.d * cos);
  943. this.tx = (tx1 * cos) - (this.ty * sin);
  944. this.ty = (tx1 * sin) + (this.ty * cos);
  945. return this;
  946. };
  947. /**
  948. * Appends the given Matrix to this Matrix.
  949. * @param matrix - The matrix to append.
  950. * @returns This matrix. Good for chaining method calls.
  951. */
  952. Matrix.prototype.append = function (matrix) {
  953. var a1 = this.a;
  954. var b1 = this.b;
  955. var c1 = this.c;
  956. var d1 = this.d;
  957. this.a = (matrix.a * a1) + (matrix.b * c1);
  958. this.b = (matrix.a * b1) + (matrix.b * d1);
  959. this.c = (matrix.c * a1) + (matrix.d * c1);
  960. this.d = (matrix.c * b1) + (matrix.d * d1);
  961. this.tx = (matrix.tx * a1) + (matrix.ty * c1) + this.tx;
  962. this.ty = (matrix.tx * b1) + (matrix.ty * d1) + this.ty;
  963. return this;
  964. };
  965. /**
  966. * Sets the matrix based on all the available properties
  967. * @param x - Position on the x axis
  968. * @param y - Position on the y axis
  969. * @param pivotX - Pivot on the x axis
  970. * @param pivotY - Pivot on the y axis
  971. * @param scaleX - Scale on the x axis
  972. * @param scaleY - Scale on the y axis
  973. * @param rotation - Rotation in radians
  974. * @param skewX - Skew on the x axis
  975. * @param skewY - Skew on the y axis
  976. * @returns This matrix. Good for chaining method calls.
  977. */
  978. Matrix.prototype.setTransform = function (x, y, pivotX, pivotY, scaleX, scaleY, rotation, skewX, skewY) {
  979. this.a = Math.cos(rotation + skewY) * scaleX;
  980. this.b = Math.sin(rotation + skewY) * scaleX;
  981. this.c = -Math.sin(rotation - skewX) * scaleY;
  982. this.d = Math.cos(rotation - skewX) * scaleY;
  983. this.tx = x - ((pivotX * this.a) + (pivotY * this.c));
  984. this.ty = y - ((pivotX * this.b) + (pivotY * this.d));
  985. return this;
  986. };
  987. /**
  988. * Prepends the given Matrix to this Matrix.
  989. * @param matrix - The matrix to prepend
  990. * @returns This matrix. Good for chaining method calls.
  991. */
  992. Matrix.prototype.prepend = function (matrix) {
  993. var tx1 = this.tx;
  994. if (matrix.a !== 1 || matrix.b !== 0 || matrix.c !== 0 || matrix.d !== 1) {
  995. var a1 = this.a;
  996. var c1 = this.c;
  997. this.a = (a1 * matrix.a) + (this.b * matrix.c);
  998. this.b = (a1 * matrix.b) + (this.b * matrix.d);
  999. this.c = (c1 * matrix.a) + (this.d * matrix.c);
  1000. this.d = (c1 * matrix.b) + (this.d * matrix.d);
  1001. }
  1002. this.tx = (tx1 * matrix.a) + (this.ty * matrix.c) + matrix.tx;
  1003. this.ty = (tx1 * matrix.b) + (this.ty * matrix.d) + matrix.ty;
  1004. return this;
  1005. };
  1006. /**
  1007. * Decomposes the matrix (x, y, scaleX, scaleY, and rotation) and sets the properties on to a transform.
  1008. * @param transform - The transform to apply the properties to.
  1009. * @returns The transform with the newly applied properties
  1010. */
  1011. Matrix.prototype.decompose = function (transform) {
  1012. // sort out rotation / skew..
  1013. var a = this.a;
  1014. var b = this.b;
  1015. var c = this.c;
  1016. var d = this.d;
  1017. var pivot = transform.pivot;
  1018. var skewX = -Math.atan2(-c, d);
  1019. var skewY = Math.atan2(b, a);
  1020. var delta = Math.abs(skewX + skewY);
  1021. if (delta < 0.00001 || Math.abs(PI_2 - delta) < 0.00001) {
  1022. transform.rotation = skewY;
  1023. transform.skew.x = transform.skew.y = 0;
  1024. }
  1025. else {
  1026. transform.rotation = 0;
  1027. transform.skew.x = skewX;
  1028. transform.skew.y = skewY;
  1029. }
  1030. // next set scale
  1031. transform.scale.x = Math.sqrt((a * a) + (b * b));
  1032. transform.scale.y = Math.sqrt((c * c) + (d * d));
  1033. // next set position
  1034. transform.position.x = this.tx + ((pivot.x * a) + (pivot.y * c));
  1035. transform.position.y = this.ty + ((pivot.x * b) + (pivot.y * d));
  1036. return transform;
  1037. };
  1038. /**
  1039. * Inverts this matrix
  1040. * @returns This matrix. Good for chaining method calls.
  1041. */
  1042. Matrix.prototype.invert = function () {
  1043. var a1 = this.a;
  1044. var b1 = this.b;
  1045. var c1 = this.c;
  1046. var d1 = this.d;
  1047. var tx1 = this.tx;
  1048. var n = (a1 * d1) - (b1 * c1);
  1049. this.a = d1 / n;
  1050. this.b = -b1 / n;
  1051. this.c = -c1 / n;
  1052. this.d = a1 / n;
  1053. this.tx = ((c1 * this.ty) - (d1 * tx1)) / n;
  1054. this.ty = -((a1 * this.ty) - (b1 * tx1)) / n;
  1055. return this;
  1056. };
  1057. /**
  1058. * Resets this Matrix to an identity (default) matrix.
  1059. * @returns This matrix. Good for chaining method calls.
  1060. */
  1061. Matrix.prototype.identity = function () {
  1062. this.a = 1;
  1063. this.b = 0;
  1064. this.c = 0;
  1065. this.d = 1;
  1066. this.tx = 0;
  1067. this.ty = 0;
  1068. return this;
  1069. };
  1070. /**
  1071. * Creates a new Matrix object with the same values as this one.
  1072. * @returns A copy of this matrix. Good for chaining method calls.
  1073. */
  1074. Matrix.prototype.clone = function () {
  1075. var matrix = new Matrix();
  1076. matrix.a = this.a;
  1077. matrix.b = this.b;
  1078. matrix.c = this.c;
  1079. matrix.d = this.d;
  1080. matrix.tx = this.tx;
  1081. matrix.ty = this.ty;
  1082. return matrix;
  1083. };
  1084. /**
  1085. * Changes the values of the given matrix to be the same as the ones in this matrix
  1086. * @param matrix - The matrix to copy to.
  1087. * @returns The matrix given in parameter with its values updated.
  1088. */
  1089. Matrix.prototype.copyTo = function (matrix) {
  1090. matrix.a = this.a;
  1091. matrix.b = this.b;
  1092. matrix.c = this.c;
  1093. matrix.d = this.d;
  1094. matrix.tx = this.tx;
  1095. matrix.ty = this.ty;
  1096. return matrix;
  1097. };
  1098. /**
  1099. * Changes the values of the matrix to be the same as the ones in given matrix
  1100. * @param {PIXI.Matrix} matrix - The matrix to copy from.
  1101. * @returns {PIXI.Matrix} this
  1102. */
  1103. Matrix.prototype.copyFrom = function (matrix) {
  1104. this.a = matrix.a;
  1105. this.b = matrix.b;
  1106. this.c = matrix.c;
  1107. this.d = matrix.d;
  1108. this.tx = matrix.tx;
  1109. this.ty = matrix.ty;
  1110. return this;
  1111. };
  1112. Matrix.prototype.toString = function () {
  1113. return "[@pixi/math:Matrix a=" + this.a + " b=" + this.b + " c=" + this.c + " d=" + this.d + " tx=" + this.tx + " ty=" + this.ty + "]";
  1114. };
  1115. Object.defineProperty(Matrix, "IDENTITY", {
  1116. /**
  1117. * A default (identity) matrix
  1118. * @readonly
  1119. */
  1120. get: function () {
  1121. return new Matrix();
  1122. },
  1123. enumerable: false,
  1124. configurable: true
  1125. });
  1126. Object.defineProperty(Matrix, "TEMP_MATRIX", {
  1127. /**
  1128. * A temp matrix
  1129. * @readonly
  1130. */
  1131. get: function () {
  1132. return new Matrix();
  1133. },
  1134. enumerable: false,
  1135. configurable: true
  1136. });
  1137. return Matrix;
  1138. }());
  1139. // Your friendly neighbour https://en.wikipedia.org/wiki/Dihedral_group
  1140. /*
  1141. * Transform matrix for operation n is:
  1142. * | ux | vx |
  1143. * | uy | vy |
  1144. */
  1145. var ux = [1, 1, 0, -1, -1, -1, 0, 1, 1, 1, 0, -1, -1, -1, 0, 1];
  1146. var uy = [0, 1, 1, 1, 0, -1, -1, -1, 0, 1, 1, 1, 0, -1, -1, -1];
  1147. var vx = [0, -1, -1, -1, 0, 1, 1, 1, 0, 1, 1, 1, 0, -1, -1, -1];
  1148. var vy = [1, 1, 0, -1, -1, -1, 0, 1, -1, -1, 0, 1, 1, 1, 0, -1];
  1149. /**
  1150. * [Cayley Table]{@link https://en.wikipedia.org/wiki/Cayley_table}
  1151. * for the composition of each rotation in the dihederal group D8.
  1152. * @type {number[][]}
  1153. * @private
  1154. */
  1155. var rotationCayley = [];
  1156. /**
  1157. * Matrices for each `GD8Symmetry` rotation.
  1158. * @type {PIXI.Matrix[]}
  1159. * @private
  1160. */
  1161. var rotationMatrices = [];
  1162. /*
  1163. * Alias for {@code Math.sign}.
  1164. */
  1165. var signum = Math.sign;
  1166. /*
  1167. * Initializes `rotationCayley` and `rotationMatrices`. It is called
  1168. * only once below.
  1169. */
  1170. function init() {
  1171. for (var i = 0; i < 16; i++) {
  1172. var row = [];
  1173. rotationCayley.push(row);
  1174. for (var j = 0; j < 16; j++) {
  1175. /* Multiplies rotation matrices i and j. */
  1176. var _ux = signum((ux[i] * ux[j]) + (vx[i] * uy[j]));
  1177. var _uy = signum((uy[i] * ux[j]) + (vy[i] * uy[j]));
  1178. var _vx = signum((ux[i] * vx[j]) + (vx[i] * vy[j]));
  1179. var _vy = signum((uy[i] * vx[j]) + (vy[i] * vy[j]));
  1180. /* Finds rotation matrix matching the product and pushes it. */
  1181. for (var k = 0; k < 16; k++) {
  1182. if (ux[k] === _ux && uy[k] === _uy
  1183. && vx[k] === _vx && vy[k] === _vy) {
  1184. row.push(k);
  1185. break;
  1186. }
  1187. }
  1188. }
  1189. }
  1190. for (var i = 0; i < 16; i++) {
  1191. var mat = new Matrix();
  1192. mat.set(ux[i], uy[i], vx[i], vy[i], 0, 0);
  1193. rotationMatrices.push(mat);
  1194. }
  1195. }
  1196. init();
  1197. /**
  1198. * @memberof PIXI
  1199. * @typedef {number} GD8Symmetry
  1200. * @see PIXI.groupD8
  1201. */
  1202. /**
  1203. * Implements the dihedral group D8, which is similar to
  1204. * [group D4]{@link http://mathworld.wolfram.com/DihedralGroupD4.html};
  1205. * D8 is the same but with diagonals, and it is used for texture
  1206. * rotations.
  1207. *
  1208. * The directions the U- and V- axes after rotation
  1209. * of an angle of `a: GD8Constant` are the vectors `(uX(a), uY(a))`
  1210. * and `(vX(a), vY(a))`. These aren't necessarily unit vectors.
  1211. *
  1212. * **Origin:**<br>
  1213. * This is the small part of gameofbombs.com portal system. It works.
  1214. * @see PIXI.groupD8.E
  1215. * @see PIXI.groupD8.SE
  1216. * @see PIXI.groupD8.S
  1217. * @see PIXI.groupD8.SW
  1218. * @see PIXI.groupD8.W
  1219. * @see PIXI.groupD8.NW
  1220. * @see PIXI.groupD8.N
  1221. * @see PIXI.groupD8.NE
  1222. * @author Ivan @ivanpopelyshev
  1223. * @namespace PIXI.groupD8
  1224. * @memberof PIXI
  1225. */
  1226. var groupD8 = {
  1227. /**
  1228. * | Rotation | Direction |
  1229. * |----------|-----------|
  1230. * | 0° | East |
  1231. * @memberof PIXI.groupD8
  1232. * @constant {PIXI.GD8Symmetry}
  1233. */
  1234. E: 0,
  1235. /**
  1236. * | Rotation | Direction |
  1237. * |----------|-----------|
  1238. * | 45°↻ | Southeast |
  1239. * @memberof PIXI.groupD8
  1240. * @constant {PIXI.GD8Symmetry}
  1241. */
  1242. SE: 1,
  1243. /**
  1244. * | Rotation | Direction |
  1245. * |----------|-----------|
  1246. * | 90°↻ | South |
  1247. * @memberof PIXI.groupD8
  1248. * @constant {PIXI.GD8Symmetry}
  1249. */
  1250. S: 2,
  1251. /**
  1252. * | Rotation | Direction |
  1253. * |----------|-----------|
  1254. * | 135°↻ | Southwest |
  1255. * @memberof PIXI.groupD8
  1256. * @constant {PIXI.GD8Symmetry}
  1257. */
  1258. SW: 3,
  1259. /**
  1260. * | Rotation | Direction |
  1261. * |----------|-----------|
  1262. * | 180° | West |
  1263. * @memberof PIXI.groupD8
  1264. * @constant {PIXI.GD8Symmetry}
  1265. */
  1266. W: 4,
  1267. /**
  1268. * | Rotation | Direction |
  1269. * |-------------|--------------|
  1270. * | -135°/225°↻ | Northwest |
  1271. * @memberof PIXI.groupD8
  1272. * @constant {PIXI.GD8Symmetry}
  1273. */
  1274. NW: 5,
  1275. /**
  1276. * | Rotation | Direction |
  1277. * |-------------|--------------|
  1278. * | -90°/270°↻ | North |
  1279. * @memberof PIXI.groupD8
  1280. * @constant {PIXI.GD8Symmetry}
  1281. */
  1282. N: 6,
  1283. /**
  1284. * | Rotation | Direction |
  1285. * |-------------|--------------|
  1286. * | -45°/315°↻ | Northeast |
  1287. * @memberof PIXI.groupD8
  1288. * @constant {PIXI.GD8Symmetry}
  1289. */
  1290. NE: 7,
  1291. /**
  1292. * Reflection about Y-axis.
  1293. * @memberof PIXI.groupD8
  1294. * @constant {PIXI.GD8Symmetry}
  1295. */
  1296. MIRROR_VERTICAL: 8,
  1297. /**
  1298. * Reflection about the main diagonal.
  1299. * @memberof PIXI.groupD8
  1300. * @constant {PIXI.GD8Symmetry}
  1301. */
  1302. MAIN_DIAGONAL: 10,
  1303. /**
  1304. * Reflection about X-axis.
  1305. * @memberof PIXI.groupD8
  1306. * @constant {PIXI.GD8Symmetry}
  1307. */
  1308. MIRROR_HORIZONTAL: 12,
  1309. /**
  1310. * Reflection about reverse diagonal.
  1311. * @memberof PIXI.groupD8
  1312. * @constant {PIXI.GD8Symmetry}
  1313. */
  1314. REVERSE_DIAGONAL: 14,
  1315. /**
  1316. * @memberof PIXI.groupD8
  1317. * @param {PIXI.GD8Symmetry} ind - sprite rotation angle.
  1318. * @returns {PIXI.GD8Symmetry} The X-component of the U-axis
  1319. * after rotating the axes.
  1320. */
  1321. uX: function (ind) { return ux[ind]; },
  1322. /**
  1323. * @memberof PIXI.groupD8
  1324. * @param {PIXI.GD8Symmetry} ind - sprite rotation angle.
  1325. * @returns {PIXI.GD8Symmetry} The Y-component of the U-axis
  1326. * after rotating the axes.
  1327. */
  1328. uY: function (ind) { return uy[ind]; },
  1329. /**
  1330. * @memberof PIXI.groupD8
  1331. * @param {PIXI.GD8Symmetry} ind - sprite rotation angle.
  1332. * @returns {PIXI.GD8Symmetry} The X-component of the V-axis
  1333. * after rotating the axes.
  1334. */
  1335. vX: function (ind) { return vx[ind]; },
  1336. /**
  1337. * @memberof PIXI.groupD8
  1338. * @param {PIXI.GD8Symmetry} ind - sprite rotation angle.
  1339. * @returns {PIXI.GD8Symmetry} The Y-component of the V-axis
  1340. * after rotating the axes.
  1341. */
  1342. vY: function (ind) { return vy[ind]; },
  1343. /**
  1344. * @memberof PIXI.groupD8
  1345. * @param {PIXI.GD8Symmetry} rotation - symmetry whose opposite
  1346. * is needed. Only rotations have opposite symmetries while
  1347. * reflections don't.
  1348. * @returns {PIXI.GD8Symmetry} The opposite symmetry of `rotation`
  1349. */
  1350. inv: function (rotation) {
  1351. if (rotation & 8) // true only if between 8 & 15 (reflections)
  1352. {
  1353. return rotation & 15; // or rotation % 16
  1354. }
  1355. return (-rotation) & 7; // or (8 - rotation) % 8
  1356. },
  1357. /**
  1358. * Composes the two D8 operations.
  1359. *
  1360. * Taking `^` as reflection:
  1361. *
  1362. * | | E=0 | S=2 | W=4 | N=6 | E^=8 | S^=10 | W^=12 | N^=14 |
  1363. * |-------|-----|-----|-----|-----|------|-------|-------|-------|
  1364. * | E=0 | E | S | W | N | E^ | S^ | W^ | N^ |
  1365. * | S=2 | S | W | N | E | S^ | W^ | N^ | E^ |
  1366. * | W=4 | W | N | E | S | W^ | N^ | E^ | S^ |
  1367. * | N=6 | N | E | S | W | N^ | E^ | S^ | W^ |
  1368. * | E^=8 | E^ | N^ | W^ | S^ | E | N | W | S |
  1369. * | S^=10 | S^ | E^ | N^ | W^ | S | E | N | W |
  1370. * | W^=12 | W^ | S^ | E^ | N^ | W | S | E | N |
  1371. * | N^=14 | N^ | W^ | S^ | E^ | N | W | S | E |
  1372. *
  1373. * [This is a Cayley table]{@link https://en.wikipedia.org/wiki/Cayley_table}
  1374. * @memberof PIXI.groupD8
  1375. * @param {PIXI.GD8Symmetry} rotationSecond - Second operation, which
  1376. * is the row in the above cayley table.
  1377. * @param {PIXI.GD8Symmetry} rotationFirst - First operation, which
  1378. * is the column in the above cayley table.
  1379. * @returns {PIXI.GD8Symmetry} Composed operation
  1380. */
  1381. add: function (rotationSecond, rotationFirst) { return (rotationCayley[rotationSecond][rotationFirst]); },
  1382. /**
  1383. * Reverse of `add`.
  1384. * @memberof PIXI.groupD8
  1385. * @param {PIXI.GD8Symmetry} rotationSecond - Second operation
  1386. * @param {PIXI.GD8Symmetry} rotationFirst - First operation
  1387. * @returns {PIXI.GD8Symmetry} Result
  1388. */
  1389. sub: function (rotationSecond, rotationFirst) { return (rotationCayley[rotationSecond][groupD8.inv(rotationFirst)]); },
  1390. /**
  1391. * Adds 180 degrees to rotation, which is a commutative
  1392. * operation.
  1393. * @memberof PIXI.groupD8
  1394. * @param {number} rotation - The number to rotate.
  1395. * @returns {number} Rotated number
  1396. */
  1397. rotate180: function (rotation) { return rotation ^ 4; },
  1398. /**
  1399. * Checks if the rotation angle is vertical, i.e. south
  1400. * or north. It doesn't work for reflections.
  1401. * @memberof PIXI.groupD8
  1402. * @param {PIXI.GD8Symmetry} rotation - The number to check.
  1403. * @returns {boolean} Whether or not the direction is vertical
  1404. */
  1405. isVertical: function (rotation) { return (rotation & 3) === 2; },
  1406. /**
  1407. * Approximates the vector `V(dx,dy)` into one of the
  1408. * eight directions provided by `groupD8`.
  1409. * @memberof PIXI.groupD8
  1410. * @param {number} dx - X-component of the vector
  1411. * @param {number} dy - Y-component of the vector
  1412. * @returns {PIXI.GD8Symmetry} Approximation of the vector into
  1413. * one of the eight symmetries.
  1414. */
  1415. byDirection: function (dx, dy) {
  1416. if (Math.abs(dx) * 2 <= Math.abs(dy)) {
  1417. if (dy >= 0) {
  1418. return groupD8.S;
  1419. }
  1420. return groupD8.N;
  1421. }
  1422. else if (Math.abs(dy) * 2 <= Math.abs(dx)) {
  1423. if (dx > 0) {
  1424. return groupD8.E;
  1425. }
  1426. return groupD8.W;
  1427. }
  1428. else if (dy > 0) {
  1429. if (dx > 0) {
  1430. return groupD8.SE;
  1431. }
  1432. return groupD8.SW;
  1433. }
  1434. else if (dx > 0) {
  1435. return groupD8.NE;
  1436. }
  1437. return groupD8.NW;
  1438. },
  1439. /**
  1440. * Helps sprite to compensate texture packer rotation.
  1441. * @memberof PIXI.groupD8
  1442. * @param {PIXI.Matrix} matrix - sprite world matrix
  1443. * @param {PIXI.GD8Symmetry} rotation - The rotation factor to use.
  1444. * @param {number} tx - sprite anchoring
  1445. * @param {number} ty - sprite anchoring
  1446. */
  1447. matrixAppendRotationInv: function (matrix, rotation, tx, ty) {
  1448. if (tx === void 0) { tx = 0; }
  1449. if (ty === void 0) { ty = 0; }
  1450. // Packer used "rotation", we use "inv(rotation)"
  1451. var mat = rotationMatrices[groupD8.inv(rotation)];
  1452. mat.tx = tx;
  1453. mat.ty = ty;
  1454. matrix.append(mat);
  1455. },
  1456. };
  1457. /**
  1458. * Transform that takes care about its versions.
  1459. * @memberof PIXI
  1460. */
  1461. var Transform = /** @class */ (function () {
  1462. function Transform() {
  1463. this.worldTransform = new Matrix();
  1464. this.localTransform = new Matrix();
  1465. this.position = new ObservablePoint(this.onChange, this, 0, 0);
  1466. this.scale = new ObservablePoint(this.onChange, this, 1, 1);
  1467. this.pivot = new ObservablePoint(this.onChange, this, 0, 0);
  1468. this.skew = new ObservablePoint(this.updateSkew, this, 0, 0);
  1469. this._rotation = 0;
  1470. this._cx = 1;
  1471. this._sx = 0;
  1472. this._cy = 0;
  1473. this._sy = 1;
  1474. this._localID = 0;
  1475. this._currentLocalID = 0;
  1476. this._worldID = 0;
  1477. this._parentID = 0;
  1478. }
  1479. /** Called when a value changes. */
  1480. Transform.prototype.onChange = function () {
  1481. this._localID++;
  1482. };
  1483. /** Called when the skew or the rotation changes. */
  1484. Transform.prototype.updateSkew = function () {
  1485. this._cx = Math.cos(this._rotation + this.skew.y);
  1486. this._sx = Math.sin(this._rotation + this.skew.y);
  1487. this._cy = -Math.sin(this._rotation - this.skew.x); // cos, added PI/2
  1488. this._sy = Math.cos(this._rotation - this.skew.x); // sin, added PI/2
  1489. this._localID++;
  1490. };
  1491. Transform.prototype.toString = function () {
  1492. return "[@pixi/math:Transform "
  1493. + ("position=(" + this.position.x + ", " + this.position.y + ") ")
  1494. + ("rotation=" + this.rotation + " ")
  1495. + ("scale=(" + this.scale.x + ", " + this.scale.y + ") ")
  1496. + ("skew=(" + this.skew.x + ", " + this.skew.y + ") ")
  1497. + "]";
  1498. };
  1499. /** Updates the local transformation matrix. */
  1500. Transform.prototype.updateLocalTransform = function () {
  1501. var lt = this.localTransform;
  1502. if (this._localID !== this._currentLocalID) {
  1503. // get the matrix values of the displayobject based on its transform properties..
  1504. lt.a = this._cx * this.scale.x;
  1505. lt.b = this._sx * this.scale.x;
  1506. lt.c = this._cy * this.scale.y;
  1507. lt.d = this._sy * this.scale.y;
  1508. lt.tx = this.position.x - ((this.pivot.x * lt.a) + (this.pivot.y * lt.c));
  1509. lt.ty = this.position.y - ((this.pivot.x * lt.b) + (this.pivot.y * lt.d));
  1510. this._currentLocalID = this._localID;
  1511. // force an update..
  1512. this._parentID = -1;
  1513. }
  1514. };
  1515. /**
  1516. * Updates the local and the world transformation matrices.
  1517. * @param parentTransform - The parent transform
  1518. */
  1519. Transform.prototype.updateTransform = function (parentTransform) {
  1520. var lt = this.localTransform;
  1521. if (this._localID !== this._currentLocalID) {
  1522. // get the matrix values of the displayobject based on its transform properties..
  1523. lt.a = this._cx * this.scale.x;
  1524. lt.b = this._sx * this.scale.x;
  1525. lt.c = this._cy * this.scale.y;
  1526. lt.d = this._sy * this.scale.y;
  1527. lt.tx = this.position.x - ((this.pivot.x * lt.a) + (this.pivot.y * lt.c));
  1528. lt.ty = this.position.y - ((this.pivot.x * lt.b) + (this.pivot.y * lt.d));
  1529. this._currentLocalID = this._localID;
  1530. // force an update..
  1531. this._parentID = -1;
  1532. }
  1533. if (this._parentID !== parentTransform._worldID) {
  1534. // concat the parent matrix with the objects transform.
  1535. var pt = parentTransform.worldTransform;
  1536. var wt = this.worldTransform;
  1537. wt.a = (lt.a * pt.a) + (lt.b * pt.c);
  1538. wt.b = (lt.a * pt.b) + (lt.b * pt.d);
  1539. wt.c = (lt.c * pt.a) + (lt.d * pt.c);
  1540. wt.d = (lt.c * pt.b) + (lt.d * pt.d);
  1541. wt.tx = (lt.tx * pt.a) + (lt.ty * pt.c) + pt.tx;
  1542. wt.ty = (lt.tx * pt.b) + (lt.ty * pt.d) + pt.ty;
  1543. this._parentID = parentTransform._worldID;
  1544. // update the id of the transform..
  1545. this._worldID++;
  1546. }
  1547. };
  1548. /**
  1549. * Decomposes a matrix and sets the transforms properties based on it.
  1550. * @param matrix - The matrix to decompose
  1551. */
  1552. Transform.prototype.setFromMatrix = function (matrix) {
  1553. matrix.decompose(this);
  1554. this._localID++;
  1555. };
  1556. Object.defineProperty(Transform.prototype, "rotation", {
  1557. /** The rotation of the object in radians. */
  1558. get: function () {
  1559. return this._rotation;
  1560. },
  1561. set: function (value) {
  1562. if (this._rotation !== value) {
  1563. this._rotation = value;
  1564. this.updateSkew();
  1565. }
  1566. },
  1567. enumerable: false,
  1568. configurable: true
  1569. });
  1570. /** A default (identity) transform. */
  1571. Transform.IDENTITY = new Transform();
  1572. return Transform;
  1573. }());
  1574. exports.Circle = Circle;
  1575. exports.DEG_TO_RAD = DEG_TO_RAD;
  1576. exports.Ellipse = Ellipse;
  1577. exports.Matrix = Matrix;
  1578. exports.ObservablePoint = ObservablePoint;
  1579. exports.PI_2 = PI_2;
  1580. exports.Point = Point;
  1581. exports.Polygon = Polygon;
  1582. exports.RAD_TO_DEG = RAD_TO_DEG;
  1583. exports.Rectangle = Rectangle;
  1584. exports.RoundedRectangle = RoundedRectangle;
  1585. exports.Transform = Transform;
  1586. exports.groupD8 = groupD8;
  1587. //# sourceMappingURL=math.js.map