Category Archives: misc

Tweetcoding

Actionscript:
  1. g=graphics;
  2. mt=g.moveTo;
  3. lt=g.lineTo;
  4. ls=g.lineStyle;
  5. m=Math;
  6. r=m.random;
  7. s=m.sin;
  8. i=0;
  9. o={};
  10. function f(e){
  11.     s=150,x=y=s,z=-s,c=(!i)?addChild(new Bitmap(new BitmapData(s,s))).bitmapData:c;while(i<22500)i++,c.setPixel(i%s,i/s,(i%s|i/s)*mouseX);i=1;
  12. }
  13. addEventListener("enterFrame",f);

Have to link to this very fun new contest:

http://tweetcoding.machine501.com/

http://gskinner.com/playpen/tweetcoding.html

I may just have to get a twitter account...

Posted in misc | Tagged , | Leave a comment

24hr Clock

Actionscript:
  1. var clock:Sprite = Sprite(addChild(new Sprite()));
  2. clock.x = clock.y = 150;
  3.  
  4. var bg:Shape = Shape(clock.addChild(new Shape()));
  5. with (bg.graphics) lineStyle(2, 0x666666), beginFill(0xEFEFEF), drawCircle(0,0,110);
  6.  
  7. var hHand:Shape = clockHand(6, 50);
  8. var mHand:Shape = clockHand(2, 80);
  9. var sHand:Shape = clockHand(1, 90);
  10.  
  11. var center:Shape = Shape(clock.addChild(new Shape()));
  12. with (center.graphics) beginFill(0x000000), drawCircle(0,0,5);
  13.  
  14. var hInc:Number = 360/24;
  15. var msInc:Number = 360/60 ;
  16. var nOff:Number = 6;
  17. var verdana:TextFormat = new TextFormat("Verdana", 8);
  18. // add numbers to clock
  19. for (var i:int = 0; i<24; i++){
  20.      var ang:Number = (i * hInc - 90) * Math.PI/180;
  21.      createNumber(70,ang, i.toString());
  22.      var ms:Number = i * 2.5;
  23.      if (ms % 5 == 0){
  24.         createNumber(95, ang, ms.toString());
  25.      }
  26. }
  27.  
  28. addEventListener(Event.ENTER_FRAME, onLoop);
  29. function onLoop(evt:Event):void {
  30.     var date:Date = new Date();
  31.     hHand.rotation =  hInc * date.getHours();
  32.     mHand.rotation =  msInc * date.getMinutes();
  33.     sHand.rotation =  msInc * date.getSeconds();
  34. }
  35.  
  36. function clockHand(thickness:Number, leng:Number):Shape{
  37.     var hand:Shape = Shape(clock.addChild(new Shape()));
  38.     with (hand.graphics) {
  39.         lineStyle(thickness, 0x000000, 1, true, LineScaleMode.NORMAL, CapsStyle.SQUARE);
  40.         lineTo(0,-leng);
  41.     }
  42.     return hand;
  43. }
  44.  
  45. function createNumber(radius:Number, theta:Number, str:String):void{
  46.     var t:TextField = TextField(clock.addChild(new TextField()));
  47.     with (t) defaultTextFormat = verdana, t.autoSize = "left";
  48.     t.text = str;
  49.     t.x = radius * Math.cos(theta) - nOff;
  50.     t.y = radius* Math.sin(theta) - nOff;
  51. }

I was watching a movie the other day and I saw a 24 hour clock in the background of one of the shots. After the movie I coded this snippet. It draws a very basic clock with an hour hand that takes 24 hours to go full circle...

Posted in misc | Tagged , | 4 Comments

Square Bracket Syntax & Typecasting

Actionscript:
  1. addChild(new TextField());
  2. getChildAt(0)["text"] = "Hello World";

Notice that square bracket syntax makes it so we don't need to typecast. Take a look at the other way:

Actionscript:
  1. addChild(new TextField());
  2. TextField(getChildAt(0)).text = "Hello World";

Typecasting is the way to go.... the square bracket technique is just an interesting trick.

I wrote this after somehow stumbling on this very entertaining page - it shows hello world written in approximately 200 different languages...

Also posted in dynamic | Tagged , | Leave a comment

Tausworthe Random Numbers (seeded)

