There are two rectangles: $r_1$ and $r_2$. $r_1$ is rotated $\theta$ and then uniformly scaled by a factor $k$ to exactly fit within $r_2$. I'm trying to find the value of $\theta$ that maximizes $k$, i.e. the angle at which $r_1$ can be inscribed the largest within $r_2$.
In this example, $r_1$ is $(5,1)$ and $r_2$ is $(20, 20)$. The result shows how $k$ is maximized when $\theta=45^\circ$. I've been able to approximate solutions like this by brute-forcing $\theta$ through the interval $[0, 90^\circ]$, but I'm looking for a more elegant mathematical approach. So far I've determined the relationship between $r_1=(w,h)$ and its transformed version $r_t$:
$$ r_t=k(w\cos(\theta)+h\sin(\theta),w\sin(\theta)+h\cos(\theta))) $$
However, I'm having trouble turning this into a way to calculate $\theta$. How can I find the optimal value without brute-forcing?