The Wayback Machine - https://web.archive.org/web/20240926221613/https://www.geeksforgeeks.org/number-of-common-tangents-between-two-circles-if-their-centers-and-radius-is-given/
Open In App

Number of common tangents between two circles if their centers and radius is given

Last Updated : 25 Mar, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Given two circles with a given radius and centers. The task is to find the number of common tangents between these circles.
Examples: 
 

Input: x1 = -10, y1 = 8, x2 = 14, y2 = -24, r1 = 30, r2 = 10
Output: 3

Input: x1 = 40, y1 = 8, x2 = 14, y2 = 54, r1 = 39, r2 = 51
Output: 2

Approach: 
 

  • First of all we will check whether the circles touch each other externally, intersect each other or do not touch each other at all.(Please refer here)

  • Then if the circles do not touch each other externally, then obviously they will have 4 common tangents, two direct and two transverse.

  • If the circles touch each other externally, then they will have 3 common tangents, two direct and one transverse. 
    The tangent in between can be thought of as the transverse tangents coinciding together.

  • If the circles intersect each other, then they will have 2 common tangents, both of them will be direct.
    •  

  • If one circle is inside another circle, then they will have only one common tangent

Algorithm:

Step 1: Create the “circle” function, which has six inputs: x1, y1, x2, y2, r1, and r2.
Step 2: Use the following formula to determine the separation between the centers of the two circles: (x1 – x2)^2 + (y1 – y2)^2. Use the formula                    (r1 + r2)^2 to determine the total of the two circles’ radii. 
Step 3: To ascertain the relationship between the two circles, compare the computed distance and the sum of radii:                                                                  a. If the distance is equal to the sum of radii, return 1 as there is only one common tangent.
             b. If the distance is greater than the sum of radii, return -1 as there are four common tangents.
             c. If the distance is less than the sum of radii, return 0 as there are two common tangents.

Below is the implementation of the above approach:

C++




// C++ program to find
// the number of common tangents
// between the two circles
 
#include <bits/stdc++.h>
using namespace std;
 
int circle(int x1, int y1, int x2,
        int y2, int r1, int r2)
{
 
    int distSq = (x1 - x2) * (x1 - x2)
                + (y1 - y2) * (y1 - y2);
 
    int radSumSq = (r1 + r2) * (r1 + r2);
 
    if (distSq == radSumSq)
        return 1;
    else if (distSq > radSumSq)
        return -1;
    else
        return 0;
}
 
// Driver code
int main()
{
    int x1 = -10, y1 = 8;
    int x2 = 14, y2 = -24;
    int r1 = 30, r2 = 10;
    int t = circle(x1, y1, x2,
                y2, r1, r2);
    if (t == 1)
        cout << "There are 3 common tangents"
            << " between the circles.";
    else if (t < 0)
        cout << "There are 4 common tangents"
            << " between the circles.";
    else
        cout << "There are 2 common tangents"
            << " between the circles.";
 
    return 0;
}


Java




// Java program to find
// the number of common tangents
// between the two circles
import java.io.*;
 
class GFG
{
     
static int circle(int x1, int y1, int x2,
        int y2, int r1, int r2)
{
 
    int distSq = (x1 - x2) * (x1 - x2)
                + (y1 - y2) * (y1 - y2);
 
    int radSumSq = (r1 + r2) * (r1 + r2);
 
    if (distSq == radSumSq)
        return 1;
    else if (distSq > radSumSq)
        return -1;
    else
        return 0;
}
 
// Driver code
public static void main (String[] args)
{
 
    int x1 = -10, y1 = 8;
    int x2 = 14, y2 = -24;
    int r1 = 30, r2 = 10;
    int t = circle(x1, y1, x2,
                y2, r1, r2);
    if (t == 1)
        System.out.println ("There are 3 common tangents"+
                    " between the circles.");
    else if (t < 0)
        System.out.println ("There are 4 common tangents"+
            " between the circles.");
    else
        System.out.println ("There are 2 common tangents" +
                " between the circles.");
 
     
}
}
 
