【Java】外接円の半径と座標点をもとめる_Ver3
コード更新致しました。
固有処理基底クラスを更新したので、更新したコードを紹介いたします。
import static java.lang.Double.parseDouble;
import static java.lang.Integer.parseInt;
import static java.lang.Math.pow;
import static java.lang.Math.sqrt;
import static java.lang.System.out;
import volume0.BaseExe;
public class CircumscribedCircleofaTriangle extends BaseExe {
public static void main(String[] args) {
new CircumscribedCircleofaTriangle().exeSysIn();
}
@Override
protected void execute() throws Exception {
int dataCount = parseInt(nextLine());
for (int i = 0; i < dataCount; i++) {
double[] vertexList = getVertexList();
double[] pXY = getXY(vertexList);
out.printf("%.3f %.3f %.3f\n", pXY[0], pXY[1], getR(vertexList, pXY));
}
}
private double[] getVertexList() {
String[] vertexStrList = nextLine().split(" ");
double[] vertexList = new double[6];
for (int i = 0; i < 6; i++) {
vertexList[i] = parseDouble(vertexStrList[i]);
}
return vertexList;
}
private double[] getIn(double[] vertexList) {
double ax = vertexList[0];
double ay = vertexList[1];
double bx = vertexList[2];
double by = vertexList[3];
double cx = vertexList[4];
double cy = vertexList[5];
double abDX = bx - ax;
double abDY = by - ay;
double acDX = cx - ax;
double acDY = cy - ay;
double[] in = { abDX, abDY, getB(ax, ay, bx, by), acDX, acDY, getB(ax, ay, cx, cy) };
return in;
}
private double getR(double[] vertexList, double[] pXY) {
return sqrt(pow(vertexList[0] - pXY[0], 2) + pow(vertexList[1] - pXY[1], 2));
}
private double[] getXY(double[] vertexList) {
double[] in = getIn(vertexList);
double deno = (in[1] * in[3] - in[0] * in[4]);
double[] xy = new double[2];
xy[0] = judge((in[1] * in[5] - in[2] * in[4]) / deno);
xy[1] = judge((in[0] * in[5] - in[2] * in[3]) / (deno * -1));
return xy;
}
public double judge(double num) {
return (num == -0.0) ? 0.0 : num;
}
private double getB(double ax, double ay, double bx, double by) {
return (pow(by, 2) - pow(ay, 2) + pow(bx, 2) - pow(ax, 2)) / 2;
}
}
テストドライバは以下になります。
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import volume0.BaseTest;
public class CircumscribedCircleofaTriangleTest extends BaseTest {
private CircumscribedCircleofaTriangle cct;
@Before
public void setUp() throws Exception {
super.setUp();
cct = new CircumscribedCircleofaTriangle();
}
@After
public void tearDown() throws Exception {
super.tearDown();
}
@Test
public void test() {
cct.exeFileIn("./data/volume0_0010/in.txt");
assertOutList("./data/volume0_0010/out.txt");
}
}
テストドライバ内で使用している「in.txt」・「out.txt」の内容は以下になります。
in.txt
4
0.0 0.0 2.0 0.0 2.0 2.0
2.0 0.0 2.0 2.0 0.0 0.0
2.0 2.0 0.0 0.0 2.0 0.0
2.0 2.0 2.0 0.0 0.0 0.0
out.txt
1.000 1.000 1.414
1.000 1.000 1.414
1.000 1.000 1.414
1.000 1.000 1.414
1.000 1.000 1.414
Ver1で問題の解説等を行なっているので、そちらも参照してください。
アルゴリスムで困った時は、以下の本を参考にしてます。Javaに置き換えるのが少々難解ですが、、、
固有処理の基底クラスは以下の記事に記載していますので、参考にしてください。
テストドライバで継承しているクラスは以下の記事に記載していますので、参考にしてみてください。