java Math.atan()函数是否存在任何问题?我在我的代码中使用它,并且由于一些奇怪的原因我无法解决,函数将返回0表示45度运动,跳转到45并保持在45直到它达到63.4度,它开始正确映射,直到它达到-63.4度并且问题重复。我很困惑!我已经三次检查我的输入总是正确的但是问题又重现了!
这是我的代码
int adjacent = p2.x - p1.x;
if (adjacent == 0) adjacent = 1; //Prevent division by 0
int opposite = p2.y - p1.y;
double pheta = Math.atan(opposite/adjacent);
System.out.println(opposite + "/" + adjacent + ", pheta=" + Math.toDegrees(pheta));
以下是测试数据的一些打印输出
73/102, angle=0.0
73/101, angle=0.0
74/100, angle=0.0
74/99, angle=0.0
75/99, angle=0.0
76/97, angle=0.0
76/96, angle=0.0
77/95, angle=0.0
78/95, angle=0.0
78/94, angle=0.0
79/92, angle=0.0
80/91, angle=0.0
81/90, angle=0.0
81/89, angle=0.0
82/87, angle=0.0
83/86, angle=0.0
84/85, angle=0.0
84/84, angle=45.0
84/83, angle=45.0
85/83, angle=45.0
85/82, angle=45.0
85/81, angle=45.0
85/80, angle=45.0
86/79, angle=45.0
87/78, angle=45.0
87/77, angle=45.0
87/76, angle=45.0
88/75, angle=45.0
88/74, angle=45.0
88/73, angle=45.0
89/72, angle=45.0
90/70, angle=45.0
90/69, angle=45.0
91/68, angle=45.0
92/67, angle=45.0
92/66, angle=45.0
93/65, angle=45.0
93/64, angle=45.0
93/62, angle=45.0
94/62, angle=45.0
94/61, angle=45.0
95/61, angle=45.0
95/60, angle=45.0
95/59, angle=45.0
95/57, angle=45.0
95/56, angle=45.0
96/55, angle=45.0
96/54, angle=45.0
96/53, angle=45.0
97/53, angle=45.0
97/52, angle=45.0
97/51, angle=45.0
98/51, angle=45.0
98/50, angle=45.0
98/49, angle=63.43494882292201
98/47, angle=63.43494882292201
99/45, angle=63.43494882292201
99/42, angle=63.43494882292201
100/40, angle=63.43494882292201
100/38, angle=63.43494882292201
100/36, angle=63.43494882292201
100/34, angle=63.43494882292201
100/33, angle=71.56505117707799
101/33, angle=71.56505117707799
101/32, angle=71.56505117707799
101/31, angle=71.56505117707799
101/30, angle=71.56505117707799
102/29, angle=71.56505117707799
102/28, angle=71.56505117707799
102/27, angle=71.56505117707799
103/27, angle=71.56505117707799
103/25, angle=75.96375653207353
103/24, angle=75.96375653207353
103/23, angle=75.96375653207353
103/22, angle=75.96375653207353
104/21, angle=75.96375653207353
104/19, angle=78.69006752597979
105/18, angle=78.69006752597979
105/16, angle=80.53767779197439
106/15, angle=81.86989764584403
106/14, angle=81.86989764584403
106/13, angle=82.87498365109819
106/12, angle=82.87498365109819
106/11, angle=83.6598082540901
106/10, angle=84.28940686250037
106/9, angle=84.8055710922652
107/8, angle=85.60129464500447
108/6, angle=86.82016988013577
有什么想法吗?
答案 0 :(得分:9)
执行整数除法,得到整数,尝试:
Math.atan((double)opposite/adjacent);
答案 1 :(得分:2)
你几乎肯定想要使用Math.atan2(y, x)
,这比指向正确的方向更聪明,而不是Math.atan
,它必须假设你处于前两个之一象限。