Category Archives: misc

ActionScript Eval (well, not really)

Actionscript:
  1. var txt:TextField = TextField(addChild(new TextField()));
  2. txt.autoSize = TextFieldAutoSize.LEFT;
  3.  
  4. var myVar:Number = 100;
  5. // works as an expression parser
  6. txt.text = js("return (" + myVar + " * 2 + 10) / 2");
  7. txt.appendText("\n\n");
  8.  
  9. // parses javascript
  10. txt.appendText(js("var a = []; for (var i = 0; i<10; i++){ a.push(i) } return a;"));
  11.  
  12. function js( data:String ):*{
  13.    var res:*= ExternalInterface.call("function(){" + data + "}");
  14.    return (res == null) ? "null" : res;
  15. }

This one made my day - CAN'T believe I never thought of it before... haven't done any speed tests yet... but it shows how to use ExternalInterface.call to parse math expressions and javascript code. This will only work when the swf is shown in an html page of course... so if your in flash cmd+f12...

I got the idea from this code snippet... which actually has an error in it... the decode() function should not return void... I found that snippet thanks to this tweet by makc3d.

Also posted in Math, external data, functions, string manipulation, strings | Tagged , , | Leave a comment

E8 Inspired Forms

This doesn't have much to do with E8.. but it is vaguely inspired by it...

View the demo... (source code is below)

Actionscript:
  1. var hw:Number = stage.stageWidth / 2;
  2. var hh:Number = stage.stageHeight / 2;
  3.  
  4. var canvas:BitmapData = new BitmapData(stage.stageWidth,stage.stageHeight, false, 0x333333);
  5. addChild(new Bitmap(canvas));
  6. var vector:Shape = Shape(addChild(new Shape()));
  7.  
  8. // contrast
  9. var cm:ColorMatrixFilter = new ColorMatrixFilter([2.6,0,0,0,-101.6,0,2.6,0,0,-101.6,0,0,2.6,0,-101.6,0,0,0,1,0]);
  10. filters = [cm];
  11.  
  12. var over:BitmapData = new BitmapData(stage.stageWidth,stage.stageHeight, false, 0x333333);
  13. var frm:Bitmap = Bitmap(addChild(new Bitmap(over)));
  14.  
  15. frm.blendMode = BlendMode.OVERLAY;
  16.  
  17. var dotNum:int = 250;
  18. var dotConnected:int = 0;
  19. var curr:int = 0;
  20. var prevConnected:int = -1;
  21. var dots:Vector.<Point> = new Vector.<Point>(dotNum, true);
  22.  
  23. // consider altering this value manually
  24. var step:Number =  Math.random()*10
  25.  
  26. var theta:Number = -.5234;
  27. for (var i:int= 0; i <dotNum; i++){
  28.     var r:Number = 280
  29.     theta += step;
  30.     var xp:Number = hw + r  * Math.cos(theta);
  31.     var yp:Number = hh + r * Math.sin(theta);
  32.     dots[i] = new Point(xp, yp);
  33. }
  34.  
  35. var col:uint = 0xFFFFFF;
  36. var a:Point, b:Point;
  37. var connections:int = 0;
  38.  
  39. var txt:TextField = TextField(addChild(new TextField()));
  40. txt.defaultTextFormat = new TextFormat("_sans", 12);
  41. txt.textColor = 0x555555, txt.x = 10, txt.y = 10;
  42. txt.autoSize = "left";
  43. txt.text = step.toString();
  44.  
  45. addEventListener(Event.ENTER_FRAME, onLoop);
  46. function onLoop(evt:Event):void{
  47.      
  48.     over.applyFilter(canvas, over.rect, new Point(0,0), new BlurFilter(10,10, 1));
  49.    
  50.     for (var i:int = 0; i<100; i++){
  51.         if (dotConnected <dotNum - 1){
  52.             if (prevConnected != dotConnected){
  53.                 a = dots[dotConnected];
  54.             }
  55.             prevConnected = dotConnected;
  56.             b = dots[curr];
  57.             curr++;
  58.            
  59.             if (a != b){
  60.                 connections++;
  61.                 vector.graphics.clear();
  62.                 vector.graphics.lineStyle(0,col,0.05);
  63.                 vector.graphics.moveTo(a.x, a.y);
  64.                 vector.graphics.lineTo(b.x, b.y);
  65.                 canvas.draw(vector);
  66.             }
  67.             if (curr> dotNum - 1){
  68.                 curr = 0;
  69.                 dotConnected++;
  70.                 col =[0,0xFFFFFF][int(Math.random()*2)];
  71.             }
  72.         }else{
  73.             break;
  74.             trace("done");
  75.             removeEventListener(Event.ENTER_FRAME, onLoop);
  76.         }
  77.     }
  78. }

Also posted in BitmapData, Math | Tagged , , | Leave a comment

Seamless Vector Texture

