Dado un círculo que tiene una cuerda en su interior. Se dan la longitud de la cuerda y el radio del círculo. La tarea es encontrar la distancia más corta desde la cuerda hasta el centro.
Ejemplos:
Input: r = 4, d = 3 Output: 3.7081 Input: r = 9.8, d = 7.3 Output: 9.09492
Enfoque :
- Sabemos que el segmento de línea que se deja caer desde el centro de la cuerda biseca la cuerda. La línea es la bisectriz perpendicular de la cuerda, y sabemos que la distancia perpendicular es la distancia más corta, por lo que nuestra tarea es encontrar la longitud de esta bisectriz perpendicular.
- sea radio del circulo = r
- longitud de la cuerda = d
- entonces, en el triángulo OBC ,
del teorema de Pitágoras ,
OB^2 + (d/2)^2 = r^2
entonces, OB = √(r^2 – d^2/4) - Asi que,
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program to find // the shortest distance from // chord to the center of circle #include <bits/stdc++.h> using namespace std; // Function to find the shortest distance void shortdis(double r, double d) { cout << "The shortest distance " << "from the chord to center " << sqrt((r * r) - ((d * d) / 4)) << endl; } // Driver code int main() { double r = 4, d = 3; shortdis(r, d); return 0; }
Java
// Java program to find // the shortest distance from // chord to the center of circle class GFG { // Function to find the shortest distance static void shortdis(double r, double d) { System.out.println("The shortest distance " + "from the chord to center " + (Math.sqrt((r * r) - ((d * d) / 4)))); } // Driver code public static void main(String[] args) { double r = 4, d = 3; shortdis(r, d); } } /* This code contributed by PrinciRaj1992 */
Python3
# Python program to find # the shortest distance from # chord to the center of circle # Function to find the shortest distance def shortdis(r, d): print("The shortest distance ",end=""); print("from the chord to center ",end=""); print(((r * r) - ((d * d) / 4))**(1/2)); # Driver code r = 4; d = 3; shortdis(r, d); # This code has been contributed by 29AjayKumar
C#
// C# program to find // the shortest distance from // chord to the center of circle using System; class GFG { // Function to find the shortest distance static void shortdis(double r, double d) { Console.WriteLine("The shortest distance " + "from the chord to center " + (Math.Sqrt((r * r) - ((d * d) / 4)))); } // Driver code public static void Main() { double r = 4, d = 3; shortdis(r, d); } } // This code is contributed by AnkitRai01
PHP
<?php // PHP program to find // the shortest distance from // chord to the center of circle // Function to find the shortest distance function shortdis($r, $d) { echo "The shortest distance "; echo "from the chord to center "; echo sqrt(($r * $r) - (($d * $d) / 4)); } // Driver code $r = 4; $d = 3; shortdis($r, $d); // This code is contributed by Naman_Garg. ?>
Javascript
<script> // JavaScript program to find // the shortest distance from // chord to the center of circle // Function to find the shortest distance function shortdis(r, d) { document.write("The shortest distance " + "from the chord to center " + Math.sqrt((r * r) - ((d * d) / 4)) + "<br>"); } // Driver code let r = 4, d = 3; shortdis(r, d); // This code is contributed by Surbhi Tyagi. </script>
Producción:
The shortest distance from the chord to centre 3.7081
Complejidad de tiempo: O(1)
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por IshwarGupta y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA