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 = x
- Let the radius of the circle with center O is OA = r2
- Radius of circle with center P is AP = r1
- From the figure, OP is perpendicular AB AC = CB AC = x/2 (Since AB = x)
- In triangle ACP, AP^2 = PC^2+ AC^2 [By Pythagoras theorem] r1^2 = PC^2 + (x/2)^2 PC^2 = r1^2 – x^2/4
- Consider triangle ACO r2^2 = OC^2+ AC^2[By Pythagoras theorem] r2^2 = OC^2+ (x/2)^2 OC^2 = r2^2 – x^2/4
- From the figure, OP = OC + PC OP = √( r1^2 – x^2/4 ) + √(r2^2 – x^2/4)
Distance between the centers = sqrt((radius of one circle)^2 – (half of the length of the common chord )^2) + sqrt((radius of the second circle)^2 – (half of the length of the common chord )^2)
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void distcenter( int r1, int r2, int x)
{
int z = sqrt ((r1 * r1)
- (x / 2 * x / 2))
+ sqrt ((r2 * r2)
- (x / 2 * x / 2));
cout << "distance between the"
<< " centers is "
<< z << endl;
}
int main()
{
int r1 = 24, r2 = 37, x = 40;
distcenter(r1, r2, x);
return 0;
}
|
Java
import java.lang.Math;
import java.io.*;
class GFG {
static double distcenter( int r1, int r2, int x)
{
double z = (Math.sqrt((r1 * r1)
- (x / 2 * x / 2 )))
+ (Math.sqrt((r2 * r2)
- (x / 2 * x / 2 )));
System.out.println ( "distance between the" +
" centers is " + ( int )z );
return 0 ;
}
public static void main (String[] args)
{
int r1 = 24 , r2 = 37 , x = 40 ;
distcenter(r1, r2, x);
}
}
|
Python3
def distcenter(r1, r2, x):
z = (((r1 * r1) - (x / 2 * x / 2 )) * * ( 1 / 2 )) + \
(((r2 * r2) - (x / 2 * x / 2 )) * * ( 1 / 2 ));
print ( "distance between thecenters is " ,end = "");
print ( int (z));
r1 = 24 ; r2 = 37 ; x = 40 ;
distcenter(r1, r2, x);
|
C#
using System;
class GFG
{
static double distcenter( int r1, int r2, int x)
{
double z = (Math.Sqrt((r1 * r1)
- (x / 2 * x / 2)))
+ (Math.Sqrt((r2 * r2)
- (x / 2 * x / 2)));
Console.WriteLine( "distance between the" +
" centers is " + ( int )z );
return 0;
}
static public void Main ()
{
int r1 = 24, r2 = 37, x = 40;
distcenter(r1, r2, x);
}
}
|
PHP
<?php
function distcenter( $r1 , $r2 , $x )
{
$z = sqrt(( $r1 * $r1 ) - ( $x / 2 * $x / 2))
+ sqrt(( $r2 * $r2 ) - ( $x / 2 * $x / 2));
echo ( "distance between the centers is " );
echo ((int) $z );
}
$r1 = 24; $r2 = 37; $x = 40;
distcenter( $r1 , $r2 , $x );
?>
|
Javascript
<script>
function distcenter(r1, r2, x)
{
var z = Math.sqrt((r1 * r1) - (x / 2 * x / 2))
+ Math.sqrt((r2 * r2) - (x / 2 * x / 2));
document.write( "distance between the centers is " + z);
}
var r1 = 24,r2 = 37,x = 40;
distcenter(r1, r2, x);
</script>
|
Output :
distance between the centers is 44
Time Complexity : O(log(n)) ,because using inbuilt sqrt function
Space Complexity : O(1) ,as no extra space used.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
03 Aug, 2022
Like Article
Save Article