Actionscript:
  1. // best thing about this are the random seeds
  2. var s1:Number= 0xFFFFFF;
  3. var s2:Number = 0xCCCCCC;
  4. var s3:Number= 0xFF00FF;
  5.  
  6. // saw this algorithm on this somewhat annoying thread:
  7. // http://www.reddit.com/r/programming/comments/7yjlc/why_you_should_never_use_rand_plus_alternative/
  8. // additional googling brought me to this: http://wakaba.c3.cx/soc/kareha.pl/1100499906/
  9. // and finally to this ( didn't understand most of this one) www.ams.org/mcom/1996-65-213/S0025-5718-96-00696-5/S0025-5718-96-00696-5.pdf
  10.  
  11. function rand():Number {
  12.     s1=((s1&4294967294)<<12)^(((s1<<13)^s1)>>19);
  13.     s2=((s2&4294967288)<<4)^(((s2<<2)^s2)>>25);
  14.     s3=((s3&4294967280)<<17)^(((s3<<3)^s3)>>11);
  15.     var r:Number = (s1^s2^s3) * 2.3283064365e-10;
  16.     r = (r<0) ? r+=1 : r;
  17.     return r;
  18. }
  19.  
  20. // see a visual comparison between this and actionscript's Math.random()
  21.  
  22. var canvas:BitmapData = new BitmapData(400,400,false, 0xCCCCCC);
  23. addChild(new Bitmap(canvas));
  24.  
  25. var posX:Number;
  26. var posY:Number;
  27.  
  28. addEventListener(Event.ENTER_FRAME, onLoop);
  29.  
  30. function onLoop(evt:Event):void {
  31.  
  32.     for (var i:int = 0; i<100; i++){
  33.         posX = rand() * 180
  34.         posY = rand() * 180
  35.         canvas.setPixel( 100 + posX, 10 + posY, 0x000000);
  36.     }
  37.    
  38.     for (i = 0; i<100; i++){
  39.         posX = Math.random() * 180
  40.         posY = Math.random() * 180
  41.         canvas.setPixel( 100 + posX, 210 + posY, 0x000000);
  42.     }
  43. }

The above snippet demo's an alternative random number generator that uses the Tausworthe method.


Top square is Tausworthe and bottom is Math.random()

I don't actually know much about this, or fully understand how it works. I just saw it on a random reddit thread, googled about it for a few minutes and then ported it to actionscript.

There's lots of talk about what the BEST random number generator is.... which one is the fastest, which one is the most random. etc... I don't really know much about that, especially since I'm just playing around with code and I don't need a RNG for some scientific experiment. For me, what's nice about this approach is the three random seeds. For some reason, Math.random() doesn't have a place where you can enter a seed for random numbers. Random seeds are very useful when you want to do something random but want to be able to replicate the random results at a later time. For instance:

Actionscript:
  1. var s1:Number= 0xFFFFFF;
  2. var s2:Number = 0xCCCCCC;
  3. var s3:Number= 0xFF00FF;
  4.  
  5. trace("first three values:");
  6. trace(rand());
  7. trace(rand())
  8. trace(rand())
  9.  
  10. s1 = 0xFF;
  11. s2 = 0xEFEFEF;
  12. s3 = 19008;
  13.  
  14. trace("\nfirst three values with different seeds:");
  15. trace(rand());
  16. trace(rand())
  17. trace(rand())
  18.  
  19. s1 = 0xFFFFFF;
  20. s2  = 0xCCCCCC;
  21. s3 = 0xFF00FF;
  22.  
  23. trace("\noriginal three values:");
  24. trace(rand());
  25. trace(rand())
  26. trace(rand())
  27.  
  28. function rand():Number {
  29.     s1=((s1&4294967294)<<12)^(((s1<<13)^s1)>>19);
  30.     s2=((s2&4294967288)<<4)^(((s2<<2)^s2)>>25);
  31.     s3=((s3&4294967280)<<17)^(((s3<<3)^s3)>>11);
  32.     var r:Number = (s1^s2^s3) * 2.3283064365e-10;
  33.     r = (r<0) ? r+=1 : r;
  34.     return r;
  35. }
  36.  
  37. /*
  38. outputs:
  39. first three values:
  40. 0.051455706589931385
  41. 0.050584114155822715
  42. 0.417276361484596
  43.  
  44. first three values with different seeds:
  45. 0.6032885762463134
  46. 0.9319786790304015
  47. 0.8631882804934321
  48.  
  49. original three values:
  50. 0.051455706589931385
  51. 0.050584114155822715
  52. 0.417276361484596
  53. */

I stumbled upon a bunch of other random number algorithms, maybe I'll throw them in a class in the next couple days.

Also posted in Number | Tagged , | 2 Comments