Description
To calculate the circumference of a circle seems to be an easy task – provided you know its diameter. But what if you don’t?
You are given the cartesian coordinates of three non-collinear points in the plane.
Your job is to calculate the circumference of the unique circle that intersects all three points.
Input
The input will contain one or more test cases. Each test case consists of one line containing six real numbers x1,y1,x2,y2,x3,y3, representing the coordinates of the three points. The diameter of the circle determined by the three points will never exceed a million. Input is terminated by end of file.
Output
For each test case, print one line containing one real number telling the circumference of the circle determined by the three points. The circumference is to be printed accurately rounded to two decimals. The value of pi is approximately 3.141592653589793.
Sample Input
0.0 -0.5 0.5 0.0 0.0 0.5 0.0 0.0 0.0 1.0 1.0 1.0 5.0 5.0 5.0 7.0 4.0 6.0 0.0 0.0 -1.0 7.0 7.0 7.0 50.0 50.0 50.0 70.0 40.0 60.0 0.0 0.0 10.0 0.0 20.0 1.0 0.0 -500000.0 500000.0 0.0 0.0 500000.0
Sample Output
3.14 4.44 6.28 31.42 62.83 632.24 3141592.65
Solution below . . .
import java.util.Scanner; public class Main { static final double EPS = 1e-12; static double dot(PT p, PT q) { return p.x * q.x + p.y * q.y; } // distance squared static double dist2(PT p, PT q) { return dot(p.subtract(q), p.subtract(q)); } static double cross(PT p, PT q) { return p.x * q.y - p.y * q.x; } static PT RotateCW90(PT p) { return new PT(p.y, -p.x); } // compute intersection of line passing through a and b // with line passing through c and d, assuming that unique // intersection exists; for segment intersection, check if // segments intersect first static PT ComputeLineIntersection(PT a, PT b, PT c, PT d) { b = b.subtract(a); d = c.subtract(d); c = c.subtract(a); assert (dot(b, b) > EPS && dot(d, d) > EPS); return a.add(b.multiply(cross(c, d)).divide(cross(b, d))); } // compute center of circle given three points static PT ComputeCircleCenter(PT a, PT b, PT c) { b = (a.add(b)).divide(2); c = (a.add(c)).divide(2); return ComputeLineIntersection(b, b.add(RotateCW90(a.subtract(b))), c, c.add(RotateCW90(a.subtract(c)))); } public static void main(String[] args) { // long start = System.currentTimeMillis(); Scanner sc = new Scanner(System.in); Scanner lineTokenizer; while (sc.hasNextLine()) { lineTokenizer = new Scanner(sc.nextLine()); PT p1 = new PT(lineTokenizer.nextDouble(), lineTokenizer.nextDouble()); PT p2 = new PT(lineTokenizer.nextDouble(), lineTokenizer.nextDouble()); PT p3 = new PT(lineTokenizer.nextDouble(), lineTokenizer.nextDouble()); lineTokenizer.close(); PT center = ComputeCircleCenter(p1, p2, p3); // distance from point to center double radius = Math.sqrt(dist2(p1, center)); System.out.printf("%.2f\n", Math.PI * radius * 2); } sc.close(); // System.out.println("$:" + (System.currentTimeMillis() - start)); } } class PT { double x, y; PT() { } PT(double x, double y) { this.x = x; this.y = y; } PT(PT p) { this.x = p.x; this.y = p.y; } PT add(final PT p) { return new PT(x + p.x, y + p.y); } PT subtract(final PT p) { return new PT(x - p.x, y - p.y); } PT multiply(double c) { return new PT(x * c, y * c); } PT divide(double c) { return new PT(x / c, y / c); } @Override public String toString() { return "(" + x + "," + y + ")"; } }