00:00
00:00
Newgrounds Background Image Theme

nuggetior 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,257 Views | 106 Replies
New Topic Respond to this Topic

Foss: Custom Functions 2005-11-03 14:53:33


This is just a thread where we can post some custom functions that do general stuff for us, nothing stupid but actual useful stuff. Say if we need anything specific, thats all :)

---

function factorial(num:Number):Number {
temp = num;
for (a=1; a<num; a++) {
temp *= a;
}
return temp;
}

Usage:

trace(factorial(4)); //24;
trace(factorial(100)); //9.33262154439442e+157
trace(factorial(6)); //720

---

function drawCircle(X:Number, Y:Number, Radius:Number):Void {
theta = (45/180)*Math.PI;
ctrlRadius = Radius/Math.cos(theta/2);
_root.lineStyle(2, 0, 100);
_root.moveTo(X+Radius, Y);
angle = 0;
for (var i = 0; i<8; i++) {
angle += theta;
var angleMid = angle-(theta/2);
var cx = X+Math.cos(angleMid)*(ctrlRadius);
var cy = Y+Math.sin(angleMid)*(ctrlRadius);
var px = X+Math.cos(angle)*Radius;
var py = Y+Math.sin(angle)*Radius;
_root.curveTo(cx, cy, px, py);
}
}

Usage:

drawCircle(275, 200, 50); //draw a circle AROUND 275, 200 with a diameter of 100

---

function allStats() {
var i:Number = 0;
var sCount:Number = 0;
for (i in _root) {
if (_root[i]._name !== undefined) {
sCount++;
trace("Item "+sCount+": "+_root[i]._name);
trace("Depth: "+_root[i].getDepth());
trace("H. Size: "+_root[i]._xscale);
trace("V. Size: "+_root[i]._yscale);
trace("Height: "+_root[i]._height);
trace("Width: "+_root[i]._width);
trace("_x: "+_root[i]._x);
trace("_y: "+_root[i]._y);
trace(" -----------");
}
}
}

Usage:

allStats(); //Get info on all the objects in your flash

---

function toHex(col:Number):String {
return col.toString(16);
}

Usage:

trace(toHex(16711680)); //ff0000

---

Meh =)


Sup, bitches :)

BBS Signature

Response to Foss: Custom Functions 2005-11-03 15:00:04


At 11/3/05 02:53 PM, -liam- wrote: This is just a thread where we can post some custom functions that do general stuff for us, nothing stupid but actual useful stuff.

function roundTo(num:Number, goTo:Number):Number{
return goTo*Math.round(num/goTo);
}

trace(roundTo(15, 10)); // Output: 20
trace(roundTo(14, 10)); // Output: 10
trace(roundTo(16, 10)); // Output: 20
trace(roundTo(31, 100)); // Output: 0
trace(roundTo(67, 100)); // Output: 100


BBS Signature

Response to Foss: Custom Functions 2005-11-03 15:09:07


finds the greatest common devider of two numbers

function gcd(a:Number,b:Number){
b==0?return a;return gcd(b,a%b);
}

try and figure THAT out liam :P

trace(gcd(10,5));//traces 5;
trace(gcd(15,5));//traces 5;
trace(gcd(16,12));//traces 4;
trace(gcd(12,11));//traces 11;
trace(gcd(36,30));//traces 6;

Response to Foss: Custom Functions 2005-11-03 15:19:25


At 11/3/05 03:09 PM, Inglor wrote: try and figure THAT out liam :P

First off (for some unknown (to me) reason) you can't use return in a ?: statement, secondly the function doesn't ever return anything except 0, the only possible outcomes are 0 and to recurse (word?) the function.

Unless you or I made a mistake.


Sup, bitches :)

BBS Signature

Response to Foss: Custom Functions 2005-11-03 15:27:03


At 11/3/05 03:19 PM, -liam- wrote:

secondly the function doesn't ever return anything except 0, the only possible outcomes are 0 and to recurse (word?) the function.

works perfectly in this syntax:

function gcd(a:Number,b:Number){
if (b==0){
return a;
} else {
return gcd(b,a%b);
}
}

trace(gcd(10,5));//traces 5;
trace(gcd(15,5));//traces 5;
trace(gcd(16,12));//traces 4;
trace(gcd(12,11));//traces 11;
trace(gcd(36,30));//traces 6;


BBS Signature

Response to Foss: Custom Functions 2005-11-03 15:30:31


At 11/3/05 02:53 PM, -liam- wrote: This is just a thread where we can post some custom functions that do general stuff for us, nothing stupid but actual useful stuff.

heres some basic functions i just wrote:

function distance(ax:Number, ay:Number, bx:Number, by:Number):Number{
return Math.sqrt(Math.pow((bx-ax), 2)+Math.pow((by-ay), 2));
}

function slope(ax:Number, ay:Number, bx:Number, by:Number):Number{
return ((by-ay)/(bx-ax));
}

function angRotat(ax:Number, ay:Number, bx:Number, by:Number, rad:Boolean):Number{
if (rad){
return Math.atan((by-ay)/(bx-ax));
}
return Math.atan((by-ay)/(bx-ax))*(180/Math.PI);
}

trace(distance(0, 0, 3, 4)); // Output: 5
trace(slope(0, 0, 3, 4)); // Output: 1.33333333333333
trace(angRotat(0, 0, 3, 4, false)); // Output: 53.130102354156
trace(angRotat(0, 0, 3, 4, true)); // Output: 0.927295218001612

usage:
distance - put in two points, finds distance
slope - put in two points on a line, finds slope of line
angRotat - finds the angle of rotation from point a to point b
the boolean value with angRotat is for radians or degrees
(true for radians, false for degrees)


BBS Signature

Response to Foss: Custom Functions 2005-11-03 15:31:25


At 11/3/05 03:27 PM, authorblues wrote: stuff

Heh, I thought return a; was return 0;, silly me =P But Inglor, you really should know better than to use return in an ?: statement.. tut tut <3


Sup, bitches :)

BBS Signature

Response to Foss: Custom Functions 2005-11-03 15:32:53


At 11/3/05 03:09 PM, Inglor wrote: trace(gcd(12,11));//traces 11;

if done correctly, that should output 1, but i cant imagine how you would edit that to do so...


BBS Signature

Response to Foss: Custom Functions 2005-11-03 15:47:30


At 11/3/05 03:32 PM, authorblues wrote:
At 11/3/05 03:09 PM, Inglor wrote: trace(gcd(12,11));//traces 11;

haha, i just tested it. it DOES trace 1.
i guess that was just a typo. neat function.

recursion is for trendy kids...

BBS Signature

Response to Foss: Custom Functions 2005-11-03 15:57:52


sinh function, Gamma function (therefore Factorial function) (aproximations)

function sinh(x:Number):Number
{
return (Math.exp(x)-Math.exp(-x))/2
}

function Gamma(x:Number):Number
{
return Math.sqrt((2*Math.PI)/x)*Math.pow( (x/Math.E)*Math.sqrt( x*sinh(1/x) + 1/(810*Math.pow(x,6)) ) ,x);
}

function Fact(x:Number):Number
{
return Gamma(x+1);
}

trace(Fact(0.5));
//outputs 0.886241485039472, actual value = 0.886226925

trace(Fact(2.5));
//outputs 3.3233511882515, actual value = 3.32335097

trace(Fact(3));
//outputs 6.000000158785, actual value = 6

trace(Fact(3.5));
//outputs 11.631728534231, actual value = 11.6317284

trace(Fact(4));
//outputs 24.0000001378404, actual value = 24

(actual values obtained from google calculator)

Response to Foss: Custom Functions 2005-11-03 16:00:36


At 11/3/05 02:53 PM, -liam- wrote: This is just a thread where we can post some custom functions that do general stuff for us, nothing stupid but actual useful stuff.

this is my last one for now:

function isPrime(num:Number):Boolean{
var i:Number;
if (num != Math.round(num)){ return true };
for (i=2; i<=Math.sqrt(num)+1; i++){
if (num/i == Math.round(num/i)){
return false;
}
}
return true;
}

usage:
provides boolean value for whether or not a number is prime.
known issue: incorrect output for 2.

for (j=3; j<=16; j++){
trace(j + ": " + isPrime(j));
}


