Dados dos enteros positivos R1 y R2 que representan el radio de dos círculos que se cortan y que tienen una distancia D entre sus centros, la tarea es encontrar el coseno del ángulo de intersección entre los dos círculos.
Ejemplos:
Entrada: R1 = 3, R2 = 4, D = 5
Salida: 0Entrada: R1 = 7, R2 = 3, D = 6
Salida: 0,52381
Enfoque: el problema dado se puede resolver utilizando el algoritmo geométrico como se ilustra a continuación:
A partir de la imagen de arriba y usando el Teorema de Pitágoras , el coseno del ángulo de intersección de los dos círculos se puede encontrar usando la fórmula:
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to find the cosine of the // angle of the intersection of two // circles with radius R1 and R2 float angle(float R1, float R2, float D) { float ans = (R1 * R1 + R2 * R2 - D * D) / (2 * R1 * R2); // Return the cosine of the angle return ans; } // Driver Code int main() { float R1 = 3, R2 = 4; float D = 5; cout << angle(R1, R2, D); return 0; }
Java
// Java program for the above approach import java.io.*; class GFG{ // Function to find the cosine of the // angle of the intersection of two // circles with radius R1 and R2 static float angle(float R1, float R2, float D) { float ans = (R1 * R1 + R2 * R2 - D * D) / (2 * R1 * R2); // Return the cosine of the angle return ans; } // Driver Code public static void main (String[] args) { float R1 = 3, R2 = 4; float D = 5; System.out.println(angle(R1, R2, D)); } } // This code is contributed by Ankita saini
Python3
# Python3 program for the above approach # Function to find the cosine of the # angle of the intersection of two # circles with radius R1 and R2 def angle(R1, R2, D): ans = ((R1 * R1 + R2 * R2 - D * D) / (2 * R1 * R2)) # Return the cosine of the angle return ans # Driver Code if __name__ == '__main__': R1 = 3 R2 = 4 D = 5 print(angle(R1, R2, D)) # This code is contributed by ipg2016107
C#
// C# program for the above approach using System; class GFG{ // Function to find the cosine of the // angle of the intersection of two // circles with radius R1 and R2 static float angle(float R1, float R2, float D) { float ans = (R1 * R1 + R2 * R2 - D * D) / (2 * R1 * R2); // Return the cosine of the angle return ans; } // Driver Code public static void Main(string[] args) { float R1 = 3, R2 = 4; float D = 5; Console.Write(angle(R1, R2, D)); } } // This code is contributed by rutvik_56.
Javascript
<script> // Javascript program for the above approach // Function to find the cosine of the // angle of the intersection of two // circles with radius R1 and R2 function angle(R1, R2, D) { var ans = (R1 * R1 + R2 * R2 - D * D) / (2 * R1 * R2); // Return the cosine of the angle return ans; } // Driver Code var R1 = 3, R2 = 4; var D = 5; document.write(angle(R1, R2, D)); // This code is contributed by Ankita saini </script>
Producción:
0
Tiempo Complejidad: O(1)
Espacio Auxiliar: O(1)