Category Archives: Math

Slow Line Drawing

Actionscript:
  1. var canvas:BitmapData = Bitmap(addChild(new Bitmap(new BitmapData(400, 400, false, 0x000000)))).bitmapData;
  2.  
  3. function line(x1:Number, y1:Number, x2:Number, y2:Number, res:int=10):void{
  4.     var dx:Number = x2 - x1;
  5.     var dy:Number = y2 - y1;
  6.     var dist:Number = Math.sqrt((dx * dx) + (dy * dy));
  7.     var step:Number = 1 / (dist / res);
  8.     for (var i:Number = 0; i<=1; i+= step){
  9.         // lerp : a  + (b - a) * f
  10.         canvas.setPixel(x1 + dx * i, y1 + dy * i, 0xFFFFFF);
  11.     }
  12. }
  13.  
  14. addEventListener(Event.ENTER_FRAME, onLoop);
  15. function onLoop(evt:Event):void {
  16.    canvas.fillRect(canvas.rect, 0x000000);
  17.   line(100 , 100, mouseX, mouseY,1);
  18.   line(100 + 50, 100, mouseX+ 50, mouseY,5);
  19. }

Yesterday I posted an implementation of the Bresenham Line Algorithm. Today I'm posting a comparatively slow way to draw a line with setPixel(). This snippet uses lerp and the Pythagorean theorem. It works nicely for small numbers of lines, its easy to draw dotted lines with it and its easy to explain. In a real app where you needed to use setPixel() to draw a line you should use one of the fast algorithms like Wu or Bresenham.

I didn't originally write this snippet to use set pixel... a few weeks ago I wrote something very similar to calculate a set of x y coords between two given points. In the program I used it in speed wasn't an issue (as I only needed to run the function one time). I've needed this kind of function many times before in games and small apps...

This was the original:

Actionscript:
  1. function calculatePoints(x1:Number, y1:Number, x2:Number, y2:Number, res:int=10):Array{
  2.     var points:Array = new Array();
  3.     var dx:Number = x2 - x1;
  4.     var dy:Number = y2 - y1;
  5.     var dist:Number = Math.sqrt((dx * dx) + (dy * dy));
  6.     var step:Number = 1 / (dist / res);
  7.     for (var i:Number = 0; i<=1; i+= step){
  8.         points.push(new Point(x1 + dx * i, y1 + dy * i));
  9.     }
  10.     return points;
  11. }
  12.  
  13. trace(calculatePoints(0,0,100,0,10));
  14. /* outputs:
  15. (x=0, y=0),(x=10, y=0),(x=20, y=0),(x=30.000000000000004, y=0),(x=40, y=0),(x=50, y=0),(x=60, y=0),(x=70, y=0),(x=80, y=0),(x=89.99999999999999, y=0),(x=99.99999999999999, y=0)
  16. */

...and another version to allow you to specify the number of points to calculate rather than the pixel interval at which they should be calculated:

Actionscript:
  1. function calculatePoints(x1:Number, y1:Number, x2:Number, y2:Number, pointNum:int=10):Array{
  2.     var points:Array = new Array();
  3.     var step:Number = 1 / (pointNum + 1);
  4.     for (var i:Number = 0; i<=1; i+= step){
  5.         points.push(new Point(x1 + (x2 - x1) * i, y1 + (y2 - y1) * i));
  6.     }
  7.     return points;
  8. }
  9.  
  10. trace(calculatePoints(0,30,30,0,1));
  11. /* outputs:
  12. (x=0, y=30),(x=15, y=15),(x=30, y=0)
  13. */

This last version isn't perfect, sometimes the pointNum will be off by 1, I may fix that in a future post.

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

Harmonic Series

Actionscript:
  1. var harmonic:Number = 0;
  2. var k:Number = 1;
  3. addEventListener(Event.ENTER_FRAME, onLoop);
  4. function onLoop(evt:Event):void {
  5.      harmonic += 1 / k;
  6.      trace("+");
  7.      trace("1 / " + k + "              =              " + harmonic);
  8.      k+=1;
  9. }