BBS Signature

Response to Foss: Custom Functions 2005-11-03 16:05:32


At 11/3/05 04:00 PM, authorblues wrote: this is my last one for now:

i didnt acctually understand the meaning of that function! :S

Response to Foss: Custom Functions 2005-11-03 16:07:56


At 11/3/05 04:05 PM, Creeepy wrote:
At 11/3/05 04:00 PM, authorblues wrote: this is my last one for now:
i didnt acctually understand the meaning of that function! :S

it determines whether a given number is prime

if you dont know what a prime number is (hahaha) a prime number is a number that is devisable by only itself and 1 (1 is not a prime)

primes are:
2,3,5,7,11,13,17,19,23.....

Response to Foss: Custom Functions 2005-11-03 16:10:02


At 11/3/05 04:07 PM, dELta_Luca wrote:
At 11/3/05 04:05 PM, Creeepy wrote:
At 11/3/05 04:00 PM, authorblues wrote: this is my last one for now:
i didnt acctually understand the meaning of that function! :S
it determines whether a given number is prime

if you dont know what a prime number is (hahaha)
dont lauhg! =P

a prime number is a number that is devisable by only itself and 1 (1 is not a prime)


primes are:
2,3,5,7,11,13,17,19,23...

ahh ok i see!
im not english so i dont know those words on english! =P

Response to Foss: Custom Functions 2005-11-03 16:39:47


Simple function, count how many times something appears in an array and where it appears:

function countArray(array:Array, item:String):Array {
count=0, slots=[];
for (a=0; a<array.length; a++) {
array[a] == item ? (count++, slots.push(a)) : 0;
}
return ["Amount:"+count+" ", "At:"+slots];
}

Usage:

var ar:Array = new Array(1, 2, 1, 2, 1);
trace(countArray(ar, "1"));

Traces: Amount:3 ,At:0,2,4


Sup, bitches :)

BBS Signature

Response to Foss: Custom Functions 2005-11-03 16:47:19


I like this, so i don't have to type getNextHighestDepth() all the time

Object.prototype.d = function() { return this.getNextHighestDepth() }

Usage:

highDepth= bleh.d()
bleh.createEmptyMovieClip("poo", highDepth() or just use bleh.d())

If you're creating a MC on the root, don't use anything

attachMovie("Menu", "menu"+d(),d())


wtfbbqhax

Response to Foss: Custom Functions 2005-11-03 16:50:20


At 11/3/05 04:39 PM, -liam- wrote: Simple function, count how many times something appears in an array and where it appears:

function countArray(array:Array, item:String):Array {
count=0, slots=[];
for (a=0; a<array.length; a++) {
array[a] == item ? (count++, slots.push(a)) : 0;
}
return ["Amount:"+count+" ", "At:"+slots];
}

Usage:

var ar:Array = new Array(1, 2, 1, 2, 1);
trace(countArray(ar, "1"));

Traces: Amount:3 ,At:0,2,4

or you could use this:
function countArray(array:Array, item:String):Number {
return array.join(", ").split(item).length-1;
}

trace(countArray(new Array(1,2,1,2,1),"2")) outputs 2

although it doesnt tell you which items contain the expression, it is faster for finding how many times it appears in the array (no loop)

Response to Foss: Custom Functions 2005-11-03 22:24:26


function logBase(base:Number, ofWhat:Number):Number{
return Math.log(ofWhat)/Math.log(base);
}

trace(logBase(10, 1000)); // Output: 3
trace(logBase(Math.E, 4)); // Output: Ln 4
trace(logBase(2, 32)); // Output: 5

USAGE:
the math object lacks the ability to do a Log base A of B, so this provides a workaround
in the form of logBase(base, ofWhat). all output is accurate to the capability of the math
object...


BBS Signature

Response to Foss: Custom Functions 2005-11-03 22:38:31


At 11/3/05 03:30 PM, authorblues wrote: function slope(ax:Number, ay:Number, bx:Number, by:Number):Number{
return ((by-ay)/(bx-ax));
}

ah yes, good ol slope. rise/run. (y2-y1)/(x2-x1)

we are just now "learning" this stuff in my algebra class. T_T


snyggys

Response to Foss: Custom Functions 2005-11-03 23:04:02


i dont know how many of these i have in me, but this is actually a lot of fun...

function fadeAway(tgtMC:MovieClip, spd:Number):Void {
var fadeInterval:Number = setInterval(function () {
tgtMC._alpha>0 ? tgtMC._alpha -= 1 : delete fadeInterval;
}, 1000/spd);
}

fadeAway(this, 10); // fades parent movie clip slowly
fadeAway(this, 100); // fades parent movie clip to transparent in one second
fadeAway(_root, 10); // fades entire timeline slowly

USAGE:
include in a game script to create a simple fade away
include with a _root target to fade out game on game over
include randomly with certain API functions (such as snow) to create a disappearing effect


BBS Signature

Response to Foss: Custom Functions 2005-11-04 11:39:55


anyone interested in a full complex number class?

Response to Foss: Custom Functions 2005-11-04 11:55:35


At 11/4/05 11:39 AM, dELta_Luca wrote: anyone interested in a full complex number class?

Go ahead =)


Sup, bitches :)

BBS Signature

Response to Foss: Custom Functions 2005-11-04 12:01:26


or possibly ill just write a full class to replace the current math class, more constants, the full range of trig functions (not just sin,cos,tan + inverses) and also logarithms, factorials, binomial coeeffecients etc + complex math functions + vector math functions lol

Response to Foss: Custom Functions 2005-11-04 12:03:32


At 11/3/05 03:47 PM, authorblues wrote: haha, i just tested it. it DOES trace 1.
i guess that was just a typo. neat function.

euclid. genious guy.

Response to Foss: Custom Functions 2005-11-04 12:06:46


At 11/4/05 12:01 PM, dELta_Luca wrote: or possibly ill just write a full class to replace the current math class, more constants, the full range of trig functions (not just sin,cos,tan + inverses) and also logarithms, factorials, binomial coeeffecients etc + complex math functions + vector math functions lol

extreme

Response to Foss: Custom Functions 2005-11-04 12:10:05


Guess I could have a crack at line/curve, possibly curve/curve collision. Don't know if I can be arsed though, don't like the sound of parsing quadratic equations.

Response to Foss: Custom Functions 2005-11-04 13:01:16


function getRandom(low:Number, high:Number, round:Boolean):Number {
if (round) {
return Math.round((Math.random()*(high-low))+low)
;
} else if (!round) {
return (Math.random()*(high-low))+low;
}
}

Usage:

trace(getRandom(10, 100, true)); //Get a round number from 10 to 100.
trace(getRandom(-50, 50, true)); //Get a number from -50 to 50.

---

function getDownload(myUrl:String, defaultName:String):Void {
import flash.net.FileReference;
var dload:FileReference = new FileReference();
dload.download(myUrl, defaultName);
}

Usage:

getDownload("http://www.anim8or.com/downlo
ad/animv09c.zip", "anim8or.zip");
//Download the anim8or .zip and give it a name of anim8or.zip (file extension must be included)


Sup, bitches :)

BBS Signature

Response to Foss: Custom Functions 2005-11-04 13:06:34


almost 8000 liam

<3

Response to Foss: Custom Functions 2005-11-04 13:08:44


At 11/4/05 01:01 PM, -liam- wrote: function getDownload(myUrl:String, defaultName:String):Void {
import flash.net.FileReference;
var dload:FileReference = new FileReference();
dload.download(myUrl, defaultName);
}

that one right there is very nice.
would i be correct in saying that it requires flash 8?

and dELta... dont the sin, cos, and tan functions require taylor approximations? how can you come up with derivations with flash. no, wait, you can hand-write the derivatives. if your new Math class could provide a few neat functions like basic derivation or equation parsing, you would be a god among men...

not that you dont already rock

BBS Signature

Response to Foss: Custom Functions 2005-11-04 13:14:43


At 11/4/05 01:08 PM, authorblues wrote: that one right there is very nice.
would i be correct in saying that it requires flash 8?

Thanks, mainly just a time saving method.. but then again all functions are :) Yeah you do need Flash 8 for the FileReference class.


Sup, bitches :)

BBS Signature