00:00
00:00
Newgrounds Background Image Theme

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

Response to Foss: Custom Functions 2005-11-05 09:10:44


Replace the word public with the word static =)

static function exactCurveTo(mc:MovieClip, startX:Number, startY:Number, curveX:Number, curveY:Number, endX:Number, endY:Number):Void {
with (mc) {
X = (startX+endX)/2;
Y = (startY+endY)/2;
moveTo(startX, startY);
curveTo(X+(curveX-X)*2, Y+(curveY-Y)*2, endX, endY);
}
}

Usage:
exactCurveTo(this, 10, 10, 400, 100, 540, 390);
Try it out with draggable points.


BBS Signature

Response to Foss: Custom Functions 2005-11-05 10:01:38


function approxTo(num:Number, apTo:Number):Number{
if (apTo!=Math.round(apTo)){ return num };
return Math.round(num*Math.pow(10, apTo))/Math.pow(10, apTo);
}

USAGE:
approxTo(num, apTo);, where num is the number to be approximated, and apTo is how many decimal places the approximation should incorporate. change math.round() to math.floor() to truncate the decimals in a similar manner...

trace(approxTo(Math.PI, 1));
// returns 3.1
trace(approxTo(Math.PI, 3));
// returns 3.142
trace(approxTo(Math.PI, 5));
// returns 3.14159
trace(approxTo(Math.PI, 7));
// returns 3.1415927


BBS Signature

Response to Foss: Custom Functions 2005-11-05 10:08:33


At 11/5/05 10:01 AM, authorblues wrote: approxTo(num, apTo);

add that to your custom emath class, dELta...
thats something i had always withed Math had


BBS Signature

Response to Foss: Custom Functions 2005-11-05 11:07:34


I was just making something (link), and I tried my getRandom(low, high, round) function.. extremly useful.. I used it to make the background image shake:

onClipEvent (enterFrame) {
_x = Custom.getRandom(265, 285);
_y = Custom.getRandom(190, 210);
}

Yay.


Sup, bitches :)

BBS Signature

Response to Foss: Custom Functions 2005-11-05 13:28:41


Now I'm trying to convert most of these (the ones possible, at least) to C++.. heh =P

For example:

#import <iostream>
#import <math.h>
using namespace std;
double roundTo(double orig, double to){
return to*round(orig/to);
}
int main(){
cout<<roundTo(9.6, 0.5);
//outputs 9.5 =P
cin.ignore();
cin.get();
return 0;
}


Sup, bitches :)

BBS Signature

Response to Foss: Custom Functions 2005-11-05 13:41:03


At 11/5/05 01:28 PM, -liam- wrote: Now I'm trying to convert most of these (the ones possible, at least) to C++.. heh =P

dumb critiszm time

int main(){

why not have a args[] and take the number you're rounding from that? if not, why not have int main(void)

cout<<roundTo(9.6, 0.5);
//outputs 9.5 =P
cin.ignore();
cin.get();

what was the point of these two lines ?

return 0;
}

Response to Foss: Custom Functions 2005-11-05 13:49:41


At 11/5/05 10:08 AM, authorblues wrote:
At 11/5/05 10:01 AM, authorblues wrote: approxTo(num, apTo);
add that to your custom emath class, dELta...
thats something i had always withed Math had

i dont need that, i have roundTo floorTo and ceilTo which are just the round floor ciel fuctions but to a certain dp (including negative dp's for things like to the nearest 10)

Response to Foss: Custom Functions 2005-11-05 20:23:44


At 11/5/05 01:49 PM, dELta_Luca wrote: i dont need that, i have roundTo floorTo and ceilTo which are just the round floor ciel fuctions but to a certain dp (including negative dp's for things like to the nearest 10)

good call. just throwing out suggestions.
and what are the sinh, cosh, and tanh for, in practical terms
what would i use them for?


BBS Signature

Response to Foss: Custom Functions 2005-11-06 06:05:42


At 11/5/05 01:41 PM, Inglor wrote: why not have a args[] and take the number you're rounding from that? if not, why not have int main(void)

Because, I've only been learning C++ for about 6 days?

what was the point of these two lines?

I actually want to see the result.. though it would work with just cin.get(); bleh.


Sup, bitches :)

BBS Signature

Response to Foss: Custom Functions 2005-11-06 09:16:10


function chooseBetween(a:Number, b:Number):Number {
return random(2) == 1 ? a : b;
}

Usage:

for (c=0; c<50; c++) {
trace(chooseBetween(-1, 1));
}//traces fifty time with either -1 or 1

Something I wrote for someone on MSN.


Sup, bitches :)

BBS Signature

Response to Foss: Custom Functions 2005-11-06 09:35:06


