# Calculate total area of a composition of images

I'm trying to calculate the total area of a game object composed of several images. For each image, I know:

• Width, Height
• X, Y
• Rotation
• ScaleX, ScaleY
• OriginX, OriginY

Where OriginX/Y are floats in the range [0, 1] indicating the relative point inside the image. Also, this is the center of the rotation and the location where the image is placed regarding the X/Y coodrinates.

I have this algorithm which works fine without taking the rotation into account.

float left = float.MaxValue;
float right = float.MinValue;
float bottom = float.MaxValue;
float top = float.MinValue;

foreach (Image i in images)
{
float w = i.Width * i.ScaleX;
float h = i.Height * i.ScaleY;

float l = i.X - i.OriginX * w;
float r = l + w;
float b = i.Y - i.OriginY * h;
float t = b + h;

left = Math.Min(left, l);
right = Math.Max(right, r);
bottom = Math.Min(bottom, b);
top = Math.Max(top, t);
}

float totalW = right - left;
float totalH = top - bottom;


What do I have to do to take rotation into account?

I know how to calculate the area of the rotated rectangle but don't know how to take the pivot point into account.

Also, I know this can be achieved easily by using transform matrices but this is going to be a part of a standalone library with no dependencies (including a math library with matrices).

EDIT

My goal is to compute the axis-aligned bounding box of the whole composition.

• The use of area is a bit ambiguous. Are you just looking for the axis-aligned bounding box of the composite shape? – Lars Viklund Feb 23 '16 at 7:54
• Yes. That's a good point - I edited the answer to reflect that. – loodakrawa Feb 23 '16 at 22:40

A rotation around a pivot point is performed by translating the geometry so that the pivot coincides with the origin, rotating, and translating the same amount back.

This holds whether you use matrices or just write out the operations, that is:

x' = oX + cos(theta) * (x - oX) - sin(theta) * (y - oY)
y' = oY + sin(theta) * (x - oX) + cos(theta) * (y - oY)


is the literal expansion of v' = T(pivot) * R(theta) * T(-pivot) * v:

     | 1  0  oX |   | +cos(theta)  -sin(theta)  0 |   | 1  0  -oX |   | x |
v' = | 0  1  oY | * | +sin(theta)  +cos(theta)  0 | * | 0  1  -oY | * | y |
| 0  0  1  |   | 0            0            1 |   | 0  0  1   |   | 1 |


Depending on if your scale applies around the pivot point or the world origin, you either scale before the rotation, or scale your input vector. I'm going to assume you scale around the pivot point:

x' = oX + cos(theta) * sX * (x - oX) - sin(theta) * sY * (y - oY)
y' = oY + sin(theta) * sX * (x - oX) + cos(theta) * sY * (y - oY)


which is the literal expansion of v' = T(pivot) * R(theta) * S * T(-pivot) * v:

     | 1  0  oX |   | +cos(theta)  -sin(theta)  0 |   | sX 0  0 |   | 1  0  -oX |   | x |
v' = | 0  1  oY | * | +sin(theta)  +cos(theta)  0 | * | 0  sY 0 | * | 0  1  -oY | * | y |
| 0  0  1  |   | 0            0            1 |   | 0  0  1 |   | 0  0  1   |   | 1 |


As your goal is to compute the axis-aligned bounding box of the extents of all the components in the composite shape, compute all the corner points and take the min/max of X and Y.

• Makes sense. Could you please expand the answer to include the scale as well? – loodakrawa Feb 23 '16 at 22:42

Are they overlapping? Otherwise the total area is simply the sum of all images areas, where each image's area is equal to the respective w*h. Rotation doesn't affect area.

• I'm aware that the area doesn't change with rotation. As Lars commented, the use or area is a bit ambiguous but I think the chunk of code in my question made pretty clear that my intention was computing the AABB of the whole composition. – loodakrawa Feb 23 '16 at 22:44