Given a circle of diameter d inside a square of side length d, what is the length of a rotated diameter when extended to the square? My question is as follows:
Given a circle with diameter d inside a square of side length d, I'm trying to find the length between the intersection points on the square should the diameter of the circle be rotated about the center and the endpoints extended straight towards the bounding square. 
In the image below, the red line is the diameter when rotated. I want to find the length of the segment should it be extended in both directions to the square.

For example, should the rotation be 45 degrees, the line segment formed by the rotated diameter hits the square at two opposite corners as a diagonal, with length equal to sqrt(2 * d^2). The solution at multiples of 90 degrees should always be d.
I'm looking for a general case solution that works for any angle of rotation. My current attempts at a solution have all been met with failure so far; I'm probably overthinking the problem.
Current attempt (incorrect):
function GetTotalWallLength(seglen, angle) {
    let dx = absolute(seglen*cos(angle)); // Horizontal component
    let dy = absolute(seglen*sin(angle)); // Vertical component
    let extralen = (((seglen - dx)/2)^2 + ((seglen - dy)/2)^2) ^ 0.5;
    return seglen + extralen*2;
}

Any guidance (or a general case solution) would be greatly appreciated.
 A: The answer is
$$\frac{d}{\max\left( |\cos\theta|, |\sin\theta| \right)}$$
To understand why the answer has this form, let us look at a related problem.

Let's say we have a convex $n$-gon with origin in its interior. If we shoot a light ray from origin in direction $\vec{t} = (\cos\theta,\sin\theta)$, how far will the light travel before it hit the boundary?

For the $i^{th}$ edge of the polygon, let $\vec{n}_i = (\cos\theta_i,\sin\theta_i)$ be its outward pointing normal and $d_i$ be its distance to origin. The region bounded by the $n$-gon is
the collection of point $\vec{p} = (x,y)$ which satisfies the inequalities
$$\vec{p}\cdot \vec{n}_i = x\cos\theta_i + y \sin\theta_i \le d_i\quad\text{ for } 1 \le i \le n$$
For a point $\vec{p} = \lambda \vec{t} = \lambda (\cos\theta,\sin\theta)$  on the ray, this becomes
$$ \lambda (\cos\theta\cos\theta_i + \sin\theta\sin\theta_i) \le d_i
\quad\iff\quad \lambda \cos(\theta - \theta_i) \le d_i$$
for all $1 \le i \le n$. Since $\lambda > 0$, this is equivalent to
$$\frac{1}{\lambda} \ge \frac{1}{d_i}\cos(\theta - \theta_i)\;\text{ for all }i 
\quad\iff\quad \frac{1}{\lambda} \ge \max_{1\le i \le n}\left(\frac{1}{d_i}\cos(\theta - \theta_i)\right)$$
This implies the light will travel along direction $\vec{t}$ for a distance
$$\Lambda(\theta) \stackrel{def}{=} \frac{1}{\max\limits_{1\le i \le n}\left(\frac{1}{d_i}\cos(\theta - \theta_i)\right)}$$
before it hit the boundary.
Back to problem at hand. If we choose a coordinate system to make the square centered at origin with sides parallel to $x$- and $y$- axis, then
$n = 4$ with $(\theta_1,\theta_2,\theta_3,\theta_4) = (0,\frac{\pi}{2},\pi,\frac{3\pi}{2})$ and all $d_i$ equals to $\frac{d}{2}$. This implies
$$\begin{align}\Lambda(\theta) 
&=  \frac{d}{2\max\left[ 
\cos\theta, \cos\left(\theta - \frac{\pi}{2}\right), 
\cos(\theta - \pi), 
\cos\left(\theta - \frac{3\pi}{2}\right)\right]}\\
&= \frac{d}{2\max(|\cos\theta|,|\sin\theta|)}\end{align}$$
The length you seek is simply
$$\Lambda(\theta) + \Lambda(\theta+\pi) = 2\Lambda(\theta) = \frac{d}{\max(|\cos\theta|,|\sin\theta|)}$$