hehehe time to throw the guantlet into the ring for all the smarty math people. I know its pretty simple to differentiate most functions with pen and paper but what about a flash function to work out the equation of the tangent at a given x value? hmm up to the challenge? Im gonna give it a shot tomorrow.

Response to Foss: Custom Functions 2005-11-06 12:47:57


At 11/6/05 09:35 AM, BigBazz wrote: hehehe time to throw the guantlet into the ring for all the smarty math people. I know its pretty simple to differentiate most functions with pen and paper but what about a flash function to work out the equation of the tangent at a given x value? hmm up to the challenge? Im gonna give it a shot tomorrow.

what you are talking about is equation parsing, and then differentiating terms of a parsed equation, and then parsing that other equation, then graphing the system of a linear tangent. yeah, give it a shot tomorrow. youll be working on it for quite a while. i have been working on it as well, and i havent gotten past parenthetical evaluation...


BBS Signature

Response to Foss: Custom Functions 2005-11-06 12:58:20


At 11/6/05 09:16 AM, -liam- wrote: function chooseBetween(a:Number, b:Number):Number {
return random(2) == 1 ? a : b;
}

function chooseBetween2(a:Number, b:Number):Number {
return [a,b][random(2)]
}

:D


wtfbbqhax

Response to Foss: Custom Functions 2005-11-06 13:14:15


At 11/6/05 12:58 PM, fwe wrote:
D

Lolwtf, theres no difference really. You just don't like typing much, do you?

<3


Sup, bitches :)

BBS Signature

Response to Foss: Custom Functions 2005-11-06 13:19:08


At 11/6/05 12:47 PM, authorblues wrote: parenthetical evaluation...

this kinda inspired me to finish my parsing engine, so all i have left is to include the trig functions, and then ill compile it into an actuall class, and ill share it.

maybe itll make it into dELta's emath class (who knows)...


BBS Signature

Response to Foss: Custom Functions 2005-11-06 13:22:57


At 11/6/05 01:19 PM, authorblues wrote: maybe itll make it into dELta's emath class (who knows)...

He's finished that (not to say he can't add to it, because he can), and the graphing example I saw was just plain omgish..


Sup, bitches :)

BBS Signature

Response to Foss: Custom Functions 2005-11-06 13:28:17


At 11/6/05 01:19 PM, authorblues wrote:
At 11/6/05 12:47 PM, authorblues wrote: parenthetical evaluation...
this kinda inspired me to finish my parsing engine, so all i have left is to include the trig functions, and then ill compile it into an actuall class, and ill share it.

maybe itll make it into dELta's emath class (who knows)...

i think what i might do now after seeing this whole thing, i might incoperate the algorithm i made for converting a mathematical equation into an ordered set of operations for computation, ie what i used in my scientific calculator where you could type in 3(4+5logx10) and it would give the answer etc

Response to Foss: Custom Functions 2005-11-08 23:27:35


function drawPoly(sides:Number, radius:Number, xcenter:Number, ycenter:Number, rotat:Number, newName:String):Void {
_root.createEmptyMovieClip(newName, 1);
with (_root[newName]) {
lineStyle(2, 0x000000, 100);
beginFill(0xFF0000, 50);
moveTo(radius*Math.cos((2*Math.PI)-Math.PI
/2+rotat)+xcenter, radius*Math.sin((2*Math.PI)-Math.PI/2+rota
t)+ycenter);
for (var i:Number = 0; i<sides; i++) {
lineTo(radius*Math.cos(((2*(i+1)*Math.PI)/
sides)-Math.PI/2+rotat)+xcenter, radius*Math.sin(((2*(i+1)*Math.PI)/sides)-
Math.PI/2+rotat)+ycenter);
}
endFill();
}
}

USAGE:
drawPoly(5, 100, Stage.width/2, Stage.height/2, 0, "penta");
// this will draw a polygon with the following characteristics
// 5 sides, "radius" of 100 px, located at center of stage, no rotation, named "penta"


BBS Signature

Response to Foss: Custom Functions 2005-11-08 23:30:19


At 11/8/05 11:27 PM, authorblues wrote: drawPoly(5, 100, Stage.width/2, Stage.height/2, 0, "penta");

you could also use this to simulate a perfect circle, which is difficult with curveTo, in my experience. this will visually produce a perfect circle:

drawPoly(2500, 100, Stage.width/2, Stage.height/2, 0, "circle");


BBS Signature

Response to Foss: Custom Functions 2005-11-10 00:10:43


function rgbToHex(red:Number, green:Number, blue:Number):String{
var hexarr:Array = new Array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F")
return "0x"+hexarr[Math.floor(red/16)]+hexarr[red
%16]+hexarr[Math.floor(green/16)]+hexarr[g
reen%16]+hexarr[Math.floor(blue/16)]+hexar
r[blue%16]
}

Usage
trace(rgbToHex(255, 255, 255));
// Output = "0xFFFFFF"

trace(rgbToHex(0, 0, 0));
// Output = "0x000000"

trace(rgbToHex(135, 221, 31));
// Output = "0x87DD1F"


BBS Signature

Response to Foss: Custom Functions 2005-11-10 00:34:02


function rgbToHex(red, green, blue) {
hex = red << 16 | green << 8 | blue;
return hex.toString(16);
}

Response to Foss: Custom Functions 2005-11-10 00:38:18


At 11/10/05 12:34 AM, ImpotentBoy2 wrote: function rgbToHex(red, green, blue) {
hex = red << 16 | green << 8 | blue;
return hex.toString(16);
}

trace(rgbToHex(0, 255, 0));
// Output = "ff00"

hmm... something to ponder


BBS Signature

Response to Foss: Custom Functions 2005-11-10 00:46:09


At 11/10/05 12:38 AM, authorblues wrote:
At 11/10/05 12:34 AM, ImpotentBoy2 wrote: function rgbToHex(red, green, blue) {
hex = red << 16 | green << 8 | blue;
return hex.toString(16);
}
trace(rgbToHex(0, 255, 0));
// Output = "ff00"

hmm... something to ponder

gah, you got me, still though the Hexdec is wrong but the dec version is always right. but i guess if you need the FF00FO stuf yours would be a better choice

Response to Foss: Custom Functions 2005-11-10 00:49:01


At 11/10/05 12:46 AM, ImpotentBoy2 wrote: gah, you got me, still though the Hexdec is wrong but the dec version is always right. but i guess if you need the FF00FO stuf yours would be a better choice

i know mine looks ugly, but when you remove ngs damn post formatting, it goes into 2 or 3 lines (depending if you include the function declaration. its very short, and works perfectly. i came up with it in 2 min (me == proud)...


BBS Signature

Response to Foss: Custom Functions 2005-11-10 00:56:58


the only thing is, it only gives you the 0xFF00FF, say you wanted to turn something that color, you would have to find a way to get that number into decimal form. and r<<16|g<<8|b gets the accuall code that flash can read. although accually that might work if you put it into a setRGB() but im not too sure. and by the way. i have no clue what << and | do. i mean i understand them a little, but i have absolutely no binary skills.

Response to Foss: Custom Functions 2005-11-10 01:10:26


At 11/10/05 12:56 AM, ImpotentBoy2 wrote: r<<16|g<<8|b
and by the way. i have no clue what << and | do. i mean i understand them a little, but i have absolutely no binary skills.

well, << converts to binary and then shifts all decimals to the left a set number of spaces. 7 << 2 will yield 28 because 7 base 10 = 111 base 2. when you shift 111 left two spaces, you get 11100, which is 28 base 10.

| is a bitwise or, which, if i understand correctly, it checks each slot, one by one, and if at least one of the slots has a 1 in it, it returns a 1 in the same slot.

used in conjunction, the first 8 slots of the binary value would be the red values, the second 8 would be the green values, and the last would be the blue values. to combine them into one large binary representation, you would have to do a bitwise or to check each slot. then, it converts the binary string to a hex representation with the num.toString(16), which is just the same as the number, but in base 16 (hexidecimal).


BBS Signature

Response to Foss: Custom Functions 2005-11-10 01:34:29


i know what they do i read the definitions i just dont get why that would get the decimal version of the color. i guess because 255 in binary is 11111111.

Response to Foss: Custom Functions 2005-11-13 21:42:32


since, as many know, random() isnt truly random, and is based off of time, i decided to use what i could think of to make a pseudo-random script. heres what i came up with, and it works perfectly:

function prandom():Number{
var whn:Date = new Date();
return (getTimer()%whn.getMilliseconds())/whn.get
Milliseconds();
}

function prandom2(num:Number):Number{
return Math.floor(prandom()*Math.floor(num));
}

USAGE:
prandom() represents the Math.random(); function
prandom2() represents the random(num); function


BBS Signature

Response to Foss: Custom Functions 2005-11-13 21:46:38


thats great but i dont get why we should use that over the inital randoms. i mean yours is based on time too.

Response to Foss: Custom Functions 2005-11-13 21:52:41


At 11/13/05 09:46 PM, ImpotentBoy2 wrote: thats great but i dont get why we should use that over the inital randoms. i mean yours is based on time too.

its actually an appendix to the attempt dELta made to rewrite the entire Math class. Math.random() would arguably be the most difficult one to recreate (aside from decimal roots and powers), so this is for that reason...


BBS Signature