I have a similar problem to the one outlined here. fitting rectangle inside another rectangle in diagonal I know the values for A, B, C, and I need to find D. A could be <, >, or = to B.

I have a similar problem to the one outlined here. fitting rectangle inside another rectangle in diagonal I know the values for A, B, C, and I need to find D. A could be <, >, or = to B.

Let $\theta$ be the angle that the side with length $d$ makes with the horizontal direction, with the condition that $0 \le \theta \le \dfrac{\pi}{2} $.
Then summing distances vertically, we get
$ d \sin \theta + c \cos \theta = b \tag{1} $
And summing distances horizontally,
$ d \cos \theta + c \sin \theta = a \tag{2}$
There are two unknowns here, $d$ and $ \theta $.
Multiply the first equation by $\cos \theta$ and the second by $\sin \theta $, and subtract. This gives you
$ c (\cos^2 \theta - \sin^2 \theta ) = b \cos \theta - a \sin \theta \tag{3}$
And this can be re-written as
$ c \cos(2 \theta) = b \cos \theta - a \sin \theta \tag{4} $
This equation can be solved by the substitution
$ t = \tan\left( \dfrac{\theta}{2} \right) \tag{5}$
This will transform the trigonometric equation into the quartic polynomial equation in the transformed variable $t$:
$ a_4 t^4 + a_3 t^3 + a_2 t^2 + a_1 t + a_0 = 0 \tag{6}$
Where
$ a_4 = b + c $
$ a_3 = 2 a $
$ a_2 = -6 c $
$ a_1 = 2 a $
$ a_0 = - b + c$
The quartic equation $(6)$ can be solved exactly using Wolframalpha or Mathematica online, or offline. In the absence of these tools, you can use the code below to solve it yourself. The code is written in plain and simple VBA script, and can be re-written in any other language.
Once the roots (the values of $t$) have been found by the quartic polynomial root finder, the values of $\theta$ can be found by using the inverse transform of $(5)$, which is
$ \theta = 2 \tan^{-1}(t) \tag{7} $
Once the possible $\theta$'s have been computed, then $d$ can be computed from $(1)$.
Here is the VBA Script code to find the roots of a quartic polynomial equation. The equation is assumed to be of the form
$ a_1 x^4 + b_1 x^3 + c_1 x^2 + d_1 x + e_1 = 0 $
Public Sub quartic1(a1, b1, c1, d1, e1, r)
Dim rt(4, 2) As Double
Dim a, b, c, d, e As Double
Dim i, j, k As Integer
Dim cubic_roots(3) As Double
Dim r0 As Double
' a z^4 + b z^3 + c z^2 + d z + e = 0
a = a1
b = b1
c = c1
d = d1
e = e1
b = b / a
c = c / a
d = d / a
e = e / a
a = 1
' z^4 + b z^3 + c z^2 + d z + e = 0
' solve the resolvent cubic
a2 = -c
a1 = b * d - 4 * e
a0 = 4 * c * e - d ^ 2 - b ^ 2 * e
Call find_cubic_real_root(a2, a1, a0, x0)
r0 = b ^ 2 / 4 - c + x0
If Abs(r0) < 0.00000001 Then r0 = 0
If r0 >= 0 Then
r0x = Sqr(r0)
r0y = 0
Else
r0x = 0
r0y = Sqr(-r0)
End If
r0norm = Sqr(r0x ^ 2 + r0y ^ 2)
r2x = r0x ^ 2 - r0y ^ 2
r2y = 0
If Abs(r0norm) > 0.00000001 Then
rinvx = 1 / r0norm ^ 2 * (r0x)
rinvy = 1 / r0norm ^ 2 * (-r0y)
d2x = 0.75 * b ^ 2 - r2x - 2 * c + 0.25 * (4 * b * c - 8 * d - b ^ 3) * rinvx
d2y = 0.25 * (4 * b * c - 8 * d - b ^ 3) * rinvy
' now find the square root of d2
theta = atn2(d2x, d2y)
d2norm = Sqr(d2x ^ 2 + d2y ^ 2)
d3x = d2norm ^ (1 / 2) * Cos(theta / 2)
d3y = d2norm ^ (1 / 2) * Sin(theta / 2)
Else
x1 = x0 ^ 2 - 4 * e
If Abs(x1) < 0.00000001 Then x1 = 0
d2 = 0.75 * b ^ 2 - 2 * c + 2 * Sqr(x1)
If d2 >= 0 Then
d3x = Sqr(d2)
d3y = 0
Else
d3x = 0
d3y = Sqr(-d2)
End If
End If
rt(1, 1) = -b / 4 + 0.5 * r0x + 0.5 * d3x
rt(1, 2) = 0.5 * r0y + 0.5 * d3y
rt(2, 1) = -b / 4 + 0.5 * r0x - 0.5 * d3x
rt(2, 2) = 0.5 * r0y - 0.5 * d3y
lnext_couple:
If r0norm > 0.00000001 Then
rinvx = 1 / r0norm ^ 2 * (r0x)
rinvy = 1 / r0norm ^ 2 * (-r0y)
e2x = 0.75 * b ^ 2 - r2x - 2 * c - 0.25 * (4 * b * c - 8 * d - b ^ 3) * rinvx
e2y = -0.25 * (4 * b * c - 8 * d - b ^ 3) * rinvy
theta = atn2(e2x, e2y)
e2norm = Sqr(e2x ^ 2 + e2y ^ 2)
e3x = e2norm ^ (1 / 2) * Cos(theta / 2)
e3y = e2norm ^ (1 / 2) * Sin(theta / 2)
Else
x1 = x0 ^ 2 - 4 * e
If Abs(x1) < 0.00000001 Then x1 = 0
e2 = 0.75 * b ^ 2 - 2 * c - 2 * Sqr(x1)
If e2 >= 0 Then
e3x = Sqr(e2)
e3y = 0
Else
e3x = 0
e3y = Sqr(-e2)
End If
End If
rt(3, 1) = -b / 4 - 0.5 * r0x + 0.5 * e3x
rt(3, 2) = -0.5 * r0y + 0.5 * e3y
rt(4, 1) = -b / 4 - 0.5 * r0x - 0.5 * e3x
rt(4, 2) = -0.5 * r0y - 0.5 * e3y
iroot = 0
For i = 1 To 4
If Abs(rt(i, 2)) < 0.000001 Then
iroot = iroot + 1
r(iroot) = rt(i, 1)
End If
lnext_i:
Next i
r(0) = iroot
End Sub
Public Sub find_cubic_real_root(a2, a1, a0, x0)
Dim b As Double
Dim q0, q, r As Double
q = (3 * a1 - a2 ^ 2) / 9
r = (9 * a2 * a1 - 27 * a0 - 2 * a2 ^ 3) / 54
d = q ^ 3 + r ^ 2
If d >= 0 Then
q0 = r + Sqr(d)
s = Sgn(q0) * (Abs(r + Sqr(d))) ^ (1 / 3)
q0 = r - Sqr(d)
t = Sgn(q0) * (Abs(r - Sqr(d))) ^ (1 / 3)
x0 = -a2 / 3 + s + t
Else
dsi = Sqr(-d)
' Atan2 is arctan(x,y)
theta = WorksheetFunction.Atan2(r, dsi)
x0 = -a2 / 3 + 2 * (Sqr(r ^ 2 + dsi ^ 2)) ^ (1 / 3) * Cos(theta / 3)
End If
End Sub