View Full Version : Useful functions

Robin

05-08-2008, 04:16 AM

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

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 + 1 : number;

}

function radian(degree) {

return (degree * 0.0174532925);

}

function degree(radian) {

return (radian * 57.2957795);

}

function square(n) {

return (n * 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, 04:19 AM

Oi! Very nice ;)

cbk1994

05-08-2008, 04: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

function random( x )

{

//blah

}

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

Robin

05-08-2008, 04: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, 04:48 AM

... for effciency's sake.

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

Robin

05-08-2008, 05:24 AM

function atan2(y,x) {

if (x < 0) {

return atan(y/x);

} elseif (x > 0) {

return atan(y/x) - radian(180);

} elseif (x == 0) {

if (y > 0) {

return radian(90);

} elseif (y < 0) {

return radian(-90);

} elseif (y==0) {

return 0;

}

}

}

Better? :P

cbk1994

05-08-2008, 05:29 AM

function floor(number) {

return int(number);

}

What does this do?

Simply for compatibility with your almighty ECMAScript Standards? ;)

DustyPorViva

05-08-2008, 05:31 AM

function ceil(number) {

return int(number) < number ? number + 1 ? number;

}

Shouldn't that be:

return int(number) < number ? number + 1 : number;

Robin

05-08-2008, 05:44 AM

function ceil(number) {

return int(number) < number ? number + 1 ? number;

}

Shouldn't that be:

return int(number) < number ? number + 1 : number;

You are correct sir! I saw it just now.

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

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.

There's degtorad and radtodeg built in.

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

function atan(number) {

return arctan(number);

}

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

function getnumberone() {

return 1;

}

cbk1994

05-08-2008, 11:16 PM

There's degtorad and radtodeg built in.

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

function atan(number) {

return arctan(number);

}

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

function getnumberone() {

return 1;

}

I agree, that's rather silly.

Robin

05-08-2008, 11:22 PM

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

cbk1994

05-08-2008, 11:25 PM

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, 11:26 PM

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.

vBulletin® v3.8.3, Copyright ©2000-2019, Jelsoft Enterprises Ltd.