Java HdAcm1174
空间一般直线的方程是:
(x-x0)/a=(y-y0)/b=(z-z0)/c,
这是一条过(x0,y0,z0),方向矢量为{a,b,c}的直线.
假设已知点的坐标是A(e,f,g),过A点,且与{a,b,c}垂直的平面是,
a(x-e)+b(y-f)+c(z-g)=0,直线(x-x0)/a=(y-y0)/b=(z-z0)/c,与这个平面的交点是B,
再由两点的距离公式求出AB,即得.
1 import java.util.Scanner; 2 3 4 public class Main { 5 6 7 8 public static void main(String[] args) { 9 10 Scanner cin = new Scanner(System.in); 11 int n = cin.nextInt(); 12 13 while(n != 0){ 14 float[] enemy = new float[5]; 15 for(int i = 0; i < 5; i++){ 16 enemy[i] = cin.nextFloat(); 17 } 18 19 float[] soldier = new float[8]; 20 for(int i = 0; i < 8; i++){ 21 soldier[i] = cin.nextFloat(); 22 } 23 24 boolean b = hit(enemy, soldier); 25 if(b){ 26 System.out.println("YES"); 27 } 28 else { 29 System.out.println("NO"); 30 } 31 n--; 32 } 33 cin.close(); 34 } 35 36 public static boolean hit(float[] enemy, float[] soldier){ 37 38 //敌人头的中心 h1,r1,x1,y1,z1 39 float r1 = enemy[1]; 40 //float[] centerEnemyHead = {enemy[2], enemy[3], enemy[0] - enemy[1] + enemy[4]}; 41 //警察瞄准敌人时人头的中心 h2,r2,x2,y2,z2,x3,y3,z3 42 float h2 = soldier[0]; 43 float r2 = soldier[1]; 44 float height = h2 * 0.9f - r2 ;//实际高度 低 0.1 45 46 float[] direction = {soldier[5], soldier[6], soldier[7]}; 47 48 float a = direction[0], b = direction[1], c = direction[2]; 49 float x0 = soldier[2], y0 = soldier[3], z0 = soldier[4] + height; 50 float e = enemy[2], f = enemy[3], g = enemy[0] - enemy[1] + enemy[4]; 51 52 float fenmu = a*a/b + b + c*c/b; 53 float fenzi = a*a*y0/b - a*x0 + a*e + b*f + c*c*y0/b - c*z0 +c*g; 54 55 float y = fenzi / fenmu; 56 57 float x = a*(y - y0)/b + x0; 58 float z = c*(y - y0)/b + z0; 59 //两点距离平方 60 float doubleR = (x-e)*(x-e) + (y-f)*(y-f) + (z-g)*(z-g); 61 62 if(doubleR > r1*r1) 63 return false; 64 else { 65 return true; 66 } 67 68 } 69 }