I’m trying to calculate an axis-aligned bounding box of a circle that is transformed through a 2D affine matrix that can contain any operations (translation, rotation, scale, skew).
I think that good approach would be to transform the circle with the matrix and obtain a rotated ellipse, but I don’t know how to do it properly. What I’ve so far is a code that can calculate a bounding box of a rotated ellipse (not attached) and a code that partially can transform circle to an ellipse (center and radiuses, but ellipse missing rotation):
transformCircle(in matrix, in circle, out ellipse) {
ellipse.center = matrix * circle.center
// Calculate conjugated half-diameters
f1 = matrix * vec2(circle.radius, 0)
f2 = matrix * vec2(0, circle.radius)
m = f1.x^2 + f1.y^2 + f2.x^2 + f2.y^2
n = cross(b, c)
p = sqrt(m + 2*n)
q = sqrt(m - 2*n)
ellipse.radiusX = 0.5 * (p + q);
ellipse.radiusY = 0.5 * (p - q);
ellipse.rotation = ???; // Need help here
}
My questions would be:
- How to calculate ellipse rotation from conjugated half-diameters?
- Alternatively how to calculate AABB of circle transformed by a 2D matrix without ellipses, if possible?