Category Archives: setPixel

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);

Also posted in motion | Tagged , | Leave a comment

Image Airbrush

Actionscript:
  1. [SWF(width=500, height=500, backgroundColor=0x000000, frameRate=30)]
  2.  
  3. // draw an ugly gradient
  4. var size:int = 500;
  5. var pixNum:int = size * size;
  6. var gradient:BitmapData = new BitmapData(size, size, true, 0xFF000000);
  7.  
  8. gradient.lock();
  9. var xp:int, yp:int;
  10. for (var i:int = 0; i<pixNum; i++){
  11.     xp = i % size;
  12.     yp = i / size;
  13.     gradient.setPixel(xp, yp, (yp /= 2) <<16 | (255 - yp) <<8 | (xp / 2) );
  14. }
  15. gradient.unlock();
  16.  
  17. // draw an alphaChannel (radial gradient)
  18. var radius:Number = 50;
  19. var diameter:Number = radius * 2;
  20.  
  21. pixNum = diameter * diameter;
  22.  
  23. var brushAlpha = new BitmapData(diameter, diameter, true, 0x00000000);
  24. var dx:int, dy:int;
  25. var ratio:Number = 255 / radius;
  26. var a:int;
  27.  
  28. brushAlpha.lock();
  29. for (i = 0; i<pixNum; i++){
  30.     xp = i % diameter;
  31.     yp = i / diameter;
  32.     dx = xp - radius;
  33.     dy = yp - radius;
  34.     a = int(255 - Math.min(255,Math.sqrt(dx * dx + dy * dy) * ratio));
  35.     brushAlpha.setPixel32(xp, yp, a <<24);
  36. }
  37. brushAlpha.unlock();
  38.  
  39. // create a black canvas
  40. var canvas:BitmapData = new BitmapData(size, size, true, 0xFF000000);
  41. addChild(new Bitmap(canvas));
  42.  
  43. addEventListener(Event.ENTER_FRAME, onLoop);
  44. function onLoop(evt:Event):void {
  45.     // draw the gradient onto the canvas using the alphaChannel (brushAlpha);
  46.     xp = mouseX - radius;
  47.     yp = mouseY - radius;
  48.     canvas.copyPixels(gradient,
  49.                     new Rectangle(xp, yp, diameter, diameter),
  50.                     new Point(xp, yp), brushAlpha, new Point(0,0), true);
  51. }

This demo creates an airbrush that paints one BitmapData onto another. This is achieved by using the alpha related arguments of the BitmapData.copyPixels() function. If your not familiar with these you should take some time to play around with them... they're very powerful.

copyPixels() is the fastest way to draw in flash, so if you need speed, copyPixels() is the way to go. It's much faster than using draw().

Also posted in BitmapData | Tagged , | 2 Comments

Quadratic Bezier

Actionscript:
  1. var canvas:BitmapData=new BitmapData(280,280,false,0x000000);
  2. addChild(new Bitmap(canvas, PixelSnapping.AUTO, true));
  3. var color:uint;
  4. // anchor x1, anchor y1,
  5. // control-handle x2, control-handle y2,
  6. // anchor x3, anchor y3, [resolution incremental value between 0-1]
  7. function quadBezier(x1:Number, y1:Number, x2:Number, y2:Number, x3:Number, y3:Number, resolution:Number=.03):void {
  8.     var b:Number,pre1:Number,pre2:Number,pre3:Number,pre4:Number;
  9.     for (var a:Number = 0; a <1;a+=resolution) {
  10.    
  11.         b=1-a;
  12.         pre1=(a*a);
  13.         pre2=2*a*b;
  14.         pre3=(b*b);
  15.        
  16.         canvas.setPixel(pre1*x1 + pre2*x2  + pre3*x3 ,
  17.                                      pre1*y1 + pre2*y2 + pre3*y3, color);
  18.     }
  19. }
  20.  
  21. // draw a few
  22. color = 0xFFFFFF;
  23.  
  24. for (var i:int = 0; i<20; i++){
  25. quadBezier(40,100, 150 , 20 + i * 10 , 200, 100,.01);
  26. }
  27.  
  28. color = 0xFF0000;
  29.  
  30. for (i= 0; i<20; i++){
  31. quadBezier(150,200, 100 + i * 10, 100  , 120, 30,.01);
  32. }

The above demos a function that draws quadratic bezier curves using setPixel().

One of the first posts on this site was a snippet that used setPixel() to draw a cubic bezier curve. I recently needed to do the exact same thing but I wanted to use a quadratic bezier... I knew I had the code laying around somewhere, but I couldn't seem to find it so I just looked on wikipedia and changed the previous cubicBezier() function accordingly.

Also posted in BitmapData, bezier, graphics algorithms, pixel manipulation | Tagged , , | 2 Comments

Bresenham Circle Filled

Actionscript:
  1. var canvas:BitmapData = new BitmapData(400, 400, false, 0xCCCCCC);
  2. addChild(new Bitmap(canvas));
  3.  
  4. fillCircle(100,100,50,0xFF0000);
  5.  
  6. function fillCircle(xp:Number,yp:Number, radius:Number, col:uint = 0x000000):void {
  7.     var xoff:int =0;
  8.     var yoff:int = radius;
  9.     var balance:int = -radius;
  10.  
  11.     while (xoff <= yoff) {
  12.          var p0:int = xp - xoff;
  13.          var p1:int = xp - yoff;
  14.          
  15.          var w0:int = xoff + xoff;
  16.          var w1:int = yoff + yoff;
  17.          
  18.          hLine(p0, yp + yoff, w0, col);
  19.          hLine(p0, yp - yoff, w0, col);
  20.          
  21.          hLine(p1, yp + xoff, w1, col);
  22.          hLine(p1, yp - xoff, w1, col);
  23.        
  24.         if ((balance += xoff++ + xoff)>= 0) {
  25.             balance-=--yoff+yoff;
  26.         }
  27.     }
  28. }
  29.  
  30. function hLine(xp:Number, yp:Number, w:Number, col:uint):void {
  31.     for (var i:int = 0; i <w; i++){
  32.         canvas.setPixel(xp + i, yp, col);
  33.     }
  34. }

An implementation of yesterdays post that draws a filled circle instead of an outlined circle.

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