public static bool SolveCubic(float e, float f, float g, out float t, out float u, out float v) { float p, q, h, rc, d, theta, costh3, sinth3; p = f - (e * e / 3.0f); q = g - (e * f / 3.0f) + (e * e * e * 2.0f / 27.0f); h = (q * q / 4.0f) + (p * p * p / 27.0f); if (h > 0.0f) { t = u = v = 0.0f; return(false); // only one real root } // all the same root if ((h == 0.0f) && (q == 0.0f)) { t = -e / 3; u = -e / 3; v = -e / 3; return(true); } d = (float)Math.Sqrt((q * q / 4.0f) - h); if (d < 0) { rc = (float)-Math.Pow(-d, 1.0f / 3.0f); } else { rc = (float)Math.Pow(d, 1.0f / 3.0f); } theta = XMScalar.ACos(-q / (2.0f * d)); costh3 = XMScalar.Cos(theta / 3.0f); sinth3 = (float)Math.Sqrt(3.0f) * XMScalar.Sin(theta / 3.0f); t = (2.0f * rc * costh3) - (e / 3.0f); u = (-rc * (costh3 + sinth3)) - (e / 3.0f); v = (-rc * (costh3 - sinth3)) - (e / 3.0f); return(true); }