Playing with Curves Point.polar()

Actionscript:
  1. [SWF(width=600, height=500, backgroundColor=0x000000, frameRate=30)]
  2. var points:Array = new Array();
  3. var index:int = -1;
  4. function polar(thetaInc:Number, radius:Number):Point{
  5.     index++;
  6.     if (!points[index]) points[index] = 0;
  7.     return Point.polar(radius, points[index] += thetaInc);
  8. }
  9. ///////////////////////////////////////////////////
  10. // test it out:
  11.  
  12. var canvas:BitmapData = new BitmapData(600, 500, false, 0xFFFFFF);
  13.  
  14. addChild(new Bitmap(canvas, "auto", true));
  15.  
  16. var p0:Point = new Point(80, 100);
  17. var p1:Point = new Point(270, 100);
  18. var p2:Point = new Point(480, 40);
  19. var p3:Point = new Point(170, 180);
  20. var p4:Point = new Point(430, 300);
  21.  
  22. addEventListener(Event.ENTER_FRAME, onLoop);
  23. function onLoop(evt:Event):void {
  24.     for (var i:int= 0; i<100; i++){
  25.        
  26.         // reset index;
  27.         index = -1;
  28.        
  29.         p0 = p0.add(polar(.2, 4).add(polar(-.4,2).add(polar(.05, 1))));
  30.         canvas.setPixel(p0.x, p0.y, 0x000000);
  31.    
  32.         p1 = p1.add(polar(.1, 2).add(polar(-.2, 2).add(polar(.03, 1).add(polar(-.01,.5)))));
  33.         canvas.setPixel(p1.x, p1.y, 0x000000);
  34.      
  35.         p2 = p2.add(polar(.08, 3 ).add(polar(-.2, -12).add(polar(2, 10))));
  36.         canvas.setPixel(p2.x, p2.y, 0x000000);
  37.      
  38.         p3 = p3.add(polar(.08, 7).add(polar(-.2, -12).add(polar(2, 11))));
  39.         canvas.setPixel(p3.x, p3.y, 0x000000);
  40.        
  41.         p4 = p4.add(polar(.025, 2).add(polar(-.05,1)));
  42.         canvas.setPixel(p4.x, p4.y, 0x000000);
  43.     }
  44. }

The polar() function is the real trick here... the rest of the code just uses it to draw this:

The Point.polar() function is just a conversion from polar to cartesian coords:

Actionscript:
  1. x = radius * Math.cos(theta);
  2. y = radius * Math.sin(theta);

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

*
*