![]() |
VOOZH | about |
Given three coordinates that lie on a circle, (x1, y1), (x2, y2), and (x3, y3). The task is to find the equation of the circle and then print the centre and the radius of the circle.
The equation of circle in general form is x² + y² + 2gx + 2fy + c = 0 and in radius form is (x - h)² + (y -k)² = r², where (h, k) is the centre of the circle and r is the radius.
Examples:
Input: x1 = 1, y1 = 0, x2 = -1, y2 = 0, x3 = 0, y3 = 1
Output:
Centre = (0, 0)
Radius = 1
The equation of the circle is x2 + y2 = 1.
Input: x1 = 1, y1 = -6, x2 = 2, y2 = 1, x3 = 5, y3 = 2
Output: Centre = (5, -3)
Radius = 5
Equation of the circle is x2 + y2 -10x + 6y + 9 = 0
Approach: As we know all three-point lie on the circle, so they will satisfy the equation of a circle and by putting them in the general equation we get three equations with three variables g, f, and c, and by further solving we can get the values. We can derive the formula to obtain the value of g, f, and c as:
Putting coordinates in eqn of circle, we get:
x12 + y12 + 2gx1 + 2fy1 + c = 0 – (1)
x22 + y22 + 2gx2 + 2fy2 + c = 0 – (2)
x32 + y32 + 2gx3 + 2fy3 + c = 0 – (3)
From (1) we get, 2gx1 = -x12 - y12 - 2fy1 - c - (4)
From (1) we get, c = -x12 - y12 - 2gx1 - 2fy1 - (5)
From (3) we get, 2fy3 = -x32 - y32 - 2gx3 - c - (6)
Subtracting eqn (2) from eqn (1) we get,
2g( x1 - x2 ) = ( x22 -x12 ) + ( y22 - y12 ) + 2f( y2 - y1 ) - (A)
Now putting eqn (5) in (6) we get,
2fy3 = -x32 - y32 - 2gx3 + x12 + y12 + 2gx1 + 2fy1 - (7)
Now putting value of 2g from eqn (A) in (7) we get,
2f = ( ( x12 - x32 )( x1 - x2 ) +( y12 - y32 )( x1 - x2 ) + ( x22 - x12 )( x1 - x3 ) + ( y22 - y12 )( x1 - x3 ) ) / ( y3 - y1 )( x1 - x2 ) - ( y2 - y1 )( x1 - x3 )
Similarly we can obtain the values of 2g :
2g = ( ( x12 - x32 )( y1 - y2 ) +( y12 - y32 )( y1 - y2 ) + ( x22 - x12 )( y1 - y3) + ( y22 - y12 )( y1 - y3 ) ) / ( x3 -x1 )( y1 - y2 ) - ( x2 - x1 )( y1 - y3 )
Putting 2g and 2f in eqn (5) we get the value of c and know we had the equation of circle as x2 + y2 + 2gx + 2fy + c = 0
Below is the implementation of the above approach:
Centre = (3, 2) Radius = 2.23607
Time Complexity: O(log(n))
Auxiliary Space: O(1)