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, (X1, Y1) = (7, 12), (X2, Y2) = (3, 4)
Output: 53.1317
Approach: The given problem can be solved based on the following observations:
- The radius makes an angle of 90 degrees with the tangent at the point of contact of the tangent and circle. Also, the angle subtended by the pair of tangents (?) is bisected by the line joining the center of the circle and the exterior point.
- Therefore, the distance between the center and the exterior point can be calculated using the distance formula as:
Distance = 

Now, consider d as the distance between the two given points, then In the right-angled triangle OAB,
=> 
=> 
=> 
=> 
Therefore, using the above formula, the angle between the pair of tangents drawn from the point (X2, Y2) to the circle can be calculated.
Below is the implementation of the above approach:
C++
#include <cmath>
#include <iostream>
using namespace std;
double point_distance( int x1, int y1,
int x2, int y2)
{
int p = (x2 - x1);
int q = (y2 - y1);
double distance = sqrt (p * p
+ q * q);
return distance;
}
void tangentAngle( int x1, int y1,
int x2, int y2,
double radius)
{
double distance = point_distance(
x1, y1, x2, y2);
if (radius / distance > 1
|| radius / distance < -1) {
cout << -1;
}
double result
= 2 * asin (radius / distance) * 180
/ 3.1415;
cout << result << " degrees" ;
}
int main()
{
int radius = 4;
int x1 = 7, y1 = 12;
int x2 = 3, y2 = 4;
tangentAngle(x1, y1, x2, y2, radius);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG
{
static double point_distance( int x1, int y1,
int x2, int y2)
{
int p = (x2 - x1);
int q = (y2 - y1);
double distance = Math.sqrt(p * p
+ q * q);
return distance;
}
static void tangentAngle( int x1, int y1,
int x2, int y2,
double radius)
{
double distance = point_distance(
x1, y1, x2, y2);
if (radius / distance > 1
|| radius / distance < - 1 ) {
System.out.println(- 1 );
}
double result
= 2 * Math.asin(radius / distance) * 180
/ 3.1415 ;
System.out.println(String.format( "%.4f" , result) + " degrees" );
}
public static void main(String[] args)
{
int radius = 4 ;
int x1 = 7 , y1 = 12 ;
int x2 = 3 , y2 = 4 ;
tangentAngle(x1, y1, x2, y2, radius);
}
}
|
Python3
import math
def point_distance(x1, y1,
x2, y2):
p = (x2 - x1)
q = (y2 - y1)
distance = math.sqrt(p * p
+ q * q)
return distance
def tangentAngle(x1, y1,
x2, y2,
radius):
distance = point_distance(
x1, y1, x2, y2)
if (radius / distance > 1
or radius / distance < - 1 ):
print ( - 1 )
result = 2 * math.asin(radius / distance) * 180 / 3.1415
print (result, " degrees" )
if __name__ = = "__main__" :
radius = 4
x1 = 7
y1 = 12
x2 = 3
y2 = 4
tangentAngle(x1, y1, x2, y2, radius)
|
C#
using System;
class GFG{
static double point_distance( int x1, int y1,
int x2, int y2)
{
int p = (x2 - x1);
int q = (y2 - y1);
double distance = Math.Sqrt(p * p + q * q);
return distance;
}
static void tangentAngle( int x1, int y1, int x2,
int y2, double radius)
{
double distance = point_distance(x1, y1, x2, y2);
if (radius / distance > 1 ||
radius / distance < -1)
{
Console.WriteLine(-1);
}
double result = 2 * Math.Asin(
radius / distance) *
180 / 3.1415;
Console.WriteLine(
String.Format( "{0:0.0000}" , result) +
" degrees" );
}
static void Main()
{
int radius = 4;
int x1 = 7, y1 = 12;
int x2 = 3, y2 = 4;
tangentAngle(x1, y1, x2, y2, radius);
}
}
|
Javascript
<script>
function point_distance( x1, y1, x2, y2)
{
var p = (x2 - x1);
var q = (y2 - y1);
var distance = Math.sqrt(p * p + q * q);
return distance;
}
function tangentAngle( x1, y1, x2, y2, radius)
{
var distance = point_distance(x1, y1, x2, y2);
if (radius / distance > 1 ||
radius / distance < -1)
{
document.write(-1 + "<br>" );
}
var result = 2 * Math.asin(
radius / distance) *
180 / 3.1415;
document.write( result.toFixed(4) + " degrees" );
}
var radius = 4;
var x1 = 7, y1 = 12;
var x2 = 3, y2 = 4;
tangentAngle(x1, y1, x2, y2, radius);
</script>
|
Time Complexity: O(logn) where n = (x2-x1)2+(y2-y1)2, because using inbuilt sqrt function
Auxiliary Space: O(1)
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!