Given six integers, a, b, c, i, j, and k representing the equation of the circle and equation of the line , the task is to find the length of the intercept cut off from the given line to the circle.

Examples:
Input: a = 0, b = 0, c = -4, i = 2, j = -1, k = 1 Output: 3.89872
Input: a = 5, b = 6, c = -16, i = 1, j = 4, k = 3 Output: 6.9282
Approach: The problem can be solved based on the following mathematical fact.
As the line is intercepted by the circle the intercepted segment forms a chord of the circle. Now if a perpendicular is drawn from the centre of the circle to a chord, it divides the chord into two equal parts.
We can easily calculate the radius of the circle and the perpendicular distance of the chord from the centre from the given equations Using them we can get the length of the intercepted line segment.
Follow the steps below to solve the problem:
- Find the center of the circle, say
as and . - The perpendicular from the center divides the intercept into two equal parts, therefore calculate the length of one of the parts and multiply it by 2 to get the total length of the intercept.
- Calculate the value of radius (r) using the formula:
, where and  - Calculate the value of perpendicular distance ( d ) of center O from the line by using the formula:
 - Now from the Pythagoras theorem in triangle OCA:

- After completing the above steps, print the value of twice of AC to get the length of the total intercept.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
double radius( int a, int b, int c)
{
int g = a / 2;
int f = b / 2;
if (g * g + f * f - c < 0)
return (-1);
return ( sqrt (g * g + f * f - c));
}
double centerDistanceFromLine( int a, int b, int i, int j,
int k)
{
int g = a / 2;
int f = b / 2;
double distance
= fabs (i * g + j * f + k) / ( sqrt (i * i + j * j));
if (distance < 0)
return (-1);
return distance;
}
void interceptLength( int a, int b, int c, int i, int j,
int k)
{
double rad = radius(a, b, c);
double dist = centerDistanceFromLine(a, b, i, j, k);
if (rad < 0 || dist < 0) {
cout << "circle not possible" ;
return ;
}
if (dist > rad) {
cout << "Line not cutting circle" ;
}
else
cout << 2 * sqrt (rad * rad - dist * dist);
}
int main()
{
int a = 0, b = 0, c = -4;
int i = 2, j = -1, k = 1;
interceptLength(a, b, c, i, j, k);
return 0;
}
|
Java
class GFG{
static double radius( int a, int b, int c)
{
int g = a / 2 ;
int f = b / 2 ;
if (g * g + f * f - c < 0 )
return (- 1 );
return (Math.sqrt(g * g + f * f - c));
}
static double centerDistanceFromLine( int a, int b,
int i, int j,
int k)
{
int g = a / 2 ;
int f = b / 2 ;
double distance = Math.abs(i * g + j * f + k) /
(Math.sqrt(i * i + j * j));
if (distance < 0 )
return (- 1 );
return distance;
}
static void interceptLength( int a, int b, int c,
int i, int j, int k)
{
double rad = radius(a, b, c);
double dist = centerDistanceFromLine(
a, b, i, j, k);
if (rad < 0 || dist < 0 )
{
System.out.println( "circle not possible" );
return ;
}
if (dist > rad)
{
System.out.println( "Line not cutting circle" );
}
else
System.out.println( 2 * Math.sqrt(
rad * rad - dist * dist));
}
public static void main(String[] args)
{
int a = 0 , b = 0 , c = - 4 ;
int i = 2 , j = - 1 , k = 1 ;
interceptLength(a, b, c, i, j, k);
}
}
|
Python3
import math
def radius(a, b, c):
g = a / 2
f = b / 2
if (g * g + f * f - c < 0 ):
return ( - 1 )
return (math.sqrt(g * g + f * f - c))
def centerDistanceFromLine(a, b, i, j, k):
g = a / 2
f = b / 2
distance = ( abs (i * g + j * f + k) /
(math.sqrt(i * i + j * j)))
if (distance < 0 ):
return ( - 1 )
return distance
def interceptLength(a, b, c, i, j, k):
rad = radius(a, b, c)
dist = centerDistanceFromLine(
a, b, i, j, k)
if (rad < 0 or dist < 0 ):
print ( "circle not possible" )
return
if (dist > rad):
print ( "Line not cutting circle" )
else :
print ( 2 * math.sqrt(
rad * rad - dist * dist))
if __name__ = = "__main__" :
a = 0
b = 0
c = - 4
i = 2
j = - 1
k = 1
interceptLength(a, b, c, i, j, k)
|
C#
using System;
class GFG{
static double radius( int a, int b, int c)
{
int g = a / 2;
int f = b / 2;
if (g * g + f * f - c < 0)
return (-1);
return (Math.Sqrt(g * g + f * f - c));
}
static double centerDistanceFromLine( int a, int b,
int i, int j,
int k)
{
int g = a / 2;
int f = b / 2;
double distance = Math.Abs(i * g + j * f + k) /
(Math.Sqrt(i * i + j * j));
if (distance < 0)
return (-1);
return distance;
}
static void interceptLength( int a, int b, int c,
int i, int j, int k)
{
double rad = radius(a, b, c);
double dist = centerDistanceFromLine(
a, b, i, j, k);
if (rad < 0 || dist < 0)
{
Console.WriteLine( "circle not possible" );
return ;
}
if (dist > rad)
{
Console.WriteLine( "Line not cutting circle" );
}
else
Console.WriteLine(2 * Math.Sqrt(
rad * rad - dist * dist));
}
public static void Main(String []args)
{
int a = 0, b = 0, c = -4;
int i = 2, j = -1, k = 1;
interceptLength(a, b, c, i, j, k);
}
}
|
Javascript
<script>
function radius(a, b, c) {
let g = a / 2;
let f = b / 2;
if (g * g + f * f - c < 0)
return (-1);
return (Math.sqrt(g * g + f * f - c));
}
function centerDistanceFromLine(a, b, i, j, k) {
let g = a / 2;
let f = b / 2;
let distance = Math.abs(i * g + j * f + k) /
(Math.sqrt(i * i + j * j));
if (distance < 0)
return (-1);
return distance;
}
function interceptLength(a, b, c, i, j, k) {
let rad = radius(a, b, c);
let dist = centerDistanceFromLine(
a, b, i, j, k);
if (rad < 0 || dist < 0) {
document.write( "circle not possible" );
return ;
}
if (dist > rad) {
document.write( "Line not cutting circle" );
}
else
document.write(2 * Math.sqrt(
rad * rad - dist * dist));
}
let a = 0, b = 0, c = -4;
let i = 2, j = -1, k = 1;
interceptLength(a, b, c, i, j, k);
</script>
|
Time Complexity: O(logN), for using in-built sqrt() function. Auxiliary Space: O(1)
|