arcade-physics
Version:
Use Arcade Physics without Phaser.
67 lines • 3.05 kB
JavaScript
"use strict";
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2020 Photon Storm Ltd.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const LineToLine_1 = __importDefault(require("./LineToLine"));
const Contains_1 = __importDefault(require("../rectangle/Contains"));
const ContainsArray_1 = __importDefault(require("../triangle/ContainsArray"));
const Decompose_1 = __importDefault(require("../rectangle/Decompose"));
/**
* Checks for intersection between Rectangle shape and Triangle shape.
*
* @function Phaser.Geom.Intersects.RectangleToTriangle
* @since 3.0.0
*
* @param {Phaser.Geom.Rectangle} rect - Rectangle object to test.
* @param {Phaser.Geom.Triangle} triangle - Triangle object to test.
*
* @return {boolean} A value of `true` if objects intersect; otherwise `false`.
*/
const RectangleToTriangle = (rect, triangle) => {
// First the cheapest ones:
if (triangle.left > rect.right ||
triangle.right < rect.left ||
triangle.top > rect.bottom ||
triangle.bottom < rect.top) {
return false;
}
const triA = triangle.getLineA();
const triB = triangle.getLineB();
const triC = triangle.getLineC();
// Are any of the triangle points within the rectangle?
if ((0, Contains_1.default)(rect, triA.x1, triA.y1) || (0, Contains_1.default)(rect, triA.x2, triA.y2)) {
return true;
}
if ((0, Contains_1.default)(rect, triB.x1, triB.y1) || (0, Contains_1.default)(rect, triB.x2, triB.y2)) {
return true;
}
if ((0, Contains_1.default)(rect, triC.x1, triC.y1) || (0, Contains_1.default)(rect, triC.x2, triC.y2)) {
return true;
}
// Cheap tests over, now to see if any of the lines intersect ...
const rectA = rect.getLineA();
const rectB = rect.getLineB();
const rectC = rect.getLineC();
const rectD = rect.getLineD();
if ((0, LineToLine_1.default)(triA, rectA) || (0, LineToLine_1.default)(triA, rectB) || (0, LineToLine_1.default)(triA, rectC) || (0, LineToLine_1.default)(triA, rectD)) {
return true;
}
if ((0, LineToLine_1.default)(triB, rectA) || (0, LineToLine_1.default)(triB, rectB) || (0, LineToLine_1.default)(triB, rectC) || (0, LineToLine_1.default)(triB, rectD)) {
return true;
}
if ((0, LineToLine_1.default)(triC, rectA) || (0, LineToLine_1.default)(triC, rectB) || (0, LineToLine_1.default)(triC, rectC) || (0, LineToLine_1.default)(triC, rectD)) {
return true;
}
// None of the lines intersect, so are any rectangle points within the triangle?
const points = (0, Decompose_1.default)(rect);
const within = (0, ContainsArray_1.default)(triangle, points, true);
return within.length > 0;
};
exports.default = RectangleToTriangle;
//# sourceMappingURL=RectangleToTriangle.js.map