思想
海伦公式用来求点到直线的距离。
代码
import java.io.*;
public class Main{
static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
public static int nextInt()throws IOException{
in.nextToken();
return (int)in.nval;
}
public static double nextDouble()throws IOException{
in.nextToken();
return in.nval;
}
public static void main(String[] args)throws IOException{
int n = nextInt();
while(n-->0){
double x1 = nextDouble(), y1 = nextDouble();
double x2 = nextDouble(), y2 = nextDouble();
double x3 = nextDouble(), y3 = nextDouble();
double s = 0.5*Math.abs(x1*y2+x2*y3+x3*y1-x1*y3-x2*y1-x3*y2);
double l_AB = Math.sqrt(Math.pow(x1-x2, 2)+Math.pow(y1-y2, 2));
out.printf("%.2f\n", 2.0*s/l_AB); //因为三角形的面积=底*高/2,所以反算长度的时候要记得乘以2
}
out.flush();
out.close();
}
}