Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   Code Gallery (https://forums.graalonline.com/forums/forumdisplay.php?f=179)
-   -   Useful functions (https://forums.graalonline.com/forums/showthread.php?t=79662)

Robin 05-08-2008 03:16 AM

Useful functions
 
Some general useful functions in GS2 :) (Some of them are for compatibility with JS/PHP/etc others are just good)
PHP Code:

function rand() {
  return new 
MRandomLCG().randFloat();
}
function 
tan(x) {
  return 
sin(x)/cos(x);
}
function 
atan2(y,x) {
  return 
x<>0?x>0?atan(y/x):atan(y/x)-radian(180):y>0?radian(90):radian(-90);
}
function 
atan(number) {
  return 
arctan(number);
}
function 
floor(number) {
  return 
int(number);
}
function 
ceil(number) {
  return 
int(number) < number number number;
}
function 
radian(degree) {
    return (
degree 0.0174532925);

function 
degree(radian) {
    return (
radian 57.2957795);

function 
square(n) {
    return (
n);

function 
norm(angle) {
    return (
angle 6.283185 floor((angle pi) / 6.283185));

function 
tile(pixel) {
    return (
pixel 16);

function 
pixel(tile) {
    return (
tile 16);


Enjoy :)

Tigairius 05-08-2008 03:19 AM

Oi! Very nice ;)

cbk1994 05-08-2008 03:24 AM

Not bad! :)

Some of these have been released by other people before (meaning, they made the same thing, not that you stole the script). By the way, you have

PHP Code:

function random)
{
  
//blah


Yet x is not used at all. Was this left over from before?

Robin 05-08-2008 03:36 AM

Ah yeah, I used to have rand.seed=x but it doesn't really change anything so I took it out for effciency's sake. Edited the post.

Tolnaftate2004 05-08-2008 03:48 AM

Quote:

Originally Posted by Robin (Post 1389554)
... for effciency's sake.

Cascade the 'if's in atan2... for efficiency's sake. ;)

Robin 05-08-2008 04:24 AM

PHP Code:


function atan2(y,x) {
  if (
0) {
    return 
atan(y/x);
  } elseif (
0) {
    return 
atan(y/x) - radian(180);
  } elseif (
== 0) {
    if (
0) {
      return 
radian(90);
    } elseif (
0) {
      return 
radian(-90);
    } elseif (
y==0) {
      return 
0;
    }
  }


Better? :P

cbk1994 05-08-2008 04:29 AM

PHP Code:

function floor(number) {
  return 
int(number);


What does this do?

Simply for compatibility with your almighty ECMAScript Standards? ;)

DustyPorViva 05-08-2008 04:31 AM

PHP Code:

function ceil(number) {
  return 
int(number) < number number number;


Shouldn't that be:
PHP Code:

return int(number) < number number number


Robin 05-08-2008 04:44 AM

Quote:

Originally Posted by DustyPorViva (Post 1389567)
PHP Code:

function ceil(number) {
  return 
int(number) < number number number;


Shouldn't that be:
PHP Code:

return int(number) < number number number


You are correct sir! I saw it just now.

Just for that, you get this special version of atan2 as your prize!
PHP Code:

function atan2(y,x) {
  return 
x<>0?x>0?atan(y/x):atan(y/x)-radian(180):y>0?radian(90):radian(-90);


That's right. They're nested.

Dan 05-08-2008 10:53 AM

There's degtorad and radtodeg built in.

Also, why have this? Besides the fact you lose 2 letters -.-
PHP Code:

function atan(number) {
  return 
arctan(number);


It's like the same as me doing a "getnumberone()" function for returning 1...
PHP Code:

function getnumberone() {
  return 
1;



cbk1994 05-08-2008 10:16 PM

Quote:

Originally Posted by Dan (Post 1389594)
There's degtorad and radtodeg built in.

Also, why have this? Besides the fact you lose 2 letters -.-
PHP Code:

function atan(number) {
  return 
arctan(number);


It's like the same as me doing a "getnumberone()" function for returning 1...
PHP Code:

function getnumberone() {
  return 
1;



I agree, that's rather silly.

Robin 05-08-2008 10:22 PM

ECMAScript compatibility, if you don't like it, don't use my code. Simple.

cbk1994 05-08-2008 10:25 PM

Quote:

Originally Posted by Robin (Post 1389693)
ECMAScript compatibility, if you don't like it, don't use my code. Simple.

No need to get angry. We simply find it strange you would make a new function when you could just type two or three extra characters.

Robin 05-08-2008 10:26 PM

Quote:

Originally Posted by cbkbud (Post 1389695)
No need to get angry. We simply find it strange you would make a new function when you could just type two or three extra characters.

Because I find myself always putting atan in instead of arctan, and wondering why things dont work properly since for some reason my console doesn't like to work and I can't see the error there.


All times are GMT +2. The time now is 11:33 AM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.