UNPKG

pixi.js

Version:

<p align="center"> <a href="https://pixijs.com" target="_blank" rel="noopener noreferrer"> <img height="150" src="https://files.pixijs.download/branding/pixijs-logo-transparent-dark.svg?v=1" alt="PixiJS logo"> </a> </p> <br/> <p align="center">

1 lines 2.75 kB
{"version":3,"file":"pointInTriangle.mjs","sources":["../../../src/maths/point/pointInTriangle.ts"],"sourcesContent":["/**\n * Check if a point is inside a triangle.\n * @param px - x coordinate of the point\n * @param py - y coordinate of the point\n * @param x1 - x coordinate of the first vertex of the triangle\n * @param y1 - y coordinate of the first vertex of the triangle\n * @param x2 - x coordinate of the second vertex of the triangle\n * @param y2 - y coordinate of the second vertex of the triangle\n * @param x3 - x coordinate of the third vertex of the triangle\n * @param y3 - y coordinate of the third vertex of the triangle\n * @returns `true` if the point is inside the triangle, `false` otherwise\n * @category maths\n * @internal\n */\nexport function pointInTriangle(\n px: number, py: number,\n x1: number, y1: number,\n x2: number, y2: number,\n x3: number, y3: number\n)\n{\n // Calculate vectors from point p to each vertex of the triangle\n const v2x = x3 - x1;\n const v2y = y3 - y1;\n const v1x = x2 - x1;\n const v1y = y2 - y1;\n const v0x = px - x1;\n const v0y = py - y1;\n\n // Compute dot products\n const dot00 = (v2x * v2x) + (v2y * v2y);\n const dot01 = (v2x * v1x) + (v2y * v1y);\n const dot02 = (v2x * v0x) + (v2y * v0y);\n const dot11 = (v1x * v1x) + (v1y * v1y);\n const dot12 = (v1x * v0x) + (v1y * v0y);\n\n // Calculate barycentric coordinates\n const invDenom = 1 / ((dot00 * dot11) - (dot01 * dot01));\n const u = ((dot11 * dot02) - (dot01 * dot12)) * invDenom;\n const v = ((dot00 * dot12) - (dot01 * dot02)) * invDenom;\n\n // Check if point is in triangle\n return (u >= 0) && (v >= 0) && (u + v < 1);\n}\n"],"names":[],"mappings":";AAcgB,SAAA,eAAA,CACZ,IAAY,EACZ,EAAA,EAAA,EAAY,IACZ,EAAY,EAAA,EAAA,EACZ,IAAY,EAEhB,EAAA;AAEI,EAA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