Open In App

Length of direct common tangent between the two non-intersecting Circles

Given two circles, of given radii, have there centres a given distance apart, such that the circles don’t touch each other. The task is to find the length of the direct common tangent between the circles.
Examples: 
 

Input: r1 = 4, r2 = 6, d = 12 
Output: 11.8322

Input: r1 = 5, r2 = 9, d = 25
Output: 24.6779


 




 




Approach
 

Below is the implementation of the above approach:

// C++ program to find
// the length of the direct
// common tangent between two circles
// which donot touch each other
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the length of the direct common tangent
void lengtang(double r1, double r2, double d)
{
    cout << "The length of the direct"
        <<" common tangent is "
        << sqrt(pow(d, 2) - pow((r1 - r2), 2))
        << endl;
}
 
// Driver code
int main()
{
    double r1 = 4, r2 = 6, d = 12;
    lengtang(r1, r2, d);
    return 0;
}

                    
// Java program to find
// the length of the direct
// common tangent between two circles
// which donot touch each other
class GFG
{
 
// Function to find the length of
// the direct common tangent
static void lengtang(double r1, double r2, double d)
{
    System.out.println("The length of the direct"
        +" common tangent is "
        +(Math.sqrt(Math.pow(d, 2) -
        Math.pow((r1 - r2), 2))));
}
 
// Driver code
public static void main(String[] args)
{
    double r1 = 4, r2 = 6, d = 12;
    lengtang(r1, r2, d);
}
}
 
/* This code contributed by PrinciRaj1992 */

                    
# Python3 program to find
# the length of the direct
# common tangent between two circles
# which do not touch each other
import math
 
# Function to find the length
# of the direct common tangent
def lengtang(r1, r2, d):
    print("The length of the direct common tangent is",
        (((d ** 2) - ((r1 - r2) ** 2)) ** (1 / 2)));
 
# Driver code
r1 = 4; r2 = 6; d = 12;
lengtang(r1, r2, d);
 
# This code is contributed by 29AjayKumar

                    
// C# program to find
// the length of the direct
// common tangent between two circles
// which donot touch each other
using System;
 
class GFG
{
 
    // Function to find the length of
    // the direct common tangent
    static void lengtang(double r1, double r2, double d)
    {
        Console.WriteLine("The length of the direct"
            +" common tangent is "
            +(Math.Sqrt(Math.Pow(d, 2) -
            Math.Pow((r1 - r2), 2))));
    }
     
    // Driver code
    public static void Main()
    {
        double r1 = 4, r2 = 6, d = 12;
        lengtang(r1, r2, d);
    }
}
 
// This code is contributed by AnkitRai01

                    
<?php
// PHP program to find the length
// of the direct common tangent
// between two circles which
// donot touch each other
 
// Function to find the length
// of the direct common tangent
function lengtang($r1, $r2, $d)
{
    echo "The length of the direct",
            " common tangent is ",
        sqrt(pow($d, 2) -
            pow(($r1 - $r2), 2)), "\n";
}
 
// Driver code
$r1 = 4;
$r2 = 6;
$d = 12;
lengtang($r1, $r2, $d);
 
// This code is contributed by akt_mit
?>

                    
<script>
 
// Javascript program to find
// the length of the direct
// common tangent between two circles
// which donot touch each other
 
 
// Function to find the length of the direct common tangent
function lengtang(r1, r2, d)
{
    document.write("The length of the direct common tangent is "+
        Math.sqrt(Math.pow(d, 2) - Math.pow((r1 - r2), 2)));
}
 
// Driver code
    var r1 = 4, r2 = 6, d = 12;
    lengtang(r1, r2, d);
 
</script>

                    

Output:

The length of the direct common tangent is 11.8322

Time Complexity: O(1)

Auxiliary Space: O(1)


Article Tags :