Category Archives: Vector

BitmapData.setVector()

Actionscript:
  1. var canvas:BitmapData=new BitmapData(255,255,false,0x000000);
  2. addChild(new Bitmap(canvas, "auto", true));
  3.  
  4. var size:int = canvas.width * canvas.height;
  5.  
  6. var pixels:Vector.<uint> = new Vector.<uint>(size);
  7.  
  8. for (var i:int  = 0; i<size; i++) {
  9.     var ox:uint= i % canvas.width;
  10.     var oy:uint= i / canvas.width;
  11.     pixels[i] = oy <<16 | ox;
  12. }
  13.    
  14. canvas.setVector(canvas.rect, pixels);

setVector() is used to set a group of pixels. I've been wanting this feature for a long time. I'm assuming that it's at least a little faster than using a bunch of setPixel() calls, but i haven't tested it. Of course, Pixel Bender will be much faster than this.... as long as your not on a powerPC based mac (or some other older computer) ... as you may or may not know Pixel Bender will run very slow on a powerPC mac:

Quote from Tinic Uro - Flash Player Engineer

But... I have more news you might not like. ;-) If you ever run a Pixel Bender filter on PowerPC based Mac you will see that it runs about 10 times slower than on an Intel based Mac. For this release we only had time to implement a JIT code engine for Intel based CPUs. On a PowerPC Mac Pixel Bender kernels will run in interpreted mode. I leave it up to you to make a judgment of how this will affect you. All I can say: Be careful when deploying content using Pixel Bender filters, know your viewers.

Read the rest of the article here.

Also posted in BitmapData, pixel manipulation, setPixel | Tagged , | Leave a comment

2D Vector

Actionscript:
  1. var map:Vector.<Vector.<int>> = new Vector.<Vector.<int>>();
  2. map[0] = new Vector.<int>();
  3. map[0].push(1);
  4. map[0].push(0);
  5. map[0].push(0);
  6.  
  7. map[1] = new Vector.<int>();
  8. map[1].push(0);
  9. map[1].push(1);
  10. map[1].push(0);
  11.  
  12. map[2] = new Vector.<int>();
  13. map[2].push(0);
  14. map[2].push(0);
  15. map[2].push(1);
  16.  
  17. /*
  18. map:
  19. 1,0,0
  20. 0,1,0
  21. 0,0,1
  22. */

This creates a two dimensional Vector of ints (flash 10 player only). The first line is the real snippet... took me a few minutes to get the syntax right the first time I needed a 2D Vector.

Also posted in arrays | Tagged , | Leave a comment

drawPath() Circle Segment

drawPath() is flash 10 player only

Actionscript:
  1. [SWF(width=400,height=400,backgroundColor=0xEFEFEF,frameRate=30)]
  2.  
  3. const TWO_PI:Number = Math.PI * 2;
  4.  
  5. var resolution:Number = 50;
  6. var step:Number = TWO_PI / resolution;
  7. var maxIndex:int = 0;
  8.  
  9. var coords:Vector.<Number> = new Vector.<Number>();
  10. var drawCommands:Vector.<int> = new Vector.<int>();
  11.  
  12. // populate vectors
  13. for (var i:Number = 0; i <TWO_PI + step; i += step){
  14.     coords.push(100 * Math.cos(i));
  15.     coords.push(100 * Math.sin(i));
  16.     drawCommands.push(GraphicsPathCommand.LINE_TO);
  17. }
  18.  
  19. var circleSegment:Shape = new Shape();
  20. circleSegment.x = circleSegment.y = 200;
  21. addChild(circleSegment);
  22.  
  23. addEventListener(Event.ENTER_FRAME, onLoop);
  24. function onLoop(evt:Event):void {
  25.     with (circleSegment.graphics) {
  26.         clear();
  27.         beginFill(0x000000);
  28.        
  29.         // count by two, up to coords.length (drawCommands is exactly half the length of coords)
  30.         maxIndex =  Math.ceil((mouseX / stage.stageWidth)  * drawCommands.length) * 2;
  31.  
  32.         drawPath(drawCommands, coords.slice(0, maxIndex));
  33.     }
  34. }

The new additions to the Drawing API are great. This is just a quick test done with drawPath() to create a circle segment - this technique could be used to draw a pie chart.

The old way would have required lots of calls to the Graphics.lineTo() method. Take a look:

Actionscript:
  1. const TWO_PI:Number = Math.PI * 2;
  2.  
  3. var resolution:Number = 50;
  4. var step:Number = TWO_PI / resolution;
  5. var maxAngle:Number;
  6.  
  7. var circleSegment:Shape = new Shape();
  8. circleSegment.x = circleSegment.y = 200;
  9. addChild(circleSegment);
  10.  
  11. addEventListener(Event.ENTER_FRAME, onLoop);
  12. function onLoop(evt:Event):void {
  13.     with (circleSegment.graphics) {
  14.         clear();
  15.         beginFill(0x000000);
  16.        
  17.         // count up to (TWO_PI + step), we add step to close the circle
  18.         maxAngle =  (TWO_PI + step) *  (mouseX / stage.stageWidth);
  19.  
  20.         for (var i:Number = 0; i <maxAngle; i += step) {
  21.             var xp:Number = 100 * Math.cos(i);
  22.             var yp:Number = 100 * Math.sin(i);
  23.             lineTo(xp, yp);
  24.         }
  25.     }
  26. }

If your interested in learning more about the FP10 Drawing API features, you can read more here.

Also posted in Graphics | Tagged , , , | Leave a comment