def rotated_rect_with_max_area()

in augly/image/utils/utils.py [0:0]


def rotated_rect_with_max_area(w: int, h: int, angle: float) -> Tuple[float, float]:
    """
    Computes the width and height of the largest possible axis-aligned
    rectangle (maximal area) within the rotated rectangle

    source:
    https://stackoverflow.com/questions/16702966/rotate-image-and-crop-out-black-borders # noqa: B950
    """
    width_is_longer = w >= h
    side_long, side_short = (w, h) if width_is_longer else (h, w)

    sin_a = abs(math.sin(math.radians(angle)))
    cos_a = abs(math.cos(math.radians(angle)))
    if side_short <= 2.0 * sin_a * cos_a * side_long or abs(sin_a - cos_a) < 1e-10:
        x = 0.5 * side_short
        wr, hr = (x / sin_a, x / cos_a) if width_is_longer else (x / cos_a, x / sin_a)
    else:
        cos_2a = cos_a * cos_a - sin_a * sin_a
        wr = (w * cos_a - h * sin_a) / cos_2a
        hr = (h * cos_a - w * sin_a) / cos_2a
    return wr, hr