math.js 63 KB

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