import java.io.*; public class Test { /****************/ /* main program */ /****************/ public static void main(String args[]) throws IOException { int n, n1, n2; BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); System.out.print("1番目の整数は? "); n1 = Integer.parseInt(in.readLine()); System.out.print("2番目の整数は? "); n2 = Integer.parseInt(in.readLine()); n = App.gcd(n1, n2); System.out.println(" 最大公約数は " + n + " です"); } } class App { /**********************/ /* xとyの最大公約数 */ /* x,y : データ */ /**********************/ static int gcd(int x, int y) { int r, g; r = x % y; if (r == 0) g = y; else g = gcd(y, r); return g; } }
import java.io.*; public class Test { /****************/ /* main program */ /****************/ public static void main(String args[]) throws IOException { int n, sw; BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); System.out.print("整数を入力してください "); n = Integer.parseInt(in.readLine()); sw = App.prime(n); if (sw > 0) System.out.println(" 与えられた数は素数です"); else System.out.println(" 与えられた数は素数ではありません"); } } class App { /*********************************/ /* 素数の判定 */ /* n : 整数データ */ /* return : =0 : 素数でない */ /* =1 : 素数 */ /*********************************/ static int prime(int n) { int i1, sw, m; m = (int)Math.sqrt((double)n); sw = (n <= 1) ? 0 : 1; for (i1 = 2; i1 <= m && sw > 0; i1++) { if (n%i1 == 0) sw = 0; } return sw; } }
import java.io.*; public class Test { /****************/ /* main program */ /****************/ public static void main(String args[]) throws IOException { double A[][], d; int i1, i2, n; BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); System.out.print("次数は? "); n = Integer.parseInt(in.readLine()); A = new double [n][n]; for (i1 = 0; i1 < n; i1++) { System.out.println((i1+1) + " 行目"); for (i2 = 0; i2 < n; i2++) { System.out.print(" " + (i2+1) + " 列目? "); A[i1][i2] = Double.parseDouble(in.readLine()); } } d = App.determinant(n, A); System.out.println("行列式の値は " + d); } } class App { /****************************/ /* 行列式の値 */ /* n : 行列式の次数 */ /* A : 行列式 */ /* return : 行列式の値 */ /****************************/ static double determinant(int n, double A[][]) { double d = 0.0, B[][], s = 1.0; int i1, i2, i3, k, m; if (n == 1) d = A[0][0]; else if (n == 2) d = A[0][0] * A[1][1] - A[1][0] * A[0][1]; else { m = n - 1; B = new double [m][m]; for (i1 = 0; i1 < n; i1++) { for (i2 = 0; i2 < m; i2++) { k = 0; for (i3 = 0; i3 < n; i3++) { if (i3 != i1) { B[i2][k] = A[i2+1][i3]; k++; } } } d += s * A[0][i1] * determinant(m, B); s = -s; } } return d; } }
import java.io.*; public class Test { /****************/ /* main program */ /****************/ public static void main(String args[]) throws IOException { double a, a1, b, b1, c, u = Math.PI / 180.0; BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); System.out.print("角度1? "); a = Double.parseDouble(in.readLine()); System.out.print("角度2? "); b = Double.parseDouble(in.readLine()); a1 = u * a; b1 = u * b; c = App.add(a1, b1) / u; System.out.println(" " + a + " + " + b + " = " + c); } } class App { /****************************/ /* 角度の和 */ /* a,b : 角度 */ /* return : a+b(±π) */ /****************************/ static double add(double a, double b) { double x; x = a + b; if (x > Math.PI) x -= 2.0 * Math.PI; else if (x < -Math.PI) x += 2.0 * Math.PI; return x; } }
import java.io.*; public class Test { /****************/ /* main program */ /****************/ public static void main(String args[]) throws IOException { double a, x[][] = new double [3][2]; BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); System.out.print("点Aの座標(x1)"); x[0][0] = Double.parseDouble(in.readLine()); System.out.print(" (y1)"); x[0][1] = Double.parseDouble(in.readLine()); System.out.print("点Bの座標(x2)"); x[1][0] = Double.parseDouble(in.readLine()); System.out.print(" (y2)"); x[1][1] = Double.parseDouble(in.readLine()); System.out.print("点Cの座標(x3)"); x[2][0] = Double.parseDouble(in.readLine()); System.out.print(" (y3)"); x[2][1] = Double.parseDouble(in.readLine()); a = App.triangle1(x); System.out.println(" 三角形ABCの面積: " + a); a = App.triangle2(x); System.out.println(" 三角形ABCの面積(ヘロンの公式): " + a); } } class App { /*******************************/ /* 三角形ABCの面積 */ /* A : (x[0][0], x[0][1]) */ /* B : (x[1][0], x[1][1]) */ /* C : (x[2][0], x[2][1]) */ /* return : 面積 */ /*******************************/ static double triangle1(double x[][]) { double area, a1, a2, ang, ba, bc, x1, y1; x1 = x[0][0] - x[1][0]; y1 = x[0][1] - x[1][1]; a1 = Math.atan2(y1, x1); ba = Math.sqrt(x1 * x1 + y1 * y1); x1 = x[2][0] - x[1][0]; y1 = x[2][1] - x[1][1]; a2 = Math.atan2(y1, x1); bc = Math.sqrt(x1 * x1 + y1 * y1); ang = Math.abs(App.add(a1, -a2)); area = 0.5 * ba * bc * Math.sin(ang); return area; } /****************************/ /* 角度の和 */ /* a,b : 角度 */ /* return : a+b(±π) */ /****************************/ static double add(double a, double b) { double x; x = a + b; if (x > Math.PI) x -= 2.0 * Math.PI; else if (x < -Math.PI) x += 2.0 * Math.PI; return x; } /**********************************/ /* 三角形ABCの面積(ヘロンの公式)*/ /* A : (x[0][0], x[0][1]) */ /* B : (x[1][0], x[1][1]) */ /* C : (x[2][0], x[2][1]) */ /* return : 面積 */ /**********************************/ static double triangle2(double x[][]) { double area, s, x1, y1, a[] = new double [3]; int i1; for (i1 = 0; i1 < 3; i1++) { x1 = x[i1][0] - x[(i1+1)%3][0]; y1 = x[i1][1] - x[(i1+1)%3][1]; a[i1] = Math.sqrt(x1 * x1 + y1 * y1); } s = 0.5 * (a[0] + a[1] + a[2]); area = Math.sqrt(s * (s - a[0]) * (s - a[1]) * (s - a[2])); return area; } }
import java.io.*; public class Test { /****************/ /* main program */ /****************/ public static void main(String args[]) throws IOException { double a, b, c, x0, y0, z0, r; BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); System.out.print(" 直線の方程式(a)? "); a = Double.parseDouble(in.readLine()); System.out.print(" 直線の方程式(b)? "); b = Double.parseDouble(in.readLine()); System.out.print(" 直線の方程式(c)? "); c = Double.parseDouble(in.readLine()); System.out.print(" 点の座標(x)? "); x0 = Double.parseDouble(in.readLine()); System.out.print(" 点の座標(y)? "); y0 = Double.parseDouble(in.readLine()); r = App.p_line_2(a, b, c, x0, y0); System.out.println(" 距離は " + r); } } class App { /************************************/ /* 平面上の点と直線との距離 */ /* (x0, y0) と ax + by + c = 0 */ /* return : 距離 */ /************************************/ static double p_line_2(double a, double b, double c, double x0, double y0) { double r; r = Math.abs(a * x0 + b * y0 + c) / Math.sqrt(a * a + b * b); return r; } }
import java.io.*; public class Test { /****************/ /* main program */ /****************/ public static void main(String args[]) throws IOException { double a1, a2, b1, b2, c1, c2; double x[] = new double [2]; int sw; BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); System.out.print("直線1の方程式(a1)"); a1 = Double.parseDouble(in.readLine()); System.out.print(" (b1)"); b1 = Double.parseDouble(in.readLine()); System.out.print(" (c1)"); c1 = Double.parseDouble(in.readLine()); System.out.print("直線2の方程式(a2)"); a2 = Double.parseDouble(in.readLine()); System.out.print(" (b2)"); b2 = Double.parseDouble(in.readLine()); System.out.print(" (c2)"); c2 = Double.parseDouble(in.readLine()); sw = App.cross(a1, b1, c1, a2, b2, c2, x); if (sw == 0) System.out.println(" 交点はありません!"); else System.out.println(" 交点の座標:(" + x[0] + ", " + x[1] + ")"); } } class App { /*********************************/ /* 二直線の交点 */ /* a1 * x + b1 * y + c1 = 0 */ /* a2 * x + b2 * y + c1 = 0 */ /* x : 交点の座標 */ /* return : =0 : 交点なし */ /* =1 : 交点あり */ /*********************************/ static int cross(double a1, double b1, double c1, double a2, double b2, double c2, double x[]) { double A[][] = new double [2][2]; double D, eps = 1.0e-10; int sw = 0; D = a1 * b2 - a2 * b1; if (Math.abs(D) > eps) { A[0][0] = b2 / D; A[0][1] = -b1 / D; A[1][0] = -a2 / D; A[1][1] = a1 / D; x[0] = -A[0][0] * c1 - A[0][1] * c2; x[1] = -A[1][0] * c1 - A[1][1] * c2; sw = 1; } return sw; } }
import java.io.*; public class Test { /****************/ /* main program */ /****************/ public static void main(String args[]) throws IOException { double a[] = new double [2], b[] = new double [2], p[] = new double [2]; int sw; BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); System.out.print("線分ABにおける点Aの座標(x)"); a[0] = Double.parseDouble(in.readLine()); System.out.print("線分ABにおける点Aの座標(y)"); a[1] = Double.parseDouble(in.readLine()); System.out.print("線分ABにおける点Bの座標(x)"); b[0] = Double.parseDouble(in.readLine()); System.out.print("線分ABにおける点Bの座標(y)"); b[1] = Double.parseDouble(in.readLine()); System.out.print("点Pの座標(x)"); p[0] = Double.parseDouble(in.readLine()); System.out.print("点Pの座標(y)"); p[1] = Double.parseDouble(in.readLine()); sw = App.ccw(a, b, p); if (sw == 1) System.out.print("点pは線分ABの反時計方向\n"); else if (sw == -1) System.out.print("点pは線分ABの時計方向\n"); else if (sw == 2) System.out.print("点pは線分ABの手前\n"); else if (sw == -2) System.out.print("点pは線分ABの先\n"); else System.out.print("点pは線分ABの上\n"); } } class App { /*************************/ /* ベクトルの大きさの2乗 */ /*************************/ static double norm(double x[]) { return x[0] * x[0] + x[1] * x[1]; } /****************************/ /* 2つのベクトルの内積 */ /* a,b : 2つのベクトル */ /****************************/ static double dot(double a[], double b[]) { return a[0] * b[0] + a[1] * b[1]; } /******************************/ /* 2つのベクトルの外積(z成分) */ /* a,b : 2つのベクトル */ /******************************/ static double cross(double a[], double b[]) { return a[0] * b[1] - a[1] * b[0]; } /***************************/ /* 点と線分の関係 */ /* p0,p1,p2 : 3つの点 */ /***************************/ static int ccw(double p0[], double p1[], double p2[]) { int sw = 0; double a[] = new double [2], b[] = new double [2], EPS = 1.0e-8; a[0] = p1[0] - p0[0]; a[1] = p1[1] - p0[1]; b[0] = p2[0] - p0[0]; b[1] = p2[1] - p0[1]; if ( cross(a, b) > EPS ) // 点p2は線分p0-p1の反時計方向 sw = 1; else if ( cross(a, b) < -EPS ) // 点p2は線分p0-p1の時計方向 sw = -1; else if ( dot(a, b) < -EPS ) // 点p2は線分p0-p1の手前 sw = 2; else if ( norm(a) < norm(b) ) // 点p2は線分p0-p1の先 sw = -2; return sw; // 点p2は線分p0-p1の上 } }
import java.io.*; public class Test { /****************/ /* main program */ /****************/ public static void main(String args[]) throws IOException { double a[] = new double [2], b[] = new double [2], c[] = new double [2], d[] = new double [2]; int sw; BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); System.out.print("線分ABにおける点Aの座標(x)"); a[0] = Double.parseDouble(in.readLine()); System.out.print("線分ABにおける点Aの座標(y)"); a[1] = Double.parseDouble(in.readLine()); System.out.print("線分ABにおける点Bの座標(x)"); b[0] = Double.parseDouble(in.readLine()); System.out.print("線分ABにおける点Bの座標(y)"); b[1] = Double.parseDouble(in.readLine()); System.out.print("線分CDにおける点Cの座標(x)"); c[0] = Double.parseDouble(in.readLine()); System.out.print("線分CDにおける点Cの座標(y)"); c[1] = Double.parseDouble(in.readLine()); System.out.print("線分CDにおける点Dの座標(x)"); d[0] = Double.parseDouble(in.readLine()); System.out.print("線分CDにおける点Dの座標(y)"); d[1] = Double.parseDouble(in.readLine()); sw = App.isIntersect(a, b, c, d); if (sw > 0) System.out.print("交点あり\n"); else System.out.print("交点なし\n"); } } class App { /*************************/ /* ベクトルの大きさの2乗 */ /*************************/ static double norm(double x[]) { return x[0] * x[0] + x[1] * x[1]; } /****************************/ /* 2つのベクトルの内積 */ /* a,b : 2つのベクトル */ /****************************/ static double dot(double a[], double b[]) { return a[0] * b[0] + a[1] * b[1]; } /******************************/ /* 2つのベクトルの外積(z成分) */ /* a,b : 2つのベクトル */ /******************************/ static double cross(double a[], double b[]) { return a[0] * b[1] - a[1] * b[0]; } /***************************/ /* 点と線分の関係 */ /* p0,p1,p2 : 3つの点 */ /***************************/ static int ccw(double p0[], double p1[], double p2[]) { int sw = 0; double a[] = new double [2], b[] = new double [2], EPS = 1.0e-8; a[0] = p1[0] - p0[0]; a[1] = p1[1] - p0[1]; b[0] = p2[0] - p0[0]; b[1] = p2[1] - p0[1]; if ( cross(a, b) > EPS ) // 点p2は線分p0-p1の反時計方向 sw = 1; else if ( cross(a, b) < -EPS ) // 点p2は線分p0-p1の時計方向 sw = -1; else if ( dot(a, b) < -EPS ) // 点p2は線分p0-p1の手前 sw = 2; else if ( norm(a) < norm(b) ) // 点p2は線分p0-p1の先 sw = -2; return sw; // 点p2は線分p0-p1の上 } /************************************/ /* 線分p1-p2と線分p3-p4の交点の有無 */ /************************************/ static int isIntersect(double p1[], double p2[], double p3[], double p4[]) { int sw = 0; // 交点なし if ( ccw(p1, p2, p3) * ccw(p1, p2, p4) <= 0 && ccw(p3, p4, p1) * ccw(p3, p4, p2) <= 0 ) sw = 1; // 交点あり return sw; } }
import java.io.*; public class Test { /****************/ /* main program */ /****************/ public static void main(String args[]) throws IOException { double a[] = new double [2], b[] = new double [2], c[] = new double [2], d[] = new double [2], p[] = new double [2]; int sw; BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); System.out.print("線分ABにおける点Aの座標(x)"); a[0] = Double.parseDouble(in.readLine()); System.out.print("線分ABにおける点Aの座標(y)"); a[1] = Double.parseDouble(in.readLine()); System.out.print("線分ABにおける点Bの座標(x)"); b[0] = Double.parseDouble(in.readLine()); System.out.print("線分ABにおける点Bの座標(y)"); b[1] = Double.parseDouble(in.readLine()); System.out.print("線分CDにおける点Cの座標(x)"); c[0] = Double.parseDouble(in.readLine()); System.out.print("線分CDにおける点Cの座標(y)"); c[1] = Double.parseDouble(in.readLine()); System.out.print("線分CDにおける点Dの座標(x)"); d[0] = Double.parseDouble(in.readLine()); System.out.print("線分CDにおける点Dの座標(y)"); d[1] = Double.parseDouble(in.readLine()); sw = App.cross(a, b, c, d, p); if (sw <= 0) System.out.print("No! ( " + sw + " )\n"); else System.out.print(p[0] + " " + p[1] + "\n"); } } class App { /*************************************/ /* 2線分の交点 */ /* A + r(B - A) と C + s(D - C) */ /* P : 交点の座標 */ /* return : =-1 : 交点が無い */ /* =0 : 交点が線分の外 */ /* =1 : 交点がある */ /*************************************/ static int cross(double A[], double B[], double C[], double D[], double P[]) { double r, s, AC[] = new double [2], BUNBO, EPS = 1.0e-8; int sw = -1; AC[0] = C[0] - A[0]; AC[1] = C[1] - A[1]; BUNBO = (B[0] - A[0]) * (D[1] - C[1]) - (B[1] - A[1]) * (D[0] - C[0]); if (Math.abs(BUNBO) > EPS) { r = ((D[1] - C[1]) * AC[0] - (D[0] - C[0]) * AC[1]) / BUNBO; s = ((B[1] - A[1]) * AC[0] - (B[0] - A[0]) * AC[1]) / BUNBO; if (r > -EPS && r < 1.0+EPS && s > -EPS && s < 1.0+EPS) { P[0] = A[0] + r * (B[0] - A[0]); P[1] = A[1] + r * (B[1] - A[1]); sw = 1; } else sw = 0; } return sw; } }
/****************************/ /* 座標軸の回転 */ /* coded by Y.Suganuma */ /****************************/ import java.io.*; class Test { public static void main (String[] args) { double a, x = 1.0, y = 1.0, X, Y; // 座標軸を45度回転する a = 45.0 * Math.PI / 180.0; X = x * Math.cos(a) + y * Math.sin(a); Y = -x * Math.sin(a) + y * Math.cos(a); System.out.println("x " + x + " y " + y); System.out.println("X " + X + " Y " + Y); } }
import java.io.*; public class Test { /****************/ /* main program */ /****************/ public static void main(String args[]) throws IOException { double ABCD[] = new double [4]; double x[][] = new double [3][3]; double eps = 1.0e-10; BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); System.out.print("1番目の点の座標(x1) "); x[0][0] = Double.parseDouble(in.readLine()); System.out.print(" (y1) "); x[0][1] = Double.parseDouble(in.readLine()); System.out.print(" (z1) "); x[0][2] = Double.parseDouble(in.readLine()); System.out.print("2番目の点の座標(x2) "); x[1][0] = Double.parseDouble(in.readLine()); System.out.print(" (y2) "); x[1][1] = Double.parseDouble(in.readLine()); System.out.print(" (z2) "); x[1][2] = Double.parseDouble(in.readLine()); System.out.print("3番目の点の座標(x3) "); x[2][0] = Double.parseDouble(in.readLine()); System.out.print(" (y3) "); x[2][1] = Double.parseDouble(in.readLine()); System.out.print(" (z3) "); x[2][2] = Double.parseDouble(in.readLine()); App.plane(x, ABCD); if (Math.abs(ABCD[0]) > eps) { if (ABCD[0] > 0) System.out.print(" " + ABCD[0] + " x"); else System.out.print(" -" + (-ABCD[0]) + " x"); } if (Math.abs(ABCD[1]) > eps) { if (ABCD[1] > 0) System.out.print(" + " + ABCD[1] + " y"); else System.out.print(" - " + (-ABCD[1]) + " y"); } if (Math.abs(ABCD[2]) > eps) { if (ABCD[2] > 0) System.out.print(" + " + ABCD[2] + " z"); else System.out.print(" - " + (-ABCD[2]) + " z"); } if (Math.abs(ABCD[3]) > eps) { if (ABCD[3] > 0) System.out.print(" + " + ABCD[3]); else System.out.print(" - " + (-ABCD[3])); } System.out.println(" = 0"); } } class App { /*****************************/ /* 三点を通る平面 */ /* Ax + Bx + Cx + D = 0 */ /* x : 3点 */ /* ABCD : A, B, C, D */ /*****************************/ static void plane(double x[][], double ABCD[]) { double A[][] = new double [3][3]; A[0][0] = x[0][1]; A[0][1] = x[0][2]; A[0][2] = 1.0; A[1][0] = x[1][1]; A[1][1] = x[1][2]; A[1][2] = 1.0; A[2][0] = x[2][1]; A[2][1] = x[2][2]; A[2][2] = 1.0; ABCD[0] = determinant(3, A); A[0][0] = x[0][0]; A[0][1] = x[0][2]; A[0][2] = 1.0; A[1][0] = x[1][0]; A[1][1] = x[1][2]; A[1][2] = 1.0; A[2][0] = x[2][0]; A[2][1] = x[2][2]; A[2][2] = 1.0; ABCD[1] = -determinant(3, A); A[0][0] = x[0][0]; A[0][1] = x[0][1]; A[0][2] = 1.0; A[1][0] = x[1][0]; A[1][1] = x[1][1]; A[1][2] = 1.0; A[2][0] = x[2][0]; A[2][1] = x[2][1]; A[2][2] = 1.0; ABCD[2] = determinant(3, A); A[0][0] = x[0][0]; A[0][1] = x[0][1]; A[0][2] = x[0][2]; A[1][0] = x[1][0]; A[1][1] = x[1][1]; A[1][2] = x[1][2]; A[2][0] = x[2][0]; A[2][1] = x[2][1]; A[2][2] = x[2][2]; ABCD[3] = -determinant(3, A); } /****************************/ /* 行列式の値 */ /* n : 行列式の次数 */ /* A : 行列式 */ /* return : 行列式の値 */ /****************************/ static double determinant(int n, double A[][]) { double d = 0.0, B[][], s = 1.0; int i1, i2, i3, k, m; if (n == 1) d = A[0][0]; else if (n == 2) d = A[0][0] * A[1][1] - A[1][0] * A[0][1]; else { m = n - 1; B = new double [m][m]; for (i1 = 0; i1 < n; i1++) { for (i2 = 0; i2 < m; i2++) { k = 0; for (i3 = 0; i3 < n; i3++) { if (i3 != i1) { B[i2][k] = A[i2+1][i3]; k++; } } } d += s * A[0][i1] * determinant(m, B); s = -s; } } return d; } }
import java.io.*; public class Test { /****************/ /* main program */ /****************/ public static void main(String args[]) throws IOException { double x0, x1, y0, y1, z0, z1, ram, myu, nyu, r; BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); System.out.print(" 直線の方程式(x0)? "); x0 = Double.parseDouble(in.readLine()); System.out.print(" 直線の方程式(y0)? "); y0 = Double.parseDouble(in.readLine()); System.out.print(" 直線の方程式(z0)? "); z0 = Double.parseDouble(in.readLine()); System.out.print(" (λ)? "); ram = Double.parseDouble(in.readLine()); System.out.print(" (μ)? "); myu = Double.parseDouble(in.readLine()); System.out.print(" (ν)? "); nyu = Double.parseDouble(in.readLine()); System.out.print(" 点の座標(x1)? "); x1 = Double.parseDouble(in.readLine()); System.out.print(" 点の座標(y1)? "); y1 = Double.parseDouble(in.readLine()); System.out.print(" 点の座標(z1)? "); z1 = Double.parseDouble(in.readLine()); r = App.p_line_3(x0, y0, z0, ram, myu, nyu, x1, y1, z1); System.out.println(" 距離は " + r); } } class App { /**********************************************************************/ /* 空間内の点と直線の距離 */ /* (x1, y1, z1) と (x - x0) / λ = (y - y0) / μ = (z - z0) / ν */ /* λ,μ,ν:方向余弦 */ /* return : 距離 */ /**********************************************************************/ static double p_line_3(double x0, double y0, double z0, double ram, double myu, double nyu, double x1, double y1, double z1) { double x, y, z, w, r; x = x1 - x0; y = y1 - y0; z = z1 - z0; w = ram * x + myu * y + nyu * z; r = Math.sqrt(Math.abs(x * x + y * y + z * z - w * w)); return r; } }
import java.io.*; public class Test { /****************/ /* main program */ /****************/ public static void main(String args[]) throws IOException { double ABCD[] = new double [4]; double x[] = new double [3]; double h; BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); System.out.print("点の座標(x) "); x[0] = Double.parseDouble(in.readLine()); System.out.print(" (y) "); x[1] = Double.parseDouble(in.readLine()); System.out.print(" (z) "); x[2] = Double.parseDouble(in.readLine()); System.out.print("平面の方程式(A) "); ABCD[0] = Double.parseDouble(in.readLine()); System.out.print(" (B) "); ABCD[1] = Double.parseDouble(in.readLine()); System.out.print(" (C) "); ABCD[2] = Double.parseDouble(in.readLine()); System.out.print(" (D) "); ABCD[3] = Double.parseDouble(in.readLine()); h = App.p_plane(x, ABCD); System.out.print(" 距離: " + h); } } class App { /*****************************/ /* 点と平面の距離 */ /* Ax + Bx + Cx + D = 0 */ /* x : 点 */ /* ABCD : A, B, C, D */ /*****************************/ static double p_plane(double x[], double ABCD[]) { double h; h = Math.abs(ABCD[0] * x[0] + ABCD[1] * x[1] + ABCD[2] * x[2] + ABCD[3]) / Math.sqrt(ABCD[0] * ABCD[0] + ABCD[1] * ABCD[1] + ABCD[2] * ABCD[2]); return h; } }
import java.io.*; public class Test { /****************/ /* main program */ /****************/ public static void main(String args[]) throws IOException { double x1, x2, y1, y2, z1, z2, u1, u2, v1, v2, w1, w2, r; BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); System.out.println("直線1の方程式"); System.out.print(" 点(x1)? "); x1 = Double.parseDouble(in.readLine()); System.out.print(" (y1)? "); y1 = Double.parseDouble(in.readLine()); System.out.print(" (z1)? "); z1 = Double.parseDouble(in.readLine()); System.out.print(" 方向比(u1)? "); u1 = Double.parseDouble(in.readLine()); System.out.print(" (v1)? "); v1 = Double.parseDouble(in.readLine()); System.out.print(" (w1)? "); w1 = Double.parseDouble(in.readLine()); System.out.println("直線2の方程式"); System.out.print(" 点(x2)? "); x2 = Double.parseDouble(in.readLine()); System.out.print(" (y2)? "); y2 = Double.parseDouble(in.readLine()); System.out.print(" (z2)? "); z2 = Double.parseDouble(in.readLine()); System.out.print(" 方向比(u2)? "); u2 = Double.parseDouble(in.readLine()); System.out.print(" (v2)? "); v2 = Double.parseDouble(in.readLine()); System.out.print(" (w2)? "); w2 = Double.parseDouble(in.readLine()); r = App.line_line(x1, y1, z1, u1, v1, w1, x2, y2, z2, u2, v2, w2); System.out.println(" 距離は " + r); } } class App { /******************************************************/ /* 二直線間の最短距離 */ /* (x - x1) / u1 = (y - y1) / v1 = (z - z1) / w1 */ /* u1,v1,w1:方向比 */ /* (x - x2) / u2 = (y - y2) / v2 = (z - z2) / w2 */ /* u2,v2,w2:方向比 */ /* return : 距離 */ /******************************************************/ static double line_line(double x1, double y1, double z1, double u1, double v1, double w1, double x2, double y2, double z2, double u2, double v2, double w2) { double A[][], D, x, y, z, r = 0.0, eps = 1.0e-10, ram, myu, nyu; int i1; x = v1 * w2 - v2 * w1; y = w1 * u2 - w2 * u1; z = u1 * v2 - u2 * v1; D = Math.sqrt(x * x + y * y + z * z); // 2直線が平行でない場合 if (D > eps) { A = new double [3][3]; A[0][0] = x2 - x1; A[0][1] = y2 - y1; A[0][2] = z2 - z1; A[1][0] = u1; A[1][1] = v1; A[1][2] = w1; A[2][0] = u2; A[2][1] = v2; A[2][2] = w2; r = Math.abs(App.determinant(3,A)) / D; } // 2直線が平行な場合 else { x = Math.sqrt(u1 * u1 + v1 * v1 + w1 * w1); ram = u1 / x; myu = v1 / x; nyu = w1 / x; r = App.p_line_3(x1, y1, z1, ram, myu, nyu, x2, y2, z2); } return r; } /****************************/ /* 行列式の値 */ /* n : 行列式の次数 */ /* A : 行列式 */ /* return : 行列式の値 */ /****************************/ static double determinant(int n, double A[][]) { double d = 0.0, B[][], s = 1.0; int i1, i2, i3, k, m; if (n == 1) d = A[0][0]; else if (n == 2) d = A[0][0] * A[1][1] - A[1][0] * A[0][1]; else { m = n - 1; B = new double [m][m]; for (i1 = 0; i1 < n; i1++) { for (i2 = 0; i2 < m; i2++) { k = 0; for (i3 = 0; i3 < n; i3++) { if (i3 != i1) { B[i2][k] = A[i2+1][i3]; k++; } } } d += s * A[0][i1] * determinant(m, B); s = -s; } } return d; } /**********************************************************************/ /* 空間内の点と直線との距離 */ /* (x1, y1, z1) と (x - x0) / λ = (y - y0) / μ = (z - z0) / ν */ /* λ,μ,ν:方向余弦 */ /* return : 距離 */ /**********************************************************************/ static double p_line_3(double x0, double y0, double z0, double ram, double myu, double nyu, double x1, double y1, double z1) { double x, y, z, w, r; x = x1 - x0; y = y1 - y0; z = z1 - z0; w = ram * x + myu * y + nyu * z; r = Math.sqrt(Math.abs(x * x + y * y + z * z - w * w)); return r; } }