phaser/src/geom/triangle/Contains.js

48 lines
1.5 KiB
JavaScript
Raw Normal View History

2018-02-12 16:01:20 +00:00
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2018 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
// http://www.blackpawn.com/texts/pointinpoly/
2017-10-13 13:11:54 +00:00
/**
2018-10-22 11:12:31 +00:00
* Checks if a point (as a pair of coordinates) is inside a Triangle's bounds.
2017-10-13 13:11:54 +00:00
*
* @function Phaser.Geom.Triangle.Contains
* @since 3.0.0
*
2018-10-22 11:12:31 +00:00
* @param {Phaser.Geom.Triangle} triangle - The Triangle to check.
* @param {number} x - The X coordinate of the point to check.
* @param {number} y - The Y coordinate of the point to check.
2017-10-13 13:11:54 +00:00
*
2018-10-22 11:12:31 +00:00
* @return {boolean} `true` if the point is inside the Triangle, otherwise `false`.
2017-10-13 13:11:54 +00:00
*/
var Contains = function (triangle, x, y)
{
var v0x = triangle.x3 - triangle.x1;
var v0y = triangle.y3 - triangle.y1;
var v1x = triangle.x2 - triangle.x1;
var v1y = triangle.y2 - triangle.y1;
var v2x = x - triangle.x1;
var v2y = y - triangle.y1;
var dot00 = (v0x * v0x) + (v0y * v0y);
var dot01 = (v0x * v1x) + (v0y * v1y);
var dot02 = (v0x * v2x) + (v0y * v2y);
var dot11 = (v1x * v1x) + (v1y * v1y);
var dot12 = (v1x * v2x) + (v1y * v2y);
// Compute barycentric coordinates
var b = ((dot00 * dot11) - (dot01 * dot01));
var inv = (b === 0) ? 0 : (1 / b);
var u = ((dot11 * dot02) - (dot01 * dot12)) * inv;
var v = ((dot00 * dot12) - (dot01 * dot02)) * inv;
return (u >= 0 && v >= 0 && (u + v < 1));
};
module.exports = Contains;