Dadas tres coordenadas que se encuentran en un círculo, (x1, y1) , (x2, y2) y (x3, y3) . La tarea es encontrar la ecuación del círculo y luego imprimir el centro y el radio del círculo.
La ecuación del círculo en forma general es x² + y² + 2gx + 2fy + c = 0 y en forma de radio es (x – h)² + (y -k)² = r² , donde (h, k) es el centro del círculo y r es el radio.
Ejemplos:
Entrada: x1 = 1, y1 = 0, x2 = -1, y2 = 0, x3 = 0, y3 = 1
Salida:
Centro = (0, 0)
Radio = 1
La ecuación del círculo es x 2 + y 2 = 1.
Entrada: x1 = 1, y1 = -6, x2 = 2, y2 = 1, x3 = 5, y3 = 2
Salida:
Centro = (5, -3)
Radio = 5
La ecuación del círculo es x 2 + y 2 -10x + 6y + 9 = 0
Enfoque: como sabemos, todos los tres puntos se encuentran en el círculo, por lo que satisfarán la ecuación de un círculo y al ponerlos en la ecuación general obtenemos tres ecuaciones con tres variables g , f y c , y resolviendo aún más tenemos puede obtener los valores. Podemos derivar la fórmula para obtener el valor de g, f y c como:
Poniendo coordenadas en la ecuación del círculo, obtenemos:
x1 2 + y1 2 + 2gx1 + 2fy1 + c = 0 – (1)
x2 2 + y2 2 + 2gx2 + 2fy2 + c = 0 – (2)
x3 2 + y3 2 + 2gx3 + 2fy3 + c = 0 – (3)
De (1) obtenemos , 2gx1 = -x1 2 – y1 2 – 2fy1 – c – (4)
De (1) obtenemos , c = -x1 2 – y1 2 – 2gx1 – 2fy1 – (5)
De (3) obtenemos , 2fy3 = -x3 2 – y3 2 – 2gx3 – c – (6)
Restando la ecuación (2) de la ecuación (1) obtenemos ,
2g( x1 – x2 ) = ( x2 2 -x1 2 ) + ( y2 2 – y1 2 ) + 2f( y2 – y1 ) – (A)
Ahora poniendo eqn ( 5) en (6) obtenemos ,
2fy3 = -x3 2 – y3 2 – 2gx3 + x1 2 + y1 2 + 2gx1 + 2fy1 – (7)
Ahora poniendo el valor de 2g de la ecuación (A) en (7) obtenemos ,
2f = ( ( x1 2 – x3 2 )( x1 – x2 ) +( y1 2 – y3 2 )( x1 – x2 ) + ( x2 2 – x1 2)( x1 – x3 ) + ( y2 2 – y1 2 )( x1 – x3 ) ) / ( y3 – y1 )( x1 – x2 ) – ( y2 – y1 )( x1 – x3 )
De manera similar podemos obtener los valores de 2g :
2g = ( ( x1 2 – x3 2 )( y1 – x2 ) +( y1 2 – y3 2 )( y1 – y2 ) + ( x2 2 – x1 2 )( y1 – y3) + ( y2 2 – y1 2 ) ( y1 – y3 ) ) / ( x3 -x1 )( y1 – y2 ) – ( x2 – x1 )( y1 – y3 )
Poniendo 2g y 2f en la ecuación (5) obtenemos el valor de c y sabemos que teníamos la ecuación de círculo como x 2 + y 2 + 2gx + 2fy + c = 0
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to find the circle on // which the given three points lie void findCircle(int x1, int y1, int x2, int y2, int x3, int y3) { int x12 = x1 - x2; int x13 = x1 - x3; int y12 = y1 - y2; int y13 = y1 - y3; int y31 = y3 - y1; int y21 = y2 - y1; int x31 = x3 - x1; int x21 = x2 - x1; // x1^2 - x3^2 int sx13 = pow(x1, 2) - pow(x3, 2); // y1^2 - y3^2 int sy13 = pow(y1, 2) - pow(y3, 2); int sx21 = pow(x2, 2) - pow(x1, 2); int sy21 = pow(y2, 2) - pow(y1, 2); int f = ((sx13) * (x12) + (sy13) * (x12) + (sx21) * (x13) + (sy21) * (x13)) / (2 * ((y31) * (x12) - (y21) * (x13))); int g = ((sx13) * (y12) + (sy13) * (y12) + (sx21) * (y13) + (sy21) * (y13)) / (2 * ((x31) * (y12) - (x21) * (y13))); int c = -pow(x1, 2) - pow(y1, 2) - 2 * g * x1 - 2 * f * y1; // eqn of circle be x^2 + y^2 + 2*g*x + 2*f*y + c = 0 // where centre is (h = -g, k = -f) and radius r // as r^2 = h^2 + k^2 - c int h = -g; int k = -f; int sqr_of_r = h * h + k * k - c; // r is the radius float r = sqrt(sqr_of_r); cout << "Centre = (" << h << ", " << k << ")" << endl; cout << "Radius = " << r; } // Driver code int main() { int x1 = 1, y1 = 1; int x2 = 2, y2 = 4; int x3 = 5, y3 = 3; findCircle(x1, y1, x2, y2, x3, y3); return 0; }
Java
// Java implementation of the approach import java.text.*; class GFG { // Function to find the circle on // which the given three points lie static void findCircle(int x1, int y1, int x2, int y2, int x3, int y3) { int x12 = x1 - x2; int x13 = x1 - x3; int y12 = y1 - y2; int y13 = y1 - y3; int y31 = y3 - y1; int y21 = y2 - y1; int x31 = x3 - x1; int x21 = x2 - x1; // x1^2 - x3^2 int sx13 = (int)(Math.pow(x1, 2) - Math.pow(x3, 2)); // y1^2 - y3^2 int sy13 = (int)(Math.pow(y1, 2) - Math.pow(y3, 2)); int sx21 = (int)(Math.pow(x2, 2) - Math.pow(x1, 2)); int sy21 = (int)(Math.pow(y2, 2) - Math.pow(y1, 2)); int f = ((sx13) * (x12) + (sy13) * (x12) + (sx21) * (x13) + (sy21) * (x13)) / (2 * ((y31) * (x12) - (y21) * (x13))); int g = ((sx13) * (y12) + (sy13) * (y12) + (sx21) * (y13) + (sy21) * (y13)) / (2 * ((x31) * (y12) - (x21) * (y13))); int c = -(int)Math.pow(x1, 2) - (int)Math.pow(y1, 2) - 2 * g * x1 - 2 * f * y1; // eqn of circle be x^2 + y^2 + 2*g*x + 2*f*y + c = 0 // where centre is (h = -g, k = -f) and radius r // as r^2 = h^2 + k^2 - c int h = -g; int k = -f; int sqr_of_r = h * h + k * k - c; // r is the radius double r = Math.sqrt(sqr_of_r); DecimalFormat df = new DecimalFormat("#.#####"); System.out.println("Centre = (" + h + "," + k + ")"); System.out.println("Radius = " + df.format(r)); } // Driver code public static void main (String[] args) { int x1 = 1, y1 = 1; int x2 = 2, y2 = 4; int x3 = 5, y3 = 3; findCircle(x1, y1, x2, y2, x3, y3); } } // This code is contributed by chandan_jnu
Python3
# Python3 implementation of the approach from math import sqrt # Function to find the circle on # which the given three points lie def findCircle(x1, y1, x2, y2, x3, y3) : x12 = x1 - x2; x13 = x1 - x3; y12 = y1 - y2; y13 = y1 - y3; y31 = y3 - y1; y21 = y2 - y1; x31 = x3 - x1; x21 = x2 - x1; # x1^2 - x3^2 sx13 = pow(x1, 2) - pow(x3, 2); # y1^2 - y3^2 sy13 = pow(y1, 2) - pow(y3, 2); sx21 = pow(x2, 2) - pow(x1, 2); sy21 = pow(y2, 2) - pow(y1, 2); f = (((sx13) * (x12) + (sy13) * (x12) + (sx21) * (x13) + (sy21) * (x13)) // (2 * ((y31) * (x12) - (y21) * (x13)))); g = (((sx13) * (y12) + (sy13) * (y12) + (sx21) * (y13) + (sy21) * (y13)) // (2 * ((x31) * (y12) - (x21) * (y13)))); c = (-pow(x1, 2) - pow(y1, 2) - 2 * g * x1 - 2 * f * y1); # eqn of circle be x^2 + y^2 + 2*g*x + 2*f*y + c = 0 # where centre is (h = -g, k = -f) and # radius r as r^2 = h^2 + k^2 - c h = -g; k = -f; sqr_of_r = h * h + k * k - c; # r is the radius r = round(sqrt(sqr_of_r), 5); print("Centre = (", h, ", ", k, ")"); print("Radius = ", r); # Driver code if __name__ == "__main__" : x1 = 1 ; y1 = 1; x2 = 2 ; y2 = 4; x3 = 5 ; y3 = 3; findCircle(x1, y1, x2, y2, x3, y3); # This code is contributed by Ryuga
C#
// C# implementation of the approach using System; class GFG { // Function to find the circle on // which the given three points lie static void findCircle(int x1, int y1, int x2, int y2, int x3, int y3) { int x12 = x1 - x2; int x13 = x1 - x3; int y12 = y1 - y2; int y13 = y1 - y3; int y31 = y3 - y1; int y21 = y2 - y1; int x31 = x3 - x1; int x21 = x2 - x1; // x1^2 - x3^2 int sx13 = (int)(Math.Pow(x1, 2) - Math.Pow(x3, 2)); // y1^2 - y3^2 int sy13 = (int)(Math.Pow(y1, 2) - Math.Pow(y3, 2)); int sx21 = (int)(Math.Pow(x2, 2) - Math.Pow(x1, 2)); int sy21 = (int)(Math.Pow(y2, 2) - Math.Pow(y1, 2)); int f = ((sx13) * (x12) + (sy13) * (x12) + (sx21) * (x13) + (sy21) * (x13)) / (2 * ((y31) * (x12) - (y21) * (x13))); int g = ((sx13) * (y12) + (sy13) * (y12) + (sx21) * (y13) + (sy21) * (y13)) / (2 * ((x31) * (y12) - (x21) * (y13))); int c = -(int)Math.Pow(x1, 2) - (int)Math.Pow(y1, 2) - 2 * g * x1 - 2 * f * y1; // eqn of circle be x^2 + y^2 + 2*g*x + 2*f*y + c = 0 // where centre is (h = -g, k = -f) and radius r // as r^2 = h^2 + k^2 - c int h = -g; int k = -f; int sqr_of_r = h * h + k * k - c; // r is the radius double r = Math.Round(Math.Sqrt(sqr_of_r), 5); Console.WriteLine("Centre = (" + h + "," + k + ")"); Console.WriteLine("Radius = " + r); } // Driver code static void Main() { int x1 = 1, y1 = 1; int x2 = 2, y2 = 4; int x3 = 5, y3 = 3; findCircle(x1, y1, x2, y2, x3, y3); } } // This code is contributed by chandan_jnu
Javascript
<script> // Function to find the circle on // which the given three points lie function findCircle(x1, y1, x2, y2, x3, y3) { var x12 = (x1 - x2); var x13 = (x1 - x3); var y12 =( y1 - y2); var y13 = (y1 - y3); var y31 = (y3 - y1); var y21 = (y2 - y1); var x31 = (x3 - x1); var x21 = (x2 - x1); //x1^2 - x3^2 var sx13 = Math.pow(x1, 2) - Math.pow(x3, 2); // y1^2 - y3^2 var sy13 = Math.pow(y1, 2) - Math.pow(y3, 2); var sx21 = Math.pow(x2, 2) - Math.pow(x1, 2); var sy21 = Math.pow(y2, 2) - Math.pow(y1, 2); var f = ((sx13) * (x12) + (sy13) * (x12) + (sx21) * (x13) + (sy21) * (x13)) / (2 * ((y31) * (x12) - (y21) * (x13))); var g = ((sx13) * (y12) + (sy13) * (y12) + (sx21) * (y13) + (sy21) * (y13)) / (2 * ((x31) * (y12) - (x21) * (y13))); var c = -(Math.pow(x1, 2)) - Math.pow(y1, 2) - 2 * g * x1 - 2 * f * y1; // eqn of circle be // x^2 + y^2 + 2*g*x + 2*f*y + c = 0 // where centre is (h = -g, k = -f) and radius r // as r^2 = h^2 + k^2 - c var h = -g; var k = -f; var sqr_of_r = h * h + k * k - c; // r is the radius var r = Math.sqrt(sqr_of_r); document.write("Centre = (" + h + ", "+ k +")" + "<br>"); document.write( "Radius = " + r.toFixed(5)); } var x1 = 1, y1 = 1; var x2 = 2, y2 = 4; var x3 = 5, y3 = 3; findCircle(x1, y1, x2, y2, x3, y3); </script>
Centre = (3, 2) Radius = 2.23607
Complejidad de tiempo: O (logn)
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por gyanendra371 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA