Dynamic Shapes

Actionscript:
  1. // build some functions:
  2. var redGradient:Function = sl(16, add(100, mult(5)));
  3.  
  4. // grid positioning
  5. var xPos:Function = add(50, mult(30, cInt(div(4))));
  6. var yPos:Function = add(50, mult(30, mod(4)));
  7.  
  8. // create some shapes:
  9. var shapes:Array = createShapes(this, 22, [["beginFill", 0xCCCCCC], ["drawCircle", 0, 0, 10], ["endFill"], ["lineStyle",1, redGradient], ["drawRect", -5, -5, 10, 10]], {x:yPos, y:xPos, rotation:mult(10)});
  10.                                                                  
  11. function createShapes(par:DisplayObjectContainer,  num:Number,
  12.                                          funcs:Array, props:Object):Array {
  13.     var shapes:Array = [];
  14.     for (var i:int = 0; i<num; i++){
  15.         shapes[i] = par.addChild(new Shape());
  16.         for (var j:int = 0; j<funcs.length; j++) {
  17.             var a:Array = funcs[j].concat();
  18.             for (var k:int = 0; k<a.length; k++){
  19.                 if (a[k] is Function){
  20.                     a[k] = a[k](i);
  21.                 }
  22.             }
  23.             for (var key:String in props){
  24.                 var v:* = props[key];
  25.                 if (v is Function){
  26.                   shapes[i][key] = v(i);
  27.                 }else{
  28.                   shapes[i][key] = v;
  29.                 }
  30.             }
  31.             shapes[i].graphics[a[0]].apply(shapes[i].graphics, a.slice(1));
  32.         }
  33.     }
  34.     return shapes;
  35. }
  36.  
  37. // function building blocks
  38. const F:Function = function(a:*):*{return a};
  39.  
  40. function cInt(f:Function=null):Function{
  41.     if (f == null) f = F;
  42.     return function(n:Number):Number {
  43.         return int(f(n));
  44.     }
  45. }
  46.  
  47. function mod(m:Number, f:Function=null):Function{
  48.     if (f == null) f = F;
  49.     return function(n:Number):Number {
  50.         return f(n) % m;
  51.     }
  52. }
  53.  
  54. function div(d:Number, f:Function=null):Function{
  55.     if (f == null) f = F;
  56.     return function(n:Number):Number {
  57.         return f(n) / d;
  58.     }
  59. }
  60.  
  61. function mult(scalar:Number, f:Function=null):Function{
  62.     if (f == null) f = F;
  63.      return function(n:Number):Number {
  64.         return f(n) * scalar;
  65.     }
  66. }
  67.  
  68. function add(off:Number, f:Function=null):Function{
  69.      if (f == null) f = F;
  70.      return function(n:Number):Number {
  71.         return f(n) + off;
  72.     }
  73. }
  74. // shift left
  75. function sl(amount:int, f:Function=null):Function{
  76.      if (f == null) f = F;
  77.      return function(n:Number):Number {
  78.         return f(n) <<amount
  79.     }
  80. }

This is an unusual snippet I wrote a couple weeks back.... not sure where I was going with this really... has some interesting ideas in it.

This entry was posted in misc and tagged , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*