HDU 2080 夹角有多大II

2022-04-12  本文已影响0人  itbird01

Problem Description

这次xhd面临的问题是这样的:在一个平面内有两个点,求两个点分别和原点的连线的夹角的大小。

注:夹角的范围[0,180],两个点不会在圆心出现。

Input

输入数据的第一行是一个数据T,表示有T组数据。
每组数据有四个实数x1,y1,x2,y2分别表示两个点的坐标,这些实数的范围是[-10000,10000]。

Output
对于每组输入数据,输出夹角的大小精确到小数点后两位。

Sample Input

2 1 1 2 2 1 1 1 0

Sample Output

0.00 45.00

根据余弦定理
cosA = (b² + c² - a²)/(2bc)
cosB = (c² + a² - b²)/(2ac)
cosC = (b² + a² - c²)/(2ab)

角度 = Math.atan((dpPoint.y-dpCenter.y) / (dpPoint.x-dpCenter.x)) / π(3.14) * 180度

用角度表示的角 B = Math.toDegrees(B);
java code

import java.text.DecimalFormat;
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        int n = cin.nextInt();
        DecimalFormat format = new DecimalFormat("0.00");
        for (int i = 0; i < n; i++) {
            // 坐标可能是小数
            double x1 = cin.nextDouble();
            double y1 = cin.nextDouble();
            double x2 = cin.nextDouble();
            double y2 = cin.nextDouble();
            double a = Math.sqrt(x1 * x1 + y1 * y1);
            double b = Math.sqrt(x2 * x2 + y2 * y2);
            double c = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
            double t = (a * a + b * b - c * c) / (2.0 * b * a);
            //角度 = Math.atan((dpPoint.y-dpCenter.y) / (dpPoint.x-dpCenter.x)) / π(3.14) * 180度
            // 用角度表示的角   B = Math.toDegrees(B);
            t = Math.acos(t) * 180 / Math.PI;
            while (t > 180)
                t -= 180;
            System.out.println(format.format(t));
        }
        cin.close();
    }

}```
上一篇 下一篇

猜你喜欢

热点阅读