math.mjs 55 KB

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