Is the java Math.atan() function known to have any issues? I'm using it in my code and for some bizarre reason that I can't work out, the function will return 0 for 45 degrees of motion, jumps to 45 and stays at 45 until it reaches 63.4 degrees where it starts mapping properly until it reaches -63.4 degrees and the problem repeats. I'm so confused! I've triple checked that my input is always correct but yet the problem recurs!
Here's my code
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));
Here's some printouts on test data
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
Any ideas?
You perform integer division, which results in round numbers, try:
Math.atan((double)opposite/adjacent);
You almost certainly want to be using Math.atan2(y, x)
, which is much smarter about pointing in the right direction, as opposed to Math.atan
, which has to assume that you're in one of the first two quadrants.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With