Apple HDU - 6206(JAVA高精度求三角形外心)

Apple HDU - 6206

Apple is Taotao’s favourite fruit. In his backyard, there are three apple trees with coordinates (x1,y1), (x2,y2), and (x3,y3). Now Taotao is planning to plant a new one, but he is not willing to take these trees too close. He believes that the new apple tree should be outside the circle which the three apple trees that already exist is on. Taotao picked a potential position (x,y)
of the new tree. Could you tell him if it is outside the circle or not?
Input
The first line contains an integer T, indicating that there are T(T≤30) cases.
In the first line of each case, there are eight integers x1,y1,x2,y2,x3,y3,x,y, as described above.
The absolute values of integers in input are less than or equal to 1,000,000,000,000
.
It is guaranteed that, any three of the four positions do not lie on a straight line.
Output
For each case, output “Accepted” if the position is outside the circle, or “Rejected” if the position is on or inside the circle.
Sample Input

3
-2 0 0 -2 2 0 2 -2
-2 0 0 -2 2 0 0 2
-2 0 0 -2 2 0 1 1

Sample Output

Accepted
Rejected
Rejected

题意:

给定四个点坐标,问最后一个点在前三个点构成的三角形的外接圆的园内还是圆外

分析:

这里写图片描述

因为每个数的绝对值是1e12所以再求距离的时候一定会有平方变成1e24,所以要用java高精度

code:

import java.util.*;
import java.math.*;

public class Main
{
    public static BigDecimal fun(BigDecimal x)
    {
        return x.multiply(x);
    }
    public static void main(String []args)
    {
        Scanner in = new Scanner(System.in);
        int Tcase = in.nextInt();
        // System.out.println(Tcase);
        while ( (Tcase --) > 0 )
        {

            BigDecimal 
            x1 = in.nextBigDecimal() ,y1 = in.nextBigDecimal(),
            x2 = in.nextBigDecimal() ,y2 = in.nextBigDecimal(),
            x3 = in.nextBigDecimal() ,y3 = in.nextBigDecimal(),
            x4 = in.nextBigDecimal() ,y4 = in.nextBigDecimal();


            BigDecimal a = y3.subtract(y2).multiply(new BigDecimal(2) ).multiply( x2.multiply(x2).add(y2.multiply(y2)) .subtract( x1.multiply(x1)).subtract( y1.multiply(y1)));
            BigDecimal b = y2.subtract(y1).multiply(new BigDecimal(2)).multiply( x3.multiply(x3).add( y3.multiply(y3)).subtract( x2.multiply(x2)).subtract(y2.multiply(y2)));
            BigDecimal c = y3.subtract(y2).multiply(new BigDecimal(4)).multiply(x2.subtract(x1));
            BigDecimal d = y2.subtract(y1).multiply(new BigDecimal(4)).multiply(x3.subtract(x2));

            BigDecimal x = a.subtract(b).divide(c.subtract(d));
            // System.out.println(a + " " + b + " " + c + " " + d );

            a = x3.subtract(x2).multiply(new BigDecimal(-2)).multiply( x2.multiply(x2).add( y2.multiply(y2)).subtract(x1.multiply(x1)).subtract( y1.multiply(y1)));
            b = x2.subtract(x1).multiply(new BigDecimal(2)).multiply( x3.multiply(x3).add(y3.multiply(y3)).subtract(x2.multiply(x2)).subtract( y2.multiply(y2)));
            c = y3.subtract(y2).multiply(new BigDecimal(4)).multiply(x2.subtract(x1));
            d = y2.subtract(y1).multiply(new BigDecimal(4)).multiply(x3.subtract(x2));

            BigDecimal y = a.add(b).divide(c.subtract(d));

            BigDecimal r = fun(x.subtract(x1)).add( fun(y.subtract(y1)) );
            BigDecimal temp = fun(x.subtract(x4)).add(fun(y.subtract(y4)) );

            // System.out.println(x + " " + y);
            if(temp.compareTo(r) == 1)System.out.println("Accepted");
            else System.out.println("Rejected");
        }
    }
}

猜你喜欢

转载自blog.csdn.net/codeswarrior/article/details/82690887