Files
nothoughts/node_modules/pixi.js/lib/maths/shapes/Triangle.mjs.map
2025-08-04 18:57:35 +02:00

1 line
8.8 KiB
Plaintext

{"version":3,"file":"Triangle.mjs","sources":["../../../src/maths/shapes/Triangle.ts"],"sourcesContent":["import { squaredDistanceToLineSegment } from '../misc/squaredDistanceToLineSegment';\nimport { Rectangle } from './Rectangle';\n\nimport type { SHAPE_PRIMITIVE } from '../misc/const';\nimport type { ShapePrimitive } from './ShapePrimitive';\n\n/**\n * A class to define a shape of a triangle via user defined coordinates.\n *\n * Create a `Triangle` object with the `x`, `y`, `x2`, `y2`, `x3`, `y3` properties.\n *\n * ```js\n * import { Triangle } from 'pixi.js';\n *\n * const triangle = new Triangle(0, 0, 100, 0, 50, 50);\n * ```\n * @memberof maths\n */\nexport class Triangle implements ShapePrimitive\n{\n /**\n * The type of the object, mainly used to avoid `instanceof` checks\n * @default 'triangle'\n */\n public readonly type: SHAPE_PRIMITIVE = 'triangle';\n\n /**\n * The X coord of the first point.\n * @default 0\n */\n public x: number;\n /**\n * The Y coord of the first point.\n * @default 0\n */\n public y: number;\n /**\n * The X coord of the second point.\n * @default 0\n */\n public x2: number;\n /**\n * The Y coord of the second point.\n * @default 0\n */\n public y2: number;\n /**\n * The X coord of the third point.\n * @default 0\n */\n public x3: number;\n /**\n * The Y coord of the third point.\n * @default 0\n */\n public y3: number;\n\n /**\n * @param x - The X coord of the first point.\n * @param y - The Y coord of the first point.\n * @param x2 - The X coord of the second point.\n * @param y2 - The Y coord of the second point.\n * @param x3 - The X coord of the third point.\n * @param y3 - The Y coord of the third point.\n */\n constructor(x = 0, y = 0, x2 = 0, y2 = 0, x3 = 0, y3 = 0)\n {\n this.x = x;\n this.y = y;\n this.x2 = x2;\n this.y2 = y2;\n this.x3 = x3;\n this.y3 = y3;\n }\n\n /**\n * Checks whether the x and y coordinates given are contained within this triangle\n * @param x - The X coordinate of the point to test\n * @param y - The Y coordinate of the point to test\n * @returns Whether the x/y coordinates are within this Triangle\n */\n public contains(x: number, y: number): boolean\n {\n const s = ((this.x - this.x3) * (y - this.y3)) - ((this.y - this.y3) * (x - this.x3));\n const t = ((this.x2 - this.x) * (y - this.y)) - ((this.y2 - this.y) * (x - this.x));\n\n if ((s < 0) !== (t < 0) && s !== 0 && t !== 0)\n { return false; }\n\n const d = ((this.x3 - this.x2) * (y - this.y2)) - ((this.y3 - this.y2) * (x - this.x2));\n\n return d === 0 || (d < 0) === (s + t <= 0);\n }\n\n /**\n * Checks whether the x and y coordinates given are contained within this triangle including the stroke.\n * @param pointX - The X coordinate of the point to test\n * @param pointY - The Y coordinate of the point to test\n * @param strokeWidth - The width of the line to check\n * @returns Whether the x/y coordinates are within this triangle\n */\n public strokeContains(pointX: number, pointY: number, strokeWidth: number): boolean\n {\n const halfStrokeWidth = strokeWidth / 2;\n const halfStrokeWidthSquared = halfStrokeWidth * halfStrokeWidth;\n\n const { x, x2, x3, y, y2, y3 } = this;\n\n if (squaredDistanceToLineSegment(pointX, pointY, x, y, x2, y3) <= halfStrokeWidthSquared\n || squaredDistanceToLineSegment(pointX, pointY, x2, y2, x3, y3) <= halfStrokeWidthSquared\n || squaredDistanceToLineSegment(pointX, pointY, x3, y3, x, y) <= halfStrokeWidthSquared)\n {\n return true;\n }\n\n return false;\n }\n\n /**\n * Creates a clone of this Triangle\n * @returns a copy of the triangle\n */\n public clone(): ShapePrimitive\n {\n const triangle = new Triangle(\n this.x,\n this.y,\n this.x2,\n this.y2,\n this.x3,\n this.y3\n );\n\n return triangle;\n }\n\n /**\n * Copies another triangle to this one.\n * @param triangle - The triangle to copy from.\n * @returns Returns itself.\n */\n public copyFrom(triangle: Triangle): this\n {\n this.x = triangle.x;\n this.y = triangle.y;\n this.x2 = triangle.x2;\n this.y2 = triangle.y2;\n this.x3 = triangle.x3;\n this.y3 = triangle.y3;\n\n return this;\n }\n\n /**\n * Copies this triangle to another one.\n * @param triangle - The triangle to copy to.\n * @returns Returns given parameter.\n */\n public copyTo(triangle: Triangle): Triangle\n {\n triangle.copyFrom(this);\n\n return triangle;\n }\n\n /**\n * Returns the framing rectangle of the triangle as a Rectangle object\n * @param out - optional rectangle to store the result\n * @returns The framing rectangle\n */\n public getBounds(out?: Rectangle): Rectangle\n {\n out = out || new Rectangle();\n\n const minX = Math.min(this.x, this.x2, this.x3);\n const maxX = Math.max(this.x, this.x2, this.x3);\n const minY = Math.min(this.y, this.y2, this.y3);\n const maxY = Math.max(this.y, this.y2, this.y3);\n\n out.x = minX;\n out.y = minY;\n out.width = maxX - minX;\n out.height = maxY - minY;\n\n return out;\n }\n}\n"],"names":[],"mappings":";;;;AAkBO,MAAM,QACb,CAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EA8CI,WAAY,CAAA,CAAA,GAAI,CAAG,EAAA,CAAA,GAAI,CAAG,EAAA,EAAA,GAAK,CAAG,EAAA,EAAA,GAAK,CAAG,EAAA,EAAA,GAAK,CAAG,EAAA,EAAA,GAAK,CACvD,EAAA;AA1CA;AAAA;AAAA;AAAA;AAAA,IAAA,IAAA,CAAgB,IAAwB,GAAA,UAAA,CAAA;AA2CpC,IAAA,IAAA,CAAK,CAAI,GAAA,CAAA,CAAA;AACT,IAAA,IAAA,CAAK,CAAI,GAAA,CAAA,CAAA;AACT,IAAA,IAAA,CAAK,EAAK,GAAA,EAAA,CAAA;AACV,IAAA,IAAA,CAAK,EAAK,GAAA,EAAA,CAAA;AACV,IAAA,IAAA,CAAK,EAAK,GAAA,EAAA,CAAA;AACV,IAAA,IAAA,CAAK,EAAK,GAAA,EAAA,CAAA;AAAA,GACd;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQO,QAAA,CAAS,GAAW,CAC3B,EAAA;AACI,IAAA,MAAM,CAAM,GAAA,CAAA,IAAA,CAAK,CAAI,GAAA,IAAA,CAAK,EAAO,KAAA,CAAA,GAAI,IAAK,CAAA,EAAA,CAAA,GAAA,CAAS,IAAK,CAAA,CAAA,GAAI,IAAK,CAAA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