// This code is contributed by ajit.


Python3




# Python3 program to find
# the number of common tangents
# between the two circles
 
def circle(x1, y1, x2,y2, r1, r2):
 
 
    distSq = (x1 - x2) * (x1 - x2)+ (y1 - y2) * (y1 - y2)
 
    radSumSq = (r1 + r2) * (r1 + r2)
 
    if (distSq == radSumSq):
        return 1
    elif (distSq > radSumSq):
        return -1
    else:
        return 0
 
 
# Driver code
x1,y1 = -10,8;
x2,y2 = 14,-24;
r1,r2 = 30,10;
 
t = circle(x1, y1, x2,y2, r1, r2);
 
if (t == 1):
    print("There are 3 common tangents between the circles.")
elif (t < 0):
    print("There are 4 common tangents between the circles.")
else:
    print("There are 2 common tangents between the circles.")
 
# This code is contributed by mohit kumar 29


C#




// C# program to find
// the number of common tangents
// between the two circles
using System;
 
class GFG
{
     
static int circle(int x1, int y1, int x2,
        int y2, int r1, int r2)
{
 
    int distSq = (x1 - x2) * (x1 - x2)
                + (y1 - y2) * (y1 - y2);
 
    int radSumSq = (r1 + r2) * (r1 + r2);
 
    if (distSq == radSumSq)
        return 1;
    else if (distSq > radSumSq)
        return -1;
    else
        return 0;
}
 
// Driver code
public static void Main (String []args)
{
 
    int x1 = -10, y1 = 8;
    int x2 = 14, y2 = -24;
    int r1 = 30, r2 = 10;
    int t = circle(x1, y1, x2,
                y2, r1, r2);
    if (t == 1)
        Console.WriteLine ("There are 3 common tangents"+
                    " between the circles.");
    else if (t < 0)
        Console.WriteLine ("There are 4 common tangents"+
            " between the circles.");
    else
        Console.WriteLine ("There are 2 common tangents" +
                " between the circles.");
 
}
}
 
// This code is contributed by Arnab Kundu


PHP




<?php
// PHP program to find
// the number of common tangents
// between the two circles
 
function circle($x1, $y1, $x2,
        $y2, $r1, $r2)
{
 
    $distSq = ($x1 - $x2) * ($x1 - $x2)
                + ($y1 - $y2) * ($y1 - $y2);
 
    $radSumSq = ($r1 + $r2) * ($r1 + $r2);
 
    if ($distSq == $radSumSq)
        return 1;
    else if ($distSq > $radSumSq)
        return -1;
    else
        return 0;
}
 
    // Driver code
    $x1 = -10; $y1 = 8;
    $x2 = 14; $y2 = -24;
    $r1 = 30; $r2 = 10;
    $t = circle($x1, $y1, $x2,
                $y2, $r1, $r2);
    if ($t == 1)
        echo "There are 3 common tangents"
                ," between the circles.";
    else if ($t < 0)
        echo "There are 4 common tangents"
            , " between the circles.";
    else
        echo "There are 2 common tangents"
            , " between the circles.";
             
    // This code is contributed by AnkitRai01
?>


Javascript




<script>
 
// Javascript program to find
// the number of common tangents
// between the two circles
 
function circle(x1, y1, x2,
        y2, r1, r2)
{
 
    var distSq = (x1 - x2) * (x1 - x2)
                + (y1 - y2) * (y1 - y2);
 
    var radSumSq = (r1 + r2) * (r1 + r2);
 
    if (distSq == radSumSq)
        return 1;
    else if (distSq > radSumSq)
        return -1;
    else
        return 0;
}
 
// Driver code
    var x1 = -10, y1 = 8;
    var x2 = 14, y2 = -24;
    var r1 = 30, r2 = 10;
    var t = circle(x1, y1, x2,
                y2, r1, r2);
    if (t == 1)
        document.write(
"There are 3 common tangents between the circles."
);
    else if (t < 0)
        document.write(
"There are 4 common tangents between the circles."
);
    else
        document.write(
"There are 2 common tangents between the circles."
);
 
