w3resource

Java: Compute the radius and the central coordinate of a circle


Compute Circle from Three Points

Write a Java program to compute the radius and central coordinates (x, y) of a circle constructed from three given points on the plane surface.

Input:

x1,y1,x2,y2,x3,y3,xp,yp separated by a single space.

Sample Solution:

Java Code:

// Importing necessary classes for input/output operations
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.BufferedReader;

// Main class named "Main"
class Main {
    // Main method with IOException in case of input error
    public static void main(String[] args) throws IOException {
        // Creating BufferedReader for efficient reading of input
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        // Prompting the user to input coordinates x1, y1, x2, y2, x3, y3 separated by a single space
        System.out.println("Input x1, y1, x2, y2, x3, y3 separated by a single space:");

        // Reading the input line and splitting it into an array of strings
        String[] input = br.readLine().split(" ");

        // Parsing the input strings into double values
        double x1 = Double.parseDouble(input[0]);
        double y1 = Double.parseDouble(input[1]);
        double x2 = Double.parseDouble(input[2]);
        double y2 = Double.parseDouble(input[3]);
        double x3 = Double.parseDouble(input[4]);
        double y3 = Double.parseDouble(input[5]);

        // Calculating intermediate values for further computations
        double A = x2 - x1;
        double B = y2 - y1;
        double p = (y2 * y2 - y1 * y1 + x2 * x2 - x1 * x1) / 2;
        double C = x3 - x1;
        double D = y3 - y1;
        double q = (y3 * y3 - y1 * y1 + x3 * x3 - x1 * x1) / 2;

        // Calculating the coordinates of the center (X, Y) and the radius (r) of the circle
        double X = (D * p - B * q) / (A * D - B * C);
        double Y = (A * q - C * p) / (A * D - B * C);

        double r = Math.sqrt(Math.pow(X - x1, 2.0) + Math.pow(Y - y1, 2.0));

        // Displaying the radius and the central coordinate of the circle
        System.out.println("\nRadius and the central coordinate:");
        System.out.printf("%.3f (%.3f %.3f)", r, X, Y);
    }
} 

Sample Output:

Input x1, y1, x2, y2, x3, y3 separated by a single space:
 5 6 4 8 7 9

Radius and the central coordinate:
1.821 (5.786 7.643)

Flowchart:

Flowchart: Java exercises: Compute the radius and the central coordinate of a circle.



For more Practice: Solve these Related Problems:

  • Write a Java program to compute the circle defined by three points and determine if the points are collinear.
  • Write a Java program to compute the circle from three points and then calculate its circumference and area.
  • Write a Java program to compute the circle that passes through three points using a determinant-based method.
  • Write a Java program to verify the computed circle by checking if a fourth point lies on its circumference.

Go to:


PREV : Count Prime Numbers ≤ N.
NEXT : Check Point Inside Triangle.


Java Code Editor:

Contribute your code and comments through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.