I am new to JavaFX and really weak in Trigonometry and Math and have been trying to find the angle (of a line) between two points. These two points revolve around a common central point in two different perfect circular orbits. These points represent Earth and Jupiter and I need to find the angle between Earth and Jupiter, to be exact, angle from Earth to Jupiter. Jupiter revolves in an orbit with higher radius as in our solar system. I tried 'atan' and 'atan2' but it doesn't give expected answer for all angles, I am too dumb to use it properly. Upon further research I found out something like ' it only calculates the angle with respect to +ve side of X axis', but I got the point. With further reading and help from 'Slope Calculator' (https://www.calculator.net/slope-calculator.html) I managed to solve that issue as they did. By adding appropriate degrees like 180,360 to the atan / atan2 results and getting correct and expected (by me) final results. This adding of 90/180/360 degrees (I haven't seen them adding 90 degrees, or why would one want to add 0 degree) is not fully understood by me. My poor Math skills says its the difference of the measured angle (the old +ve x axis, 360/180 - measured angle ?) to 360 degrees, or simply put, the unmeasured angle (total of 180 or 360 degrees) The problem is, the results are not always the expected, rarely it goes wrong, very big difference, totally wrong. This is owing to the adding of wrong degrees to atan / atan2 results. The method used at the Calculator Site gives correct results, except for line of 180 degrees (3'O clock to 9'O clock line), it gives 0 degrees. (Shouldn't be it 180 degrees? What ever, I need 180 degree for such a line). The site adds 180 degree or 360 degree to the result to get the final result and is correct and as per my requirement, expect for 3'O Clock ----> 9'O Clock line case. The angle for 9'O ---> 3'O is correct and as per my requirement. To figure out how much degrees to add to the atan / atan2 results, I am currently finding the slope of the line and adding 0/90/180/360 degrees to the atan / atan2 results and getting expected result even for 3'O clock ----> 9 'O clock line. Still something is wrong.
//PANE_HEIGHT is 960, the height and width of pane. PositionX is subtracted from it to get the //Cartesian plane coordinates instead of screen coordinates
currentJupiterAngleRetroRough = (Math.toDegrees(Math.atan((((PANE_HEIGHT - jupiterPositionY) - ( PANE_HEIGHT-earthPositionY))) / ((jupiterPositionX) - (earthPositionX)))));
//Finding the slope of the line
slope = (((PANE_HEIGHT-jupiterPositionY) - (PANE_HEIGHT-earthPositionY)) / ((jupiterPositionX) - (earthPositionX)));
//Adding required angles to output of atan to get final degrees,based on the slope
currentJupiterAngleRetro = (Math.toDegrees( Math.atan((((PANE_HEIGHT - jupiterPositionY) - ( PANE_HEIGHT-earthPositionY))) / ((jupiterPositionX) - (earthPositionX) )))) +
(slope<0 ? 360:(slope==0?0:(slope>0 & slope<1 ? 0:(slope>1 & slope<2 ? 90:180 ))));
//Various approaches to find the appropriate degrees to add to atan result
(slope<0 ? 360:180);
(slope<0 ? 360:(slope==0?0:180 ));
// Different One
// Another one
// and so on
(slope<0 ? 360:(slope==0?0:(slope>0 & slope<1 ? 0:(slope>1 & slope<2 ? 90:180 )))); //Improved one, still not fully correct
The app simulates the position of planets continuously for any date/time and continuously updates the position and degree in both graphics and text. So it is a time taking task to find if the calculations are wrong and I sure found it is, but very hard to find. Apart from that, the degrees which are to be calculated are of angle between planets that are retrograding (backward moving optical illusion), so way hard to spot the calculation errors by comparing to anything. Will have to log all the original angles,positions,retro angles to console and ready line by line to see big jumps or will have the watch the original angle and calculate the retro angle approximately in mind and verify.
It took two complete days to find a almost-fully-correct working solution and cant longer pull my hair. Have read similar questions on StackOverflow and even someone had almost the same issue, but no answer I believe, and the discussion were continued on chat but couldn't find more info. I hope it would be very easy for people who are good at math. Please provide a perfect solution. Thanks in advance.
You essentially have a vector from Earth to Jupiter and you want to find the angle (i.e. direction) of this vector. You also want the angle measured counterclockwise from the positive x-axis. What that means is you can measure the same angle using two vectors—your vector and the unit vector in the positive x direction. This is important because it can simplify the implementation since:
Point2D
class which can represent vectors and provides convenient methods (e.g.angle
).0
and180
degrees which I personally find easier to work with than inverse tangent's range of-90
to90
degrees.For example, if you have two points then you can calculate the angle between the implicit vector and the positive x-axis using the following:
Note the reason I use
vector.getY() > 0
rather thanvector.getY() < 0
is because JavaFX, like most(?) GUI frameworks, has the positive y direction pointing down the screen. Depending on how you represent the coordinate system in your model you may have to modify the code slightly.Here's an application demonstrating the above in a way I believe matches what you want:
And here's what the example looks like: