00:00
00:00
Newgrounds Background Image Theme

PalmVoe just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

Foss: Custom Functions

6,846 Views | 106 Replies
New Topic Respond to this Topic

Response to Foss: Custom Functions 2005-11-04 13:15:56


At 11/4/05 01:06 PM, T-H wrote: almost 8000 liam

I noticed I had 7, 550 the other day.. heh, I just didn't notice my last post was my 8000th so I didn't announce it.. and I misspelled Stem, heh.. <3

//bleh, forgot to copy+paste this into the other post so sorry for the double post.


Sup, bitches :)

BBS Signature

Response to Foss: Custom Functions 2005-11-04 13:40:23


whats a taylor approximation? im just using the exponential function

sinh(x) =( exp(x)-exp(-x) )/2

Response to Foss: Custom Functions 2005-11-04 13:45:48


At 11/4/05 01:40 PM, dELta_Luca wrote: whats a taylor approximation?

Ive got something that might be it written down in my book:

for R >=0
sinX = X - ((X^3) / (3!)) + ((X^5) / (5!)) - .......R iterations....
+ ((-1)^R) * ((X^(2R + 1)) / (2R + 1)!

Gives answer in radiens, not sure if its taylor or not. but using this for Math.sin would be parsing as authorblues said.

Response to Foss: Custom Functions 2005-11-04 13:46:53


At 11/4/05 01:40 PM, dELta_Luca wrote: whats a taylor approximation? im just using the exponential function

The taylor series, hard hard maths =(


Sup, bitches :)

BBS Signature

Response to Foss: Custom Functions 2005-11-04 13:59:11


ive also found this definition for sin(x) which is for real and complex numbers

sin(x) = e^(ix) - e^(-ix) / (2i)

Response to Foss: Custom Functions 2005-11-04 14:26:53


function limit(v:Number, low:Number, high:Number):Number {
if (v<low) {
return low;
} else if (v>high) {
return high;
} else {
return v;
}
}

Returns the value 'v' forced to stay within the specified limits.
I know it's incredibly basic, pretty much all my other stored custom functions have already been covered =(


BBS Signature

Response to Foss: Custom Functions 2005-11-04 14:39:16


At 11/4/05 02:26 PM, Rantzien wrote: function limit(v:Number, low:Number, high:Number):Number {
if (v<low) {
return low;
} else if (v>high) {
return high;
} else {
return v;
}
}

Usage:

trace(limit(10, 5, 10)); //trace 10.

Not really useful at all, but I guess it could be used for limiting speed and such...


Sup, bitches :)

BBS Signature

Response to Foss: Custom Functions 2005-11-04 15:08:45


At 11/4/05 02:39 PM, -liam- wrote: Not really useful at all, but I guess it could be used for limiting speed and such...

Yah, what I meant was to use it like, for example:
onEnterFrame = function() {
if(Key.isDown(Key.UP)) speed++;
if(Key.isDown(Key.DOWN)) speed--;
speed = limit(speed, -10, 10);
}

Remember that me === lazyness =)


BBS Signature

Response to Foss: Custom Functions 2005-11-04 15:10:44


If you have any ideas for custom functions, but aren't able to do them yourself, then post them.. we need ideas =P Else we'll be stuck making functions like this:

function getMid(low:Number, high:Number):Number {
return ((high-low)/2)+low
}

Usage:

trace(getMid(5, 10)); //7.5
trace(getMid(0.5, 1.6)); //1.05

Ideas are good.


Sup, bitches :)

BBS Signature

Response to Foss: Custom Functions 2005-11-04 15:12:58


At 11/4/05 02:26 PM, Rantzien wrote: function limit(v:Number, low:Number, high:Number):Number {
if (v<low) {
return low;
} else if (v>high) {
return high;
} else {
return v;
}
}

function limit(v:Number, low:Number, high:Number):Number {
return v<low?low:v>high?high:v
}

:)


wtfbbqhax

Response to Foss: Custom Functions 2005-11-04 15:24:10


At 11/4/05 03:12 PM, fwe wrote: function limit(v:Number, low:Number, high:Number):Number {
return v<low?low:v>high?high:v
}

Actually I didn't know you could use ? : syntax with else if statements =)


BBS Signature

Response to Foss: Custom Functions 2005-11-04 15:27:24


function toArray(str:String):Array {
var arr:Array = [];
for (a=0; a<str.length; a++) {
arr.push(str.charAt(a));
}
return arr;
}

Usage:

var myArray:Array = toArray("lol");
//my Array = l, o, l.
trace(toArray("lol")); //l, o, l


Sup, bitches :)

BBS Signature

Response to Foss: Custom Functions 2005-11-04 17:18:48


At 11/4/05 03:27 PM, -liam- wrote: var myArray:Array = toArray("lol");
//my Array = l, o, l.
trace(toArray("lol")); //l, o, l

lol @ u... good try...
even the call of the function is longer than the string method
var myArray:Array = new String("lol").split("");
trace(myArray);


BBS Signature

Response to Foss: Custom Functions 2005-11-04 17:25:52


At 11/4/05 05:18 PM, authorblues wrote: lol @ u... good try...

It's really hard to be inventive =( lol..

I kinda like this, because it works well =) I don't know how it could be used, but who knows..

function pickRandom(array:Array, restart:Boolean):String {
!(temp instanceof Array) ? temp=new Array() : 0;
restart ? temp=[] : 0;
randomValue = array[(random(array.length))];
for (a=0; a<temp.length; a++) {
if (randomValue == temp[a]) {
return pickRandom(array);
}
}
temp.push(randomValue);
return ((randomValue instanceof String) ? randomValue : randomValue.toString());
}

Usage:

pickRandom(["one", "two", 3, 4], true);
//pick a random char from the array, used twice the same value might be picked so only use this the first time you start picking from this array

pickRandom(["one", "two", 3, 4]);
//same as before, but used for the second, third, etc. time with the same array.


Sup, bitches :)

BBS Signature

Response to Foss: Custom Functions 2005-11-04 18:35:51


Wow, you had less than 8,000 posts this morning. How does it feel knowing that you post around 40 times a day?


wtfbbqhax

Response to Foss: Custom Functions 2005-11-04 18:37:23


At 11/4/05 03:12 PM, fwe wrote:
At 11/4/05 02:26 PM, Rantzien wrote: function limit(v:Number, low:Number, high:Number):Number {
if (v<low) {
return low;
} else if (v>high) {
return high;
} else {
return v;
}
}
function limit(v:Number, low:Number, high:Number):Number {
return v<low?low:v>high?high:v
}

)

function limit(v:Number, low:Number, high:Number):Number {
return Math.max(low,Math.min(high,v))
}
trace([limit(2,1,3),limit(0,1,3),limit(4,1
,3)])

function scramble(num:Number) {
var arr = new Array(num);
var added = 0;
while (added < num) {
ran = random(num);
unUsed = true;
for (var n in arr) {
unUsed *= ran != arr[n];
}
if (unUsed) {
arr[added] = ran;
added++;
}
}
return arr;
}

trace(scramble(5))

this next one was hard to do, it takes 3 coordinates and creates a circle that passes through all 3, i made this about a year ago, but i used a for loops to check the distances to get a mid point, now it's done with pure algebra, and it runs alot better. this function probly serves no purpose at all, but i love a challenge.

function distance(x1:Number, y1:Number, x2:Number, y2:Number):Number {
var xc:Number = x2 - x1;
var yc:Number = y2 - y1;
return Math.sqrt(xc * xc + yc * yc);
}
function circle(x1:Number, y1:Number, x2:Number, y2:Number, x3:Number, y3:Number) {
var xM1:Number = (x1 + x2) / 2;
var yM1:Number = (y1 + y2) / 2;
var xM2:Number = (x3 + x2) / 2;
var yM2:Number = (y3 + y2) / 2;
var m1:Number = (x2 - x1) / (y2 - y1) * -1;
var m2:Number = (x3 - x2) / (y3 - y2) * -1;
var b1:Number = (m1 * -xM1) + yM1;
var b2:Number = (m2 * -xM2) + yM2;
var xInt:Number = (b2 - b1) / (m1 - m2);
var yInt:Number = (m1 * xInt) + b1;
var radius:Number = distance(x1, y1, xInt, yInt);
_root.createEmptyMovieClip("line", 1);
_root.line.lineStyle(2, 0x00FF00);
_root.line.moveTo(x1, y1);
_root.line.lineTo(xInt, yInt);
_root.line.lineTo(x2, y2);
_root.line.moveTo(x3, y3);
_root.line.lineTo(xInt, yInt);
_root.line.lineStyle(2, 0xFF0000);
_root.line.moveTo(x1, y1);
_root.line.lineTo(x2, y2);
_root.line.lineTo(x3, y3);
_root.line.lineTo(x1, y1);
_root.line.lineStyle(2, 0x0000FF);
_root.line.moveTo(xInt + radius, yInt);
for (var angle = 1 / radius; angle < 360; angle += 100 / radius) {
var drawX:Number = Math.cos(angle / 180 * Math.PI) * radius;
var drawY:Number = Math.sin(angle / 180 * Math.PI) * radius;
_root.line.lineTo(xInt + drawX, yInt + drawY);
}
}
circle(75, 50, 100, 150, 25, 125);

I cleaned it up with them there fancy "var" indentifiers that y'all love so much. try it with 3 dragging buttons to really understand it.

Response to Foss: Custom Functions 2005-11-04 18:39:08


function scrambleMe(correct:String):String{
var broken:Array = correct.split(" ")
for (i=0; i<broken.length; i++){
broken[i] = broken[i].split("");
var scram:Array = [];
for (j=broken[i].length; j>0; j--){
var where:Number = int(Math.random()*broken[i].length);
scram.push(broken[i][where]);
broken[i].splice(where, 1);
}
broken[i] = scram
broken[i] = broken[i].join("");
}
return broken.join(" ");
}

USAGE:
trace(scrambleMe("-liam- is t3h s3cks"));
this will return a scrambled version of the text


BBS Signature

Response to Foss: Custom Functions 2005-11-04 20:30:17


At 11/4/05 06:39 PM, authorblues wrote: function scrambleMe(correct:String):String{

an alteration of the above:

function scrambleMe(correct:String, sepWord:Boolean):String{
var broken:Array = sepWord ? correct.split(" ") : new Array(correct);
for (i=0; i<broken.length; i++){
broken[i] = broken[i].split("");
var scram:Array = [];
for (j=broken[i].length; j>0; j--){
var where:Number = int(Math.random()*broken[i].length);
scram.push(broken[i][where]);
broken[i].splice(where, 1);
}
broken[i] = scram
broken[i] = broken[i].join("");
}
return broken.join(" ");
}

USAGE:

trace(scrambleMe("-liam- is t3h s3cks", false));
POSSIBLE OUTCOME: im--al si 3ht cs3ks

trace(scrambleMe("-liam- is t3h s3cks", trace));
POSSIBLE OUTCOME: -til3 ms c-is3akh s


BBS Signature

Response to Foss: Custom Functions 2005-11-04 20:46:05


At 11/4/05 01:46 PM, -liam- wrote:
The taylor series, hard hard maths =(

You guys. Freak me out.... :| I didnt even understand half of those math-terms.....

sticks to animating....

Response to Foss: Custom Functions 2005-11-04 20:58:26


At 11/4/05 08:46 PM, GuntherSwanh wrote:
At 11/4/05 01:46 PM, -liam- wrote:
The taylor series, hard hard maths =(
You guys. Freak me out.... :| I didnt even understand half of those math-terms.....

sticks to animating....

i dont even know what that weird zigzaggy symbol is (looks like a mixture of both a 3 and a backwards E....) im accually really good in mathematics, but i just havent gone anywhere near stuff like that (i think)

switches to animating

Response to Foss: Custom Functions 2005-11-04 21:07:07


At 11/4/05 08:58 PM, ImpotentBoy2 wrote: i dont even know what that weird zigzaggy symbol is (looks like a mixture of both a 3 and a backwards E....) im accually really good in mathematics, but i just havent gone anywhere near stuff like that (i think)

thats SIGMA: the summation notation. it means you add up all the values from 0 to infinity, or between whatever values are specified. its used in power series' and taylor notation. it can also be used for riemann sums, which are integral approximations.

very useful...


BBS Signature

Response to Foss: Custom Functions 2005-11-04 21:28:48


At 11/4/05 09:07 PM, authorblues wrote: thats SIGMA: the summation notation. it means you add up all the values from 0 to infinity, or between whatever values are specified. its used in power series' and taylor notation. it can also be used for riemann sums, which are integral approximations.

very useful...

so like an addition factoral, ok. there's still alot i need to learn on that page b4 i understand it

Response to Foss: Custom Functions 2005-11-04 21:39:14


At 11/4/05 09:28 PM, ImpotentBoy2 wrote: so like an addition factoral, ok. there's still alot i need to learn on that page b4 i understand it

i dont fully understand taylor approximations, but it is a way to estimate complex functions. you can use it to turn trigonometric functions into rectangular functions (non-trigonometric)... thats where its power is...


BBS Signature

Response to Foss: Custom Functions 2005-11-04 21:50:09


so how did you learn this stuff, advanced math classes? i shouldve taken advanced maths, i could've, i would've, but i took remedial..... funny how that worked out. but seriously dude your my age and you seem to know alot more than me

Response to Foss: Custom Functions 2005-11-04 21:53:01


At 11/4/05 09:50 PM, ImpotentBoy2 wrote: so how did you learn this stuff, advanced math classes? i shouldve taken advanced maths, i could've, i would've, but i took remedial..... funny how that worked out. but seriously dude your my age and you seem to know alot more than me

im in college. im a math major. im in the honors college. im taking spacial calculus with non-euclidian application. all those together, you learn some cool things along the way. we did taylor approximations so that we could learn to do all of our function evaluation without a calculator...


BBS Signature

Response to Foss: Custom Functions 2005-11-05 02:52:50


At 11/4/05 09:50 PM, ImpotentBoy2 wrote: so how did you learn this stuff, advanced math classes? i shouldve taken advanced maths, i could've, i would've, but i took remedial..... funny how that worked out. but seriously dude your my age and you seem to know alot more than me

internet - im 14 so obvisously i cant take any advanced maths class on complex numbers/ quaternions etc:

so far in my math class: (called emath for some reason)

there are 24 trig functions (sin cos tan, inverses, hyperbolics + inverse hyperbolics) + the sec,csc,cot versions

i have Gamma and Factorial (with an extra Boolean parameter for non integers - if true then the return is more accurate, but with the expense of a 810*(1/pow(x,6)) )

i have Binomial Coeffecient function ( nCr -> emath.C(n,r) ) which using the factorial can take fractional parameters (and once i get to complex math functions, will take complex numbers including negatives although i dont really no if it would work to find the coeffecients ive never tried)

i have roundTo, floorTo, ceilTo which just round the numbers to a certain dp (including negative dp's for things like the nearest 10)

i have alot of constants: Phi (1.618...) phi (1/Phi) G (gravitational constant) and g (1/gravitational constant), the sqrt's from 2 to 25, PI2 (PI*2), Deg and Rad (just the conversions for radians to degrees vice versa) inf and ninf (emath.inf = Number.POSITIVE_INFINITY)

Min and Max (which take 'n' number of parameters which can also be arrays of numbers)
emath.Min(1,4,[2,4,1],2,[2,3]) is acceptable

function for Fib(n) (fibonacci numbers)

isPrime(x);
isOdd(x);
isEven(x);
isInt(x) <- using round();

xlog(n,x) <- logarithm base x of n

and it extends Math so it still has all of their functions and constants

Response to Foss: Custom Functions 2005-11-05 08:04:40


I've put all the functions we have so far into a Class (named Custom, for want of a better name).. you can find it here (thanks to IWantSomeCookies for filehosting). You must save it as a .as file though =P

Use it simply like so:

custom = new Custom();
custom.functionName(property1, property2, propertyN);
//for example
trace(custom.scramble(10));
//to show the credits:
trace(custom.showCredits());

=)


Sup, bitches :)

BBS Signature

Response to Foss: Custom Functions 2005-11-05 08:19:54


At 11/5/05 08:04 AM, -liam- wrote: custom = new Custom();

If you make them static functions you wont need that =)


BBS Signature

Response to Foss: Custom Functions 2005-11-05 09:01:24


At 11/5/05 08:19 AM, Rantzien wrote: If you make them static functions you wont need that =)

how do you manage that?


BBS Signature

Response to Foss: Custom Functions 2005-11-05 09:07:57


At 11/5/05 09:01 AM, authorblues wrote: how do you manage that?

nameofclass.functioninclass(functionparame
ters) basically =P so it's like:

trace(Custom.scrabble(10));

Which is how (for example) the Math class is done..

Math.round(number):Number - Math is the name of the class (and is been accessed directly) and round is a static function inside that class.


Sup, bitches :)

BBS Signature