Calculate the inscribed circle between one line and two circles

geometry

I am currently working on finding a calculation that will help me determine the values of $C_x$, $C_y$ and $C_r$ as shown in the image below.

The goal is for the unknown red circle to touch the line and be tangent to the two green circles.

This website explains the situation as pointed out by Intelligenti pauca

For example, the given values are:

C1r = 2
C1x = 2
C1y = 8
C2r = 2.5
C2x = 11
C2y = 9
P1x = 5
P1y = 0
P2x = 12
P2y = 3

The outcome should be somewhere around:
Cr = 3.671
Cx = 6.596
Cy = 4.678

enter image description here

The worked out result in C# code
(based on the accepted answer)

    public static bool GetCircleFromLCC(out double C_x, out double C_y, out double C_radius)
    {
        // -- Input values --
        double C1_radius = 2;
        double C1_x = 2;
        double C1_y = 8;
        double C2_radius = 2.5;
        double C2_x = 11;
        double C2_y = 9;
        double P1_x = 5;
        double P1_y = 0;
        double P2_x = 12;
        double P2_y = 3;

        // -- Calculations --
        double u_x = P2_x - P1_x;
        double u_y = P2_y - P1_y;
        double norm = Math.Sqrt(u_x * u_x + u_y * u_y);
        u_x /= norm;
        u_y /= norm;
        double u1_x = -(u_x * P1_x + u_y * P1_y);
        double u1_y = -(-u_y * P1_x + u_x * P1_y);
        double u2_x = u_x * C1_x + u_y * C1_y;
        double u2_y = -u_y * C1_x + u_x * C1_y;
        double c1p_x = u2_x + u1_x;
        double c1p_y = u2_y + u1_y;
        u2_x = u_x * C2_x + u_y * C2_y;
        u2_y = -u_y * C2_x + u_x * C2_y;
        double c2p_x = u2_x + u1_x;
        double c2p_y = u2_y + u1_y;
        double a = 2.0 * (c1p_x - c2p_x);
        double b = 2.0 * (c1p_y - c2p_y + C1_radius - C2_radius);
        double c = Math.Pow(C1_radius, 2) - Math.Pow(C2_radius, 2) + Math.Pow(c2p_x, 2) - Math.Pow(c1p_x, 2) + Math.Pow(c2p_y, 2) - Math.Pow(c1p_y, 2);
        double a0 = Math.Pow(c1p_x, 2) + Math.Pow(c1p_y, 2) - Math.Pow(C1_radius, 2) + 2.0 * c / b * (c1p_y + C1_radius);
        double a1 = -2.0 * c1p_x + 2.0 * a / b * (C1_radius + c1p_y);
        double xdisc = Math.Pow(a1, 2) - 4.0 * a0;

        // Find quadratic roots
        if (xdisc > 0.00000001)
        {
            double root1 = (-a1 - Math.Sqrt(xdisc)) / 2;
            C_radius = 1 / b * (-c - a * root1);
            if (Math.Sign(C_radius) == Math.Sign(c1p_y) && Math.Sign(C_radius) == Math.Sign(c2p_y))
            {
                // Valid root, calculate the corresponding x,y coordinates
                u2_x = u_x * root1 + -u_y * C_radius;
                u2_y = u_y * root1 + u_x * C_radius;
                C_x = u2_x + P1_x;
                C_y = u2_y + P1_y;

                Console.WriteLine($"Solution 1   C_x: {C_x:0.0##}   y: {C_y:0.0##}   radius: {C_radius:0.0##}");
                // Solution 1   x: 6.596   y: 4.678   radius: 3.671
            }
            double root2 = (-a1 + Math.Sqrt(xdisc)) / 2;
            C_radius = 1 / b * (-c - a * root2);
            if (Math.Sign(C_radius) == Math.Sign(c1p_y) && Math.Sign(C_radius) == Math.Sign(c2p_y))
            {
                // Valid root, calculate the corresponding x,y coordinates
                u2_x = u_x * root2 + -u_y * C_radius;
                u2_y = u_y * root2 + u_x * C_radius;
                C_x = u2_x + P1_x;
                C_y = u2_y + P1_y;

                Console.WriteLine($"Solution 2   C_x: {C_x:0.0##}   y: {C_y:0.0##}   radius: {C_radius:0.0##}");
                // Solution 2   x: -48.359   y: 336.121   radius: 329.963
                return true;
            }
        }

        C_x = 0;
        C_y = 0;
        C_radius = 0;
        return false;

    }

Best Answer

For convenience, rotate the picture so the line is parallel to the $x$ axis, say $y = y_0$. Now you want the centre of your third circle to be a point $C = (x,y)$ such that $\text{dist}(C, C_1) = r_1 + y - y_1$ and $\text{dist}(C, C_2) = r_2 + y - y_2$, where $C_1 = (x_1, y_1)$, $C_2 = (x_2, y_2)$, and the given circles have radii $r_1$ and $r_2$. Expanding $(x - x_1)^2 + (y - y_1)^2 = (r_1 + y - y_1)^2$ and simplifying gives $x^2 - 2 x_1 x - 2 r_1 y + a_1 = 0$ where $a_1 = x_1^2 + 2 r_1 y_1 - r_1^2$. Similarly for the second circle you get $x^2 - 2 x_2 x - 2 r_2 y + a_2 = 0$. You can then eliminate $y$ and solve a quadratic equation to get $x$.