Read more about the harmonic series at wikipedia.


1 / 1 = 1
+
1 / 2 = 1.5
+
1 / 3 = 1.8333333333333333
+
1 / 4 = 2.083333333333333
+
...

Posted in Math | Tagged , , | Leave a comment

Gumowski / Mira

Actionscript:
  1. [SWF(width = 600, height = 600)]
  2. var a:Number = 0.02;
  3. var b:Number = .9998;
  4.  
  5. var xn1:Number = 5;
  6. var yn1:Number = 0;
  7. var xn:Number, yn:Number;
  8.  
  9. var scale:Number = 10;
  10. var iterations:Number = 20000;
  11.  
  12. function f(x:Number):Number{
  13.     var x2:Number = x * x;
  14.     return a * x + (2 * (1 - a) * x2) / (1 + x2);
  15. }
  16.  
  17. var canvas:BitmapData = Bitmap(addChild(new Bitmap(new BitmapData(600,600,false,0xEFEFEF)))).bitmapData;
  18.                                                                        
  19. addEventListener(Event.ENTER_FRAME, onLoop);
  20. function onLoop(evt:Event):void {
  21.    
  22.     canvas.fillRect(canvas.rect, 0xEFEFEF);
  23.     a = mouseY / 1000;
  24.     xn1 = mouseX / 30;
  25.     yn1 = 0;
  26.     for (var i:int = 0; i<iterations; i++){
  27.           xn = xn1;
  28.           yn = yn1;
  29.          
  30.           xn1 = b * yn + f(xn);
  31.           yn1 =  -xn + f(xn1);
  32.           canvas.setPixel( 280 + xn1 * scale, 300 + yn1 * scale, 0x000000);
  33.     }
  34. }

Notice the setup for this is very similar to yesterdays flames attractor post.

Back in october of last year I stumbled upon the excellent subblue website by Tom Beddard. I was REALLY impressed by a blog post about Gumowski / Mira patterns. If you haven't seen it you should go take a look.

I'd never heard of Gumowski / Mira patterns before and made a mental note to go and try to read about them and maybe find an equation to port to actionscript or processing. Anyway, a few days ago I decided to go ahead and look up the math and... this is the result.

I got equation over at mathworld...

For simplicity I intentionally made the actionscript code look as much like the mathworld equation as possible. Using f for my function name and using xn1, yn1 etc... There are a few speed optimizations that could be made but I wanted this snippet to be very readable.

Here are a few examples of what this code will generate:

Also posted in setPixel | Tagged , | Leave a comment

Zig Zag Binary ix2 & i/2

Actionscript:
  1. var txt:TextField = TextField(addChild(new TextField()));
  2. txt.text = "";
  3. txt.width = 190;
  4. txt.height = 400;
  5. txt.multiline = true;
  6.  
  7. var count:int = 1;
  8. function render():void{
  9.     var line = int(count).toString(2);
  10.     while(line.length <31){
  11.         line = "0" + line;
  12.     }
  13.     txt.appendText(line + "\n");
  14.     txt.scrollV= txt.maxScrollV;
  15. }
  16.  
  17. addEventListener(Event.ENTER_FRAME, onCountUp);
  18. function onCountUp(evt:Event):void {
  19.     count *= 2;
  20.     render();
  21.     if (count ==0x40000000){
  22.         removeEventListener(Event.ENTER_FRAME, onCountUp);
  23.         addEventListener(Event.ENTER_FRAME, onCountDown);
  24.     }
  25. }
  26. function onCountDown(evt:Event):void {
  27.     count /= 2;
  28.     render();
  29.     if (count ==1){
  30.         addEventListener(Event.ENTER_FRAME, onCountUp);
  31.         removeEventListener(Event.ENTER_FRAME, onCountDown);
  32.     }
  33. }

The above animates a zig zag pattern in a TextField.

Also posted in string manipulation, strings | Leave a comment