Rectangle Intersection Test (with C#)
Rectangle Intersection Test (with C#) by Sebastian Krysmanski
http://manski.net/2011/05/rectangle-intersection-test-with-csharp/
/// <summary> /// Does axis separation test for a convex quadrilateral. /// </summary> /// <param name="x1">Defines together with x2 the edge of quad1 to be checked whether its a separating axis.</param> /// <param name="x2">Defines together with x1 the edge of quad1 to be checked whether its a separating axis.</param> /// <param name="x3">One of the remaining two points of quad1.</param> /// <param name="otherQuadPoints">The four points of the other quad.</param> /// <returns>Returns <c>true</c>, if the specified edge is a separating axis (and the quadrilaterals therefor don't /// intersect). Returns <c>false</c>, if it's not a separating axis.</returns> bool DoAxisSeparationTest(Point x1, Point x2, Point x3, Point[] otherQuadPoints) { Vector vec = x2 - x1; Vector rotated = new Vector(-vec.Y, vec.X); bool refSide = (rotated.X * (x3.X - x1.X) + rotated.Y * (x3.Y - x1.Y)) >= 0; foreach (Point pt in otherQuadPoints) { bool side = (rotated.X * (pt.X - x1.X) + rotated.Y * (pt.Y - x1.Y)) >= 0; if (side == refSide) { // At least one point of the other quad is one the same side as x3. Therefor the specified edge can't be a // separating axis anymore. return false; } } // All points of the other quad are on the other side of the edge. Therefor the edge is a separating axis and // the quads don't intersect. return true; }