An arctan2(y, x) that takes two arguments, instead of (or in addition to) the single-argument arctan(y/x)
You want this anytime you want to find the angle of a line connecting one location to another, like to point towards or to move towards another object. The current HS way seems to be of two things:
- use arctan(y/x) but with a conditional (“check once if”) on the sign of x, or
- use a more complicated single formula (no conditional)
but either way, you still need to watch out for division by zero.
Here is an example from the forums where arctan2() would have been useful:
With an arctan2(), you don’t need the check on If MCx > Rx.
Here is a project that uses method 1:
Notice here that if you get the hand exactly above or below the star, the hand points left instead of towards the star, because of pesky divide-by-zero.
This uses method 2:
Notice here that if you get the hand exactly to the right of the star, the hand points away from the star instead of towards it, because of pesky divide-by-zero.
You can completely and easily avoid these divide-by-zero glitches with a real arctan2(y,x).
@ThinBuffalo also implemented a full arctan2() within HS (actually, did so multiple times for different variables) in this project:
If you look in the code, you’ll see some “atan2” blocks that avoid all the divide-by-zero problems by using an additional check-if; in all 3 conditionals are used.
THT probably has a way of searching through all published projects to find uses of the current 1-arg arctan(). (@Awesome_E is this something possible with your player’s view of projects?) I would bet money that in > 95% of the uses, the thing being passed to arctan() is a quotient of the form y/x or (y2-y1)/(x2-x1). That is a clue that we really should be using arctan2(y, x) or arctan2(y2-y1, x2-x1) instead.
A two-argument arctan could look like any other math function that takes two labeled arguments (like random):
The problem is that HS has gone many years with a single-arg arctan, so it might seem too late to change anything now. But since atan2() is ubiquitous in other languages, because it really is a basic functionality useful in lots of game and graphics settings, and since this has been a source of questions on HSF more than once, it is worth figuring out a graceful transition to a two-argument arctan(y, x).
You could add a new “Inverse Tan (2-arg)” function button that goes alongside the current one-argument “Inverse Tangent” (which creates the current “arctan” function evaluation), and that could generate the arctan(y,x) function evaluation shown above. Having both a 1-arg and a 2-arg “Inverse Tan” in the math section is a bummer, but there’d be no ambiguity.
More daringly, you could just replace the single-argument arctan() with a two argument version, and in all existing code using arctan(), the current single argument would appear in the first slot of arctan(y,x), and the the second argument would appear pre-set as “1”. All the existing code using 1-arg arctan() would then work exactly the same; it just wouldn’t be taking advantage of the smarts built into the 2-arg arctan(y,x). Users could be invited to revisit their use of arctan(), and see if their code logic can be simplified (it nearly always could be).
On calling it arctan vs arctan2 within HS
In my mock-up above, arctan() is clearly taking two args, suggestively labeled, and consistent with how the rest of the world does atan2(y,x); see below. I think “arctan” is the right name even for this 2-argument function. From the standpoint of teaching people about coding with trig, I would rather the 1-arg arctan(y/x) never existed: the inverse tangent is a way to recover angles, and the simple robust way to recover the full range of angles is using both x and y in a 2-arg arctan(y, x). Dealing with a subtleties of making arctan(y/x) reliably recover angles is annoying and error-prone; that’s why atan2(y,x) is so common elsewhere.
If you web search:
when to use atan2
you’ll find pages saying essentially: you should probably be using atan2 instead of atan. This page suggests that scr4tch doesn’t have atan2, so someone coded up that demo.