mirror of
https://github.com/photonstorm/phaser
synced 2024-11-27 07:01:20 +00:00
Added Rectangle to Triangle, and Triangle to Triangle intersection functions. Detects for face intersection and also point within point (so a triangle fully enclosed inside a rectangle for example)
This commit is contained in:
parent
4914895509
commit
3c12e15054
5 changed files with 143 additions and 2 deletions
|
@ -1,4 +1,4 @@
|
|||
var CHECKSUM = {
|
||||
build: 'dc876af0-18bc-11e7-88e3-6f7931ffc17c'
|
||||
build: 'e1a4b350-18c9-11e7-9368-79f8c4c534b2'
|
||||
};
|
||||
module.exports = CHECKSUM;
|
60
v3/src/geom/intersects/RectangleToTriangle.js
Normal file
60
v3/src/geom/intersects/RectangleToTriangle.js
Normal file
|
@ -0,0 +1,60 @@
|
|||
|
||||
var LineToLine = require('./LineToLine');
|
||||
var Contains = require('../rectangle/Contains');
|
||||
var ContainsArray = require('../triangle/ContainsArray');
|
||||
var Decompose = require('../rectangle/Decompose');
|
||||
|
||||
var RectangleToTriangle = function (rect, triangle)
|
||||
{
|
||||
var triA = triangle.getLineA();
|
||||
var triB = triangle.getLineB();
|
||||
var triC = triangle.getLineC();
|
||||
|
||||
// Are any of the triangle points within the rectangle?
|
||||
|
||||
if (Contains(rect, triA.x1, triA.y1) || Contains(rect, triA.x2, triA.y2))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (Contains(rect, triB.x1, triA.y1) || Contains(rect, triB.x2, triA.y2))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (Contains(rect, triC.x1, triA.y1) || Contains(rect, triC.x2, triA.y2))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
// Cheap tests over, no to see if any of the lines intersect ...
|
||||
|
||||
var rectA = rect.getLineA();
|
||||
var rectB = rect.getLineB();
|
||||
var rectC = rect.getLineC();
|
||||
var rectD = rect.getLineD();
|
||||
|
||||
if (LineToLine(triA, rectA) || LineToLine(triA, rectB) || LineToLine(triA, rectC) || LineToLine(triA, rectD))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (LineToLine(triB, rectA) || LineToLine(triB, rectB) || LineToLine(triB, rectC) || LineToLine(triB, rectD))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (LineToLine(triC, rectA) || LineToLine(triC, rectB) || LineToLine(triC, rectC) || LineToLine(triC, rectD))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
// None of the lines intersect, so are any rectangle points within the triangle?
|
||||
|
||||
var points = Decompose(rect);
|
||||
var within = ContainsArray(triangle, points, true);
|
||||
|
||||
return (within.length > 0);
|
||||
};
|
||||
|
||||
module.exports = RectangleToTriangle;
|
55
v3/src/geom/intersects/TriangleToTriangle.js
Normal file
55
v3/src/geom/intersects/TriangleToTriangle.js
Normal file
|
@ -0,0 +1,55 @@
|
|||
|
||||
var LineToLine = require('./LineToLine');
|
||||
var ContainsArray = require('../triangle/ContainsArray');
|
||||
var Decompose = require('../triangle/Decompose');
|
||||
|
||||
var TriangleToTriangle = function (triangleA, triangleB)
|
||||
{
|
||||
var lineAA = triangleA.getLineA();
|
||||
var lineAB = triangleA.getLineB();
|
||||
var lineAC = triangleA.getLineC();
|
||||
|
||||
var lineBA = triangleB.getLineA();
|
||||
var lineBB = triangleB.getLineB();
|
||||
var lineBC = triangleB.getLineC();
|
||||
|
||||
// Now check the lines against each line of TriangleB
|
||||
if (LineToLine(lineAA, lineBA) || LineToLine(lineAA, lineBB) || LineToLine(lineAA, lineBC))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (LineToLine(lineAB, lineBA) || LineToLine(lineAB, lineBB) || LineToLine(lineAB, lineBC))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (LineToLine(lineAC, lineBA) || LineToLine(lineAC, lineBB) || LineToLine(lineAC, lineBC))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
// Nope, so check to see if any of the points of triangleA are within triangleB
|
||||
|
||||
var points = Decompose(triangleA);
|
||||
var within = ContainsArray(triangleB, points, true);
|
||||
|
||||
if (within.length > 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
// Finally check to see if any of the points of triangleB are within triangleA
|
||||
|
||||
points = Decompose(triangleB);
|
||||
within = ContainsArray(triangleA, points, true);
|
||||
|
||||
if (within.length > 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
module.exports = TriangleToTriangle;
|
|
@ -11,8 +11,10 @@ module.exports = {
|
|||
PointToLine: require('./PointToLine'),
|
||||
PointToLineSegment: require('./PointToLineSegment'),
|
||||
RectangleToRectangle: require('./RectangleToRectangle'),
|
||||
RectangleToTriangle: require('./RectangleToTriangle'),
|
||||
RectangleToValues: require('./RectangleToValues'),
|
||||
TriangleToCircle: require('./TriangleToCircle'),
|
||||
TriangleToLine: require('./TriangleToLine')
|
||||
TriangleToLine: require('./TriangleToLine'),
|
||||
TriangleToTriangle: require('./TriangleToTriangle')
|
||||
|
||||
};
|
||||
|
|
|
@ -60,6 +60,30 @@ Rectangle.prototype = {
|
|||
isEmpty: function ()
|
||||
{
|
||||
return (this.width <= 0 || this.height <= 0);
|
||||
},
|
||||
|
||||
// TOP
|
||||
getLineA: function ()
|
||||
{
|
||||
return { x1: this.x, y1: this.y, x2: this.right, y2: this.y };
|
||||
},
|
||||
|
||||
// RIGHT
|
||||
getLineB: function ()
|
||||
{
|
||||
return { x1: this.right, y1: this.y, x2: this.right, y2: this.bottom };
|
||||
},
|
||||
|
||||
// BOTTOM
|
||||
getLineC: function ()
|
||||
{
|
||||
return { x1: this.right, y1: this.bottom, x2: this.x, y2: this.bottom };
|
||||
},
|
||||
|
||||
// LEFT
|
||||
getLineD: function ()
|
||||
{
|
||||
return { x1: this.x, y1: this.bottom, x2: this.x, y2: this.y };
|
||||
}
|
||||
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue