Compare your rectangle (or square) with each edge of the triangle, taking triangular vertices and constructing a line equation for each edge, with sequential ordering (clockwise or counterclockwise around the triangle).
If the rectangle is completely outside the triangle on any edge, it does not intersect.
Repeat the test with the edges of the rectangle relative to the triangle.
There is a potential opportunity to increase productivity, knowing that the rectangle is aligned along the axis, since you can determine which angle is most likely to be inside the triangle, and check only this one, and not test all four corners.
Regardless of whether it wins, it depends on the implementation. Sometimes it can be faster to blindly check the four coordinates, rather than actually calculate the best.
Testing a triangle for a rectangle should be simpler, because linear equations are simple tests against x or y when the rectangle is aligned on the axis.
This is a generalized form of analysis of the dividing axis - the search for a line or plane that separates two objects, which proves that they cannot intersect. If you need better performance, you can find the nearest functions of two objects to develop the most suitable line / plane for use, and not for everyone.
Jasond
source share