Skip to content

Instantly share code, notes, and snippets.

@audreybongalon
Last active February 14, 2026 03:49
Show Gist options
  • Select an option

  • Save audreybongalon/52ba3bb9d9df8a5d30318b6f771bf8c1 to your computer and use it in GitHub Desktop.

Select an option

Save audreybongalon/52ba3bb9d9df8a5d30318b6f771bf8c1 to your computer and use it in GitHub Desktop.
reference for scratch games. scratch doesn't have an atan2 function, which is very useful for calculating directions (in terms of angles). so i made a reference file here. keep in mind that scratch automatically puts things in degrees, so there's no need to have a conversion function in scratch. also, sometimes this will return values 360 degree…
function toDegrees (angle) {
return angle * (180 / Math.PI);
}
function myAtan2(y, x) {
if (x > 0) {
return toDegrees(Math.atan(y/x));
}
else if (x < 0) {
return (180 + toDegrees(Math.atan(y/x)));
}
else if (y !== 0) {
return toDegrees(Math.atan(y/x));
}
else {
return "not a number";
}
}
var inputx = -1;
var inputy = -2;
var num1 = Math.atan(inputy, inputx);
var num2 = Math.atan2(inputy, inputx);
var num3 = myAtan2(inputy, inputx);
num1 = toDegrees(num1);
num2 = toDegrees(num2);
// num3 is converted to degrees in its function
console.log(" atan: " + num1);
console.log(" atan2: " + num2);
console.log("my atan2: " + num3);
console.log(toDegrees(Math.atan(inputy/inputx)));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment