setPixel() Flames Attractor

Actionscript:
  1. [SWF(width = 600, height = 600)]
  2. var a:Number = 0.02;
  3.  
  4. var xn:Object = new Object();
  5.  
  6. var scale:Number = 20;
  7. var iterations:Number = 10000;
  8.  
  9. var canvas:BitmapData = Bitmap(addChild(new Bitmap(new BitmapData(600,600,false,0xEFEFEF)))).bitmapData;
  10.  
  11. addEventListener(Event.ENTER_FRAME, onLoop);
  12. function onLoop(evt:Event):void {
  13.    
  14.     canvas.fillRect(canvas.rect, 0xEFEFEF);
  15.     a = mouseX / 100;
  16.    
  17.     // equations from here: http://www.discretedynamics.net/Attractors/Flames.htm
  18.     // i used object syntax so that I could really reflect the math notation, changing these to
  19.     // 3 variables will significantly improve performance:
  20.     xn["n-1"] =  mouseX / 200;
  21.     xn["n"] =   mouseY / 200;
  22.     xn["n+1"] = 0;
  23.    
  24.     for (var i:int = 0; i<iterations; i++){
  25.          
  26.           xn["n+1"] = ((a * xn["n"]) / (1 + (xn["n-1"] * xn["n-1"]))) - ((xn["n-1"]) / (1 + (xn["n"] * xn["n"])));
  27.          
  28.           canvas.setPixel( 280 + xn["n-1"] * scale, 300 + xn["n"] * scale, 0x000000);
  29.          
  30.           xn["n-1"] = xn["n"];
  31.           xn["n"] = xn["n+1"];
  32.     }
  33. }

This is an attractor that vaguely resembles flames, I wrote this code so that it would be very similar to the math notation to help anyone who wonders how to go from notation to code. You could replace the Object syntax with 3 separate variables for increased performance.

See the equation here:
http://www.discretedynamics.net/Attractors/Flames.htm


This snippet will create something that looks like this and is reactive to the mouse location

This entry was posted in setPixel 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 *

*
*