解析几何,注意除数为零的时候特殊处理,即平行于x轴或y轴:
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for(int i = 0; i < n; ++i){
double a = sc.nextDouble(), b = sc.nextDouble(), c = sc.nextDouble(), d = sc.nextDouble();
double t1 = a, t2 = b; //记录(a, b)的初值
c = c - a; //把(a, b)平移到(0, 0),当然(c, d)也跟着平移
d = d - b;
a = 0;
b = 0;
if(d == 0){ //除数为零的时候特殊处理
double x1 = c/2, x2 = c/2, y1 = x1*Math.sqrt(3), y2 = -y1;
x1 += t1; //最后把答案都平移回来
x2 += t1;
y1 += t2;
y2 += t2;
f(x1, y1, x2, y2);
continue;
}
double k = c*c/(2*d) + d/2.0;
double A = 1 + c*c/(d*d), B = -2*k*c/d, C = k*k-c*c-d*d;
double x1 = (-B + Math.sqrt(B*B-4*A*C))/(2*A); //求根公式
double x2 = (-B - Math.sqrt(B*B-4*A*C))/(2*A);
double y1 = k - c/d*x1, y2 = k - c/d*x2;
x1 += t1; ////最后把答案都平移回来
x2 += t1;
y1 += t2;
y2 += t2;
f(x1, y1, x2, y2);
}
}
static void f(double x1, double y1, double x2, double y2){ //先打印较小坐标
if(x1 < x2) System.out.printf("%.2f %.2f %.2f %.2f\n", x1, y1, x2, y2);
if(x1 > x2) System.out.printf("%.2f %.2f %.2f %.2f\n", x2, y2, x1, y1);
if(x1 == x2){
if(y1 < y2) System.out.printf("%.2f %.2f %.2f %.2f\n", x1, y1, x2, y2);
else System.out.printf("%.2f %.2f %.2f %.2f\n", x2, y2, x1, y1);
}
}
}
京公网安备 11010502036488号