By Zevan | November 29, 2009
CLICK HERE TO COPY
Actionscript:
// print some floats as fractions
printFraction(0.5);
printFraction(0.75);
printFraction(0.48);
// try something a little more complex
var float:Number = 0.98765432;
trace("\na more complex example:");
printFraction(float);
var frac:Array = asFraction(float);
trace("double check it:");
trace(frac[0] + "/" + frac[1] +" = " + frac[0] / frac[1]);
/* outputs:
0.5 = 1/2
0.75 = 3/4
0.48 = 12/25
a more complex example:
0.98765432 = 12345679/12500000
double check it:
12345679/12500000 = 0.98765432
*/
function printFraction(n:Number):void{
[...]
Posted in Math, misc | Tagged actionscript, as3, flash |
By Zevan | November 28, 2009
CLICK HERE TO COPY
Actionscript:
trace(gcf(75,145));
// outputs 5
function gcf(a:int, b:int):int{
var remainder:int;
var factor:Number = 0;
while (1){
if (b> a){
var swap:int = a;
a = b;
b = swap;
}
remainder [...]
Posted in Math | Tagged actionscript, as3, flash |
By Zevan | November 27, 2009
CLICK HERE TO COPY
Actionscript:
egyptianMultiply(12, 99);
// trace(12 * 99) // test to make sure it works
/* outputs:
/64 768
/32 384
16 192
8 96
4 48
/2 24
/1 12
---
1188
*/
function egyptianMultiply(valueA:Number, valueB:Number):void {
var left:Array = [];
var right:Array = []
// swap if valueB is smaller than value A
if (valueB <valueA){
[...]
By Zevan | November 25, 2009
CLICK HERE TO COPY
Actionscript:
var container:Sprite = new Sprite();
container.x = stage.stageWidth / 2;
container.y = stage.stageHeight / 2;
addChild(container);
var redBox:Sprite = new Sprite();
redBox.graphics.beginFill(0xFF0000);
redBox.graphics.drawRect(-50,-250,100,500);
redBox.rotationZ = 10;
container.addChild(redBox);
var logos:Array = []
var elements:Array = [];
elements.push({element:redBox, z:0});
// add the logos
for (var i:int = 0; i<6; i++){
var logoContainer:MovieClip = new MovieClip();
var logoText:TextField = new TextField();
logoText.defaultTextFormat = new TextFormat("_sans", [...]