Actionscript:
  1. [SWF(width = 800, height = 450, backgroundColor=0xFFFFFF)]
  2.  
  3. graphics.beginFill(0xDEDEDE);
  4. graphics.drawRect(0,0,400,400);
  5.  
  6. var container:Sprite = Sprite(addChild(new Sprite()));
  7. draggable(rx(), ry(), 0x112233, 20);
  8. draggable(rx(), ry(), 0x2266FF, 15);
  9. draggable(rx(), ry(), 0x003299, 50, "rect");
  10. draggable(rx(), ry(), 0xFFFFFF, 30);
  11. draggable(rx(), ry(), 0x00CCFF, 15);
  12. draggable(rx(), ry(), 0x1188CC, 50, "rect");
  13.  
  14. function rx():Number{
  15.     return Math.random() * 200 + 100;
  16. }
  17. function ry():Number{
  18.     return Math.random() * 200 + 100;
  19. }
  20.  
  21. var guides:Shape = Shape(addChild(new Shape()));
  22. with(guides.graphics){
  23.        lineStyle(0, 0x666666)
  24.        moveTo(200,0);
  25.        lineTo(200,400);
  26.        moveTo(0, 200);
  27.        lineTo(400, 200);
  28.        moveTo(400,0);
  29.        lineTo(400,400);
  30.        moveTo(0,400);
  31.        lineTo(800,400);
  32. }
  33.  
  34. var canvas:BitmapData = new BitmapData(200,200,true, 0x00000000);
  35. var tex:BitmapData = new BitmapData(800,800,false, 0xFFFFFF);
  36.  
  37. var frame:Bitmap = Bitmap(addChild(new Bitmap(tex, "auto", true)));
  38. frame.x = 401;
  39. frame.scaleX = frame.scaleY = 0.5;
  40.  
  41. var pnt:Point = new Point();
  42. var m:Matrix = new Matrix();
  43.  
  44. addEventListener(Event.ENTER_FRAME, onLoop);
  45. function onLoop(evt:Event):void {
  46.     canvas.fillRect(canvas.rect, 0x00000000);
  47.     canvas.draw(container);
  48.     drawQuadrant(-200,0)
  49.     drawQuadrant(-400,0)
  50.     drawQuadrant(0,-200)
  51.     drawQuadrant(-200,-200);
  52.     tex.fillRect(tex.rect, 0xFFFFFF);
  53.     for (var i:int = 0; i<16; i++){
  54.         var px:Number = i % 4;
  55.         var py:Number = int(i / 4);
  56.         pnt.x = px * 200;
  57.         pnt.y = py * 200;
  58.         tex.copyPixels(canvas, canvas.rect, pnt, null, null, true);
  59.        }
  60. }
  61.  
  62. function drawQuadrant(x:Number, y:Number):void{
  63.      m.tx = x;
  64.      m.ty = y;
  65.      canvas.draw(container, m);
  66. }
  67.  
  68. // draggable Sprite
  69. function draggable(xp:Number, yp:Number, col:uint = 0xFF0000,
  70. size:Number=4, type:String="circle"):Sprite {
  71.        var s:Sprite = Sprite(container.addChild(new Sprite()));
  72.        s.x = xp;
  73.        s.y = yp;
  74.        with(s.graphics){
  75.                beginFill(col)
  76.                if(type == "circle"){
  77.                  drawCircle(size,size,size);
  78.                }else if(type == "rect"){
  79.                  drawRect(0,0,size, size);
  80.                }
  81.        }
  82.        s.buttonMode = true;
  83.        s.addEventListener(MouseEvent.MOUSE_DOWN, onDrag);
  84.        return s;
  85. }
  86. function onDrag(evt:MouseEvent):void {
  87.     var w:Number = evt.currentTarget.width;
  88.     var h:Number = evt.currentTarget.height;
  89.     evt.currentTarget.startDrag(false, new Rectangle(0,0,400 - w, 400 - h));
  90. }
  91. stage.addEventListener(MouseEvent.MOUSE_UP, onUp);
  92. function onUp(evt:MouseEvent):void{ stopDrag() }

This snippet creates a seamless texture from a few draggable vector shapes. BitmapData is used to combine four quadrants into one seamless tile.


Have a look at the swf

I created this snippet as a sketch for a new feature for my command drawing project.

Also posted in BitmapData, Graphics | Tagged , , | 4 Comments

Calculate Surface Area and Volume of a Sphere

Actionscript:
  1. var rad:Number = 1;
  2.  
  3. var surfaceArea:Number = sphereSurfaceArea(rad);
  4. var volume:Number = sphereVolume(rad)
  5. trace("surface area: ", surfaceArea);
  6. trace("volume:", volume);
  7. trace("volume / surface area:", volume / surfaceArea);
  8.  
  9. function sphereSurfaceArea(r:Number):Number{
  10.     return 4 * Math.PI * r * r;
  11. }
  12.  
  13. function sphereVolume(r:Number):Number{
  14.     return r * r * r * (4.0/3.0) * Math.PI;
  15. }
  16.  
  17. /*outputs:
  18. surface area:  12.566370614359172
  19. volume: 4.1887902047863905
  20. volume / surface area: 0.3333333333333333
  21. */

Last night I was feeling curious about sphere volume and sphere surface area... I used WolframAlpha to find the neccessary equations and then wrote the above code snippet...

Check out how WolframAlpha does it:
surface area of a sphere
surface area of a sphere with radius 10
sphere volume
sphere volume with radius 10

Also posted in Math | Tagged , , , | 2 Comments