競プロ・数学を頑張りたい(願望)

競技プログラミングの問題を解いたときや数学に関してのメモにしようと思っています。競プロはAOJを、数学は数検準1を目標で。

AOJ0010 Circumscribed Circle of a Triangle

コメント

少し調べてみたけど本当に力押しで解くしかない問題。すごい疲れた。
表示するときの小数点の四捨五入はprintfメソッドがあるのでC言語のように簡単に行うことができる。(これ知らなくてちょっと感動した)

ソース

import java.util.Scanner;

public class Main {

	// printfの%.3fは小数第4位を四捨五入してくれる!
	void run() {
		Scanner sc = new Scanner(System.in);

		int n = sc.nextInt();

		for (int i = 0; i < n; i++) {
		  double x1 = sc.nextDouble();
		  double y1 = sc.nextDouble();
		  double x2 = sc.nextDouble();
	  	  double y2 = sc.nextDouble();
		  double x3 = sc.nextDouble();
		  double y3 = sc.nextDouble();

		  //連立3元1次方程式
		  double a = (x1 * x1) - (x2 * x2);
		  double b = 2 * (x2 - x1);
		  double c = (y1 * y1) - (y2 * y2);
		  double d = 2 * (y2 - y1);

		  double e = (x1 * x1) - (x3 * x3);
		  double f = 2 * (x3 - x1);
		  double g = (y1 * y1) - (y3 * y3);
		  double h = 2 * (y3 - y1);

		  // 円の中心座標cx,cy
		  double cx = (d * (g + e) - h * (a + c)) / (b * h - (d * f));
		  double cy = (b * (g + e) - f * (a + c)) / (d * f - (b * h));

		  // 円の半径r
		  double r = Math.sqrt(((x1 - cx) * (x1 - cx)) + ((y1 - cy) * (y1 - cy)));
		  System.out.printf("%.3f %.3f %.3f\n", cx, cy, r);

		}
		sc.close();
	}

    public static void main(String[] args) {
		new Main().run();
	}

}