</script>


Output:

There are 3 common tangents between the circles.

Time Complexity: O(1)

Auxiliary Space: O(1)



Similar Reads

Ratio of the distance between the centers of the circles and the point of intersection of two direct common tangents to the circles
Given two circles of given radii, such that the circles don’t touch each other, the task is to find the ratio of the distance between the centers of the circles and the point of intersection of two direct common tangents to the circles. Examples: Input: r1 = 4, r2 = 6 Output: 2:3 Input: r1 = 22, r2 = 43 Output: 22:43 Approach: Let the radii of the
5 min read
Ratio of the distance between the centers of the circles and the point of intersection of two transverse common tangents to the circles
Given two circles, of given radii, such that the circles don’t touch each other. The task is to find the ratio of the distance between the centres of the circles and the point of intersection of two transverse common tangents to the circles.Examples: Input :r1 = 4, r2 = 8 Output :1:2 Input :r1 = 5, r2 = 13 Output :5:13 Approach: Let the radii of th
5 min read
Find the radius of the circles which are lined in a row, and distance between the centers of first and last circle is given
Given here are n circles which touch each other externally, and are lined up in a row. The distance between the centers of the first and last circle is given. The circles have a radius of equal length. The task is to find the radius of each circle. Examples: Input: d = 42, n = 4 Output: The radius of each circle is 7 Input: d = 64, n = 5 Output: Th
3 min read
Distance between centers of two intersecting circles if the radii and common chord length is given
Given are two circles, with given radii, which intersect each other and have a common chord. The length of the common chord is given. The task is to find the distance between the center of the two circles. Examples: Input: r1 = 24, r2 = 37, x = 40 Output: 44 Input: r1 = 14, r2 = 7, x = 10 Output: 17 Approach: let the length of common chord AB = xLe
5 min read
Check if two circles intersect such that the third circle passes through their points of intersections and centers
Given centres and the radii of three circles A, B, and C in the form of {X, Y, R}, where (X, Y) is the centre of the circle and R is the radius of that circle. The task is to check if any two circles intersect such that the third circle passes through the intersecting points and the centres of the two circles. If found to be true, then print "Yes".
9 min read
Angle of intersection of two circles having their centers D distance apart
Given two positive integers R1 and R2 representing the radius of two intersecting circles having a distance D between their centers, the task is to find the cosine of the angle of intersection between the two circles. Examples: Input: R1 = 3, R2 = 4, D = 5Output: 0 Input: R1 = 7, R2 = 3, D = 6Output: 0.52381 Approach: The given problem can be solve
4 min read
Radii of the three tangent circles of equal radius which are inscribed within a circle of given radius
Given here is a circle of a given radius. Inside it, three tangent circles of equal radius are inscribed. The task is to find the radii of these tangent circles. Examples: Input: R = 4Output: 1.858 Input: R = 11Output: 5.1095 Approach: Let the radii of the tangent circles be r, and the radius of the circumscribing circle is R.x is the smaller dista
3 min read
Length of the perpendicular bisector of the line joining the centers of two circles
Given are two circles whose radii are given, such that the smaller lies completely within the bigger circle, and they touch each other at one point. We have to find the length of the perpendicular bisector of the line joining the centres of the circles.Examples: Input: r1 = 5, r2 = 3 Output: 9.79796 Input: r1 = 8, r2 = 4 Output: 15.4919 Approach: L
4 min read
Radius of a circle having area equal to the sum of area of the circles having given radii
Given two integers r1 and r2, representing the radius of two circles, the task is to find the radius of the circle having area equal to the sum of the area of the two circles having given radii. Examples: Input:r1 = 8r2 = 6Output:10Explanation:Area of circle with radius 8 = 201.061929 Area of a circle with radius 6 = 113.097335Area of a circle with
5 min read
Minimum Circles needed to be removed so that all remaining circles are non intersecting
Given n Circles present in x-y plane such that all the circles have their center aligned on the x-axis. The task is to remove some of them, such that no two circles are intersecting. Find the minimum number of circles that need to be removed. Note : Touching circles are also considered to be intersecting. Given N and an array of pair of integers. E
7 min read
Length of the transverse common tangent between the two non intersecting circles
Given two circles of given radii, having there centres a given distance apart, such that the circles don’t touch each other. The task is to find the length of the transverse common tangent between the circles.Examples: Input: r1 = 4, r2 = 6, d = 12 Output: 6.63325 Input: r1 = 7, r2 = 9, d = 21 Output: 13.6015 Approach: Let the radii of the circles
4 min read
Length of the direct common tangent between two externally touching circles
Given two circles, of given radii, that touch each other externally. The task is to find the length of the direct common tangent between the circles.Examples: Input: r1 = 5, r2 = 9 Output: 13.4164 Input: r1 = 11, r2 = 13 Output: 23.9165 Approach Let the radii be r1 & r2 respectively.Draw a line OR parallel to PQangle OPQ = 90 deg angle O’QP = 9
4 min read
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: Let the radii of the circles be r1
4 min read
Length of direct common tangent between two intersecting Circles
Given two circles, of given radii, have their centres a given distance apart, such that the circles intersect each other at two points. The task is to find the length of the direct common tangent between the circles.Examples: Input: r1 = 4, r2 = 6, d = 3 Output: 2.23607 Input: r1 = 14, r2 = 43, d = 35 Output: 19.5959 Approach: Let the radii of the
4 min read
Find the side of the squares which are lined in a row, and distance between the centers of first and last square is given
Given here are n squares which touch each other externally, and are lined up in a row. The distance between the centers of the first and last square is given. The squares have equal side length. The task is to find the side of each square.Examples: Input: d = 42, n = 4 Output: The side of each square is 14 Input: d = 36, n = 5 Output: The side of e
3 min read
Tangents between two Convex Polygons
Given two convex polygons, we need to find the lower and upper tangents to these polygons. As shown in the figure below, [Tex]T_{RL}      [/Tex]and [Tex]T_{LR}      [/Tex]show upper and lower tangent respectively.  Examples:   Input : First Polygon : {(2, 2), (3, 3), (5, 2), (4, 0), (3, 1)} Second Polygon : {(-1, 0), (0, 1), (1, 0), (0, -2)}.Output
15+ min read
Find the angle between tangents drawn from a given external point to a Circle
Given a positive integer R representing the radius of the circle and the center of the circle (X1, Y1) and another point (X2, Y2) in the cartesian plane, the task is to find the angle between the pair of tangents drawn from the point (X2, Y2) to the circle. Examples: Input: R = 6, (X1, Y1) = (5, 1), (X2, Y2) = (6, 9)Output: 96.1851 Input: R = 4, (X
7 min read
Number of triangles formed by joining vertices of n-sided polygon with two common sides and no common sides
Given N-sided polygon we need to find the total number of triangles formed by joining the vertices of the given polygon with exactly two sides being common and no side being common.Examples: Input : N = 6 Output : 6 2 The image below is of a triangle forming inside a Hexagon by joining vertices as shown above. The triangle formed has two sides (AB
5 min read
Maximum possible intersection by moving centers of line segments
Given three points on the X-axis which denotes the center of three line segments. The length of the line segment is also given as L. The task is to move the center of the given line segments by a distance of K to maximize the length of intersection between the three lines. Examples: Input: c1 = 1, c2 = 2, c3 = 3, L = 1, K = 0 Output: 0 Since, no ce
5 min read
Greedy Approximate Algorithm for K Centers Problem
Given n cities and distances between every pair of cities, select k cities to place warehouses (or ATMs or Cloud Server) such that the maximum distance of a city to a warehouse (or ATM or Cloud Server) is minimized. For example consider the following four cities, 0, 1, 2, and 3, and the distances between them, how to place 2 ATMs among these 4 citi
9 min read
Count of pairs in a given range with sum of their product and sum equal to their concatenated number
Given two numbers A and B, the task is to find the count of pairs (X, Y) in range [A, B], such that (X * Y) + (X + Y) is equal to the number formed by concatenation of X and YExamples: Input: A = 1, B = 9 Output: 9 Explanation: The pairs (1, 9), (2, 9), (3, 9), (4, 9), (5, 9), (6, 9), (7, 9), (8, 9) and (9, 9) are the required pairs.Input: A = 4, B
5 min read
Find two numbers B and C such that their product is A and their GCD is maximum
Given a positive integer, A. The task is to find two numbers B and C such that their product is A and their GCD should be maximum. Examples: Input: A = 72Output: 12 6Explanation: The product of 12 and 6 is 72 and GCD(12, 6) is 6 which is maximum possible. Input: A = 54Output: 6 9Explanation: The product of 6 and 9 is 54, gcd(6, 9) is 3 which is max
6 min read
Program to calculate the area between two Concentric Circles
Given two concentric circles with radius X and Y where (X > Y). Find the area between them. You are required to find the area of the green region as shown in the following image: Examples: Input : X = 2, Y = 1 Output : 9.42478 Input : X = 4, Y = 2 Output : 37.6991 Approach:The area between the two given concentric circles can be calculated by su
4 min read
Maximum sum of a subsequence having difference between their indices equal to the difference between their values
Given an array A[] of size N, the task is to find the maximum sum of a subsequence such that for each pair present in the subsequence, the difference between their indices in the original array is equal to the difference between their values. Examples: Input: A[] = {10, 7, 1, 9, 10, 15}, N = 6Output: 26Explanation: Subsequence: {7, 9, 10}. Indices
5 min read
Check if a given circle lies completely inside the ring formed by two concentric circles
Given two circles of radius r and R, both have their centre at the origin. Now, given another circle of radius r1 and centre at (x1, y1). Check, if the third circle(circle of radius r1) lies completely inside the ring formed by two circles of radius r and R.Examples : Input : r = 8 R = 4 r1 = 2 x1 = 6 y1 = 0 Output : yes Input : r = 8 R = 4 r1 = 2
6 min read
Check whether given circle resides in boundary maintained by two other circles
Given outer circle radius R and inner circle radius r, making circles from the same center and forming the boundary between them. Now, given X and Y coordinates which denote the center of the new circle to be formed with radius rad, your task is to check whether the circle with coordinates X and Y as the center can fit in the boundary of circles fo
6 min read
Check if two given circles touch or intersect each other
There are two circles A and B with their centres C1(x1, y1) and C2(x2, y2) and radius R1 and R2. The task is to check both circles A and B touch each other or not. Examples : Input : C1 = (3, 4) C2 = (14, 18) R1 = 5, R2 = 8Output : Circles do not touch each other. Input : C1 = (2, 3) C2 = (15, 28) R1 = 12, R2 = 10Output : Circles intersect with eac
5 min read
Find two integers X and Y with given GCD P and given difference between their squares Q
Given two integers P and Q, the task is to find any two integers whose Greatest Common Divisor(GCD) is P and the difference between their squares is Q. If there doesn't exist any such integers, then print "-1". Examples: Input: P = 3, Q = 27Output: 6 3Explanation:Consider the two number as 6, 3. Now, the GCD(6, 3) = 3 and 6*6 - 3*3 = 27 which satis
7 min read
Length of the chord of the circle whose radius and the angle subtended at the center by the chord is given
Given a circle whose radius and the angle subtended at the centre by its chord is given. The task is to find the length of the chord.Examples: Input: r = 4, x = 63 Output: 4.17809 Input:: r = 9, x = 71 Output:: 10.448 Approach: Let the circle has center at O and has radius r, and it's chord be AB.length of the chord be 2d, and the angle subtended b
4 min read
Radius of the circle when the width and height of an arc is given
Given a circle in which the width and height of an arc are given. The task is to find the radius of the circle with the help of the width and height of the arc.Examples: Input: d = 4, h = 1 Output: The radius of the circle is 2.5 Input: d = 14, h = 8 Output: The radius of the circle is 7.0625 Approach Let the radius of the circle be rLet the height
4 min read
Article Tags :
Practice Tags :