Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Practice Mathematical Algorithm
  • Mathematical Algorithms
  • Pythagorean Triplet
  • Fibonacci Number
  • Euclidean Algorithm
  • LCM of Array
  • GCD of Array
  • Binomial Coefficient
  • Catalan Numbers
  • Sieve of Eratosthenes
  • Euler Totient Function
  • Modular Exponentiation
  • Modular Multiplicative Inverse
  • Stein's Algorithm
  • Juggler Sequence
  • Chinese Remainder Theorem
  • Quiz on Fibonacci Numbers
Open In App
Next Article:
Satisfy the parabola when point (A, B) and the equation is given
Next article icon

Equation of circle when three points on the circle are given

Last Updated : 14 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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: 

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 
Python
# 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> 

Output
Centre = (3, 2) Radius = 2.23607

Time Complexity: O(log(n))
Auxiliary Space: O(1)



Next Article
Satisfy the parabola when point (A, B) and the equation is given
author
gyanendra371
Improve
Article Tags :
  • DSA
  • Geometric
  • Mathematical
  • circle
Practice Tags :
  • Geometric
  • Mathematical

Similar Reads

  • Satisfy the parabola when point (A, B) and the equation is given
    Given a point (A, B) such that distance from each point on the curve M.y = N.x2 + O.x + P with (A, B) is equal to the distance between that point on the curve and x-axis. The task is to find the value of M, N O and P. Note: The equation should be in simple form i.e. gcd( |M|, |N|, |O|, |P| ) = 1 and
    4 min read
  • Equation of a normal to a Circle from a given point
    Given three integers a, b, c representing coefficients of the equation x2 + y2 + ax + by + c = 0 of a circle, the task is to find the equation of the normal to the circle from a given point (x1, y1).Note: Normal is a line perpendicular to the tangent at the point of contact between the tangent and t
    8 min read
  • Radii of the three tangent circles of equal radius which are inscribed within a circle of given radius
    Given here is a circle of a given radius. Inside it, three tangent circles of equal radius are inscribed. The task is to find the radii of these tangent circles. Examples: Input: R = 4Output: 1.858 Input: R = 11Output: 5.1095 Approach: Let the radii of the tangent circles be r, and the radius of the
    3 min read
  • Given equation of a circle as string, find area
    Given an equation of the circle X2 + Y2 = R2 whose center at origin (0, 0) and the radius is R. The task is to find area of circle. Examples : Input : X*X + Y*Y = 25Output : The area of circle centered at origin is : 78.55 Input : X*X + Y*Y = 64Output : The area of circle centered at origin is : 201
    7 min read
  • Queries on count of points lie inside a circle
    Given n coordinate (x, y) of points on 2D plane and Q queries. Each query contains an integer r, the task is to count the number of points lying inside or on the circumference of the circle having radius r and centered at the origin.Examples : Input : n = 5 Coordinates: 1 1 2 2 3 3 -1 -1 4 4 Query 1
    8 min read
  • Radius of a circle having area equal to the sum of area of the circles having given radii
    Given two integers r1 and r2, representing the radius of two circles, the task is to find the radius of the circle having area equal to the sum of the area of the two circles having given radii. Examples: Input:r1 = 8r2 = 6Output:10Explanation:Area of circle with radius 8 = 201.061929 Area of a circ
    5 min read
  • Check whether a point exists in circle sector or not.
    We have a circle centered at origin (0, 0). As input we are given with starting angle of the circle sector and the size of the circle sector in percentage. Examples: Input : Radius = 8 StartAngle = 0 Percentage = 12 x = 3 y = 4 Output : Point (3, 4) exists in the circle sector Input : Radius = 12 St
    5 min read
  • Radius of the circle when the width and height of an arc is given
    Given a circle in which the width and height of an arc are given. The task is to find the radius of the circle with the help of the width and height of the arc.Examples: Input: d = 4, h = 1 Output: The radius of the circle is 2.5 Input: d = 14, h = 8 Output: The radius of the circle is 7.0625 Approa
    4 min read
  • Find if a point lies inside, outside or on the circumcircle of three points A, B, C
    Given four non-collinear points A, B, C, and P. The task is to find whether point P lies inside, outside or on the circumcircle formed by points A, B, and C.Examples Input: A = {2, 8}, B = {2, 1}, C = {4, 5}, P = {2, 4}; Output: Point (2, 4) is inside the circumcircle. Input: A = {2, 8}, B = {2, 1},
    15+ min read
  • Check if two given circles touch or intersect each other
    There are two circles A and B with their centres C1(x1, y1) and C2(x2, y2) and radius R1 and R2. The task is to check both circles A and B touch each other or not. Examples : Input : C1 = (3, 4) C2 = (14, 18) R1 = 5, R2 = 8Output : Circles do not touch each other. Input : C1 = (2, 3) C2 = (15, 28) R
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences