2-argument arctan2(y,x) would simplify "move towards"

A feature I’d like to see in Hopscotch is:

An arctan2(y, x) that takes two arguments, instead of (or in addition to) the single-argument arctan(y/x)

I’d use it for this type of project:

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:

  1. use arctan(y/x) but with a conditional (“check once if”) on the sign of x, or
  2. use a more complicated single formula (no conditional)

but either way, you still need to watch out for division by zero.

Here are some examples of Hopscotch code that could be improved if we had this:

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.

Here is an example design I’ve made:

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.

I’ve seen something similar in another programming languages, it looks like this:

The rest of the world calls it “atan2”: in JavaScript it’s Math.atan2(); in Python it’s math.atan2(); in C/C++ it’s atan2(). There’s a wikipedia page about it: atan2 - Wikipedia
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.


This is definitely too hard for my little brain, idk why but it seems cool to see that in hs


I think the real annoyances of making arctan(y/x) do what you want are part of the reason people are spooked by coding with trig. It can all be avoided with a proper 2-arg arctan.


I understand this, but I don’t think this would be correct


can you explain more: what would not be correct?


I think arctan should stay.

@anisotr0py, I ain’t as smart as Awesome E, but you’re right, it is too late.

Even though many people here may understand you, if you plop the block in the editor, people ain’t gonna know what you’re talking about.

Hopscotch is for children, and not all children might understand. It might take the fun out of coding.

Like I understand and love your idea, but look; not everyone might understand it.


Everybody not understanding it but that doesn’t matter

You don’t have to remove anything, add both.


Ok but this can be added too


But I think that the point of Hopscotch is to simplify coding, but maybe in the Math section there could be a Advanced part, where all the advanced stuff is there, and there could be videos about it.


Ok but


I like the idea nevertheless


well, you can click and hold to have more information on the block


@AECREATION do you know examples of HS programs where arctan() is being used, but the 2-arg arctan(y, x) would not have been simpler to use? I’d like to learn.

I’m trying to understand what’s more advanced about arctan2(y, x) compared to arctan(y/x). With the examples I found, if arctan2() had existed those HS programs would be simpler to code and/or be less glitchy.

Talking about HS language evolution means talking about how things can change. HS experts can stick with 1-arg arctan(), but arctan2(y, x) would simplify things for anyone new coming to HS, asking “how do I get the direction from this object to that object?”. The answer is: use arctan2(y diff, x diff) and you’re done. And those people will then understand the role of atan2() if they graduate to other languages.


I don’t usually use arctan but I like your idea, I just had some ideas to add of of it


I second this idea! I don’t personally find myself using atan very much as of right now, but I’d imagine that this would be very convenient for implementing anything directional trigonometrically.


Yes. Go to the web explorer, play a project, click the list button next to the x, then click the pencil. You should see a new editor window pop up with that project. Tap the search button and search for the block name (arctan) and all matches will show up.


You have a good point – although I’d probably go for a different name. It’d definitely be a super useful addition, considering I copy and paste any point towards code currently.


Cool, thank you. But is there a way to survey all the projects that have been opened in your player (or all published projects, period), to see how arctan() has been used? It would be nice to have this usage evidence to help argue that arctan2() is worth adding.


I have project stats, but no, I can only access the data of one Hopscotch project at a time.


Reflecting on the feedback from @AECREATION, maybe it was a mistake for me to call this “arctan2”. I wasn’t thinking like a new coder when I suggested “arctan2”: was thinking like someone who’s used atan2 for many years.

If I’m claiming that this is the one function you need when you want the angle of an x,y pair, then maybe HS can just call it “Angle of”:

This is more self-explanatory than “arctan2” (and the existing “arctan”): the name can show its purpose, not the trig implementation.

The documentation from long press can explain more how to use “angle of”, and maybe note that angleof(x,y) is atan2(y,x), but that is just fun details for the trig-curious, rather than essential info. The existing 1-arg “Inverse Tangent / arctan” should stay as it is, people will reach for “angle of” when they want angles.

What do you think?


This is a great idea I think! It would simplify a lot of game mechanics. I also like the concept of naming it “angle of” - THT could just state in the help menu for the block which mathematical formula it maps to. I think it would be less confusing for a lot of beginners or to those who simply don’t know these mathematical functions.