Ignoring the rotation thing, and focusing on how we define a circle and a rectangle, they are perhaps not so different. A circle is defined by a point in 2D space, and a radius, where all the points along its edge are the same distance away from its centre. Okay, so a rectangle isn't exactly the same; a rectangle could be defined by an axis-aligned line which has a length and a width or height, if we were to define it in the same way as the circle, where all the points along its edge are the half its width or height away from the line, we wouldn't quite get a rectangle, we would get a capsule (or a 2D version of a capsule), so instead we need to define it with two axis-aligned lines, both going through its mid-point, and define its edges to be axis aligned too.
Why is this important? It is important for 2D collision detection (or more accurately, intersection testing). Consider the following code:
struct circle{
float x, y, r;
};
bool AreCirclesIntersecting(circle A, circle B)
{
// Calculate the square distance between A and B
float deltaX = abs(A.x - B.x);
float deltaY = abs(A.y - B.y);
float distance = deltaX * deltaX + deltaY * deltaY;
// Is the sum of the radii squared greater than the distance squared?
float sumRad = A.r + B.r;
return distance < sumRad * sumRad;
}
struct rect{
float x, y, w, h;
};
bool AreRectanglesIntersecting(rect A, rect B)
{
// Calculate the half width and height of each rectangle and find its midpoint
float hwA = A.w * 0.5f;
float hhA = A.h * 0.5f;
float hwB = B.w * 0.5f;
float hhB = B.h * 0.5f;
float aX = A.x + hwA;
float aY = A.y + hhA;
float bX = B.x + hwB;
float bY = B.y + hhB;
// Calculate the horizontal and vertical distances between the midpoints of A and B
float deltaX = abs(aX - bX);
float deltaY = abs(aY - bY);
// Are their combined widths greater than their horizontal distance apart,
// and are their combined heights greater than their vertical distance apart?
return deltaX < hwA + hwB && deltaY < hhA + hhB;
}
One thing you'll notice about the second one is a lot longer than the first. This is mainly due to the set-up. If you are going to be doing a lot of intersection tests, then instead of defining your rectangles by their top left point and width and height, perhaps you should define it by its midpoint and half width and half height.