Category Archives: misc

Difference Between Two Angles

Actionscript:
  1. // from http://codebase.dbp-site.com/code/find-difference-between-two-angles-25
  2. function angleDifference(angle0:Number, angle1:Number):Number{
  3.     return Math.abs((angle0 + 180 -  angle1) % 360 - 180);
  4. }
  5.  
  6. trace("get the angle between:");
  7. trace("350 and 10 =", angleDifference(350, 10));
  8. trace("180 and -1 =", angleDifference(180, -1));
  9. trace("-10 and 5 =", angleDifference(-10, 5));
  10. trace("725 and -45 =", angleDifference(725, -45));
  11. /* outputs:
  12. get the angle between:
  13. 350 and 10 =  20
  14. 180 and -1 =  179
  15. -10 and 5 =  15
  16. 725 and -45 =  50
  17. */

This snippet shows an easy way to find the difference between two angles. The tricky part about doing this is finding the difference between something like 350 and 1 (which should be 11).

Over the years I have done this a few different ways - I needed it for something today and realized that my way was a tad clunky (had a little redundant logic) - So with a quick google search I found a more elegant solution.

Also posted in Math, motion | Tagged , | 9 Comments

Global Illumination Links

Recently saw some great links making use of Global Illumination/Ambient Occlusion... these ones are from wonderfl posted by keim at Si :
This first example is based on something called AO bench.
one
two
three

and there is something called MiniLight which has been ported to Flex.

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

More Messy Code

Actionscript:
  1. var size:Number = 800;
  2. var canvas:BitmapData = new BitmapData(size,size,false, 0x000000);
  3. addChild(new Bitmap(canvas, "auto", true));
  4.  
  5. scaleX = scaleY = .5;
  6. var pix:Number = size * size;
  7. var scale:Number = 1/(size/3);
  8.  
  9. for (var i:Number = 0; i<pix; i++){
  10.        var xp:Number = (i % size);
  11.        var yp:Number = int(i / size);
  12.        var xt:Number = xp * scale;
  13.        var yt:Number = yp * scale;
  14.        var ca:Number =  (Math.abs(Math.tan(yt) * Math.pow(Math.sin(xt),3)) * 100 ) % 155;
  15.        var cb:Number =  (Math.abs(Math.tan(xt) * Math.pow(Math.sin(yt),3)) * 100)  % 155;
  16.        ca|= cb;
  17.        canvas.setPixel(xp, yp,  ca <<16 | ca <<8 | ca);
  18. }

Another messy code snippet that I e-mailed to myself at some point...

Try replacing line 16 with some of these variations:

ca &= cb;
ca += cb;
ca -= cb;
ca ^= cb;
ca %= cb

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

Messy Code

Actionscript:
  1. var xp:Number=0,yp:Number=0;
  2. var r:Number=0,t:Number=0;
  3. var speed:Number=.01;
  4. var scale:Number=10;
  5. var shape:Shape = Shape(addChild(new Shape()));
  6. shape.x=stage.stageWidth/2;
  7. shape.y=stage.stageHeight/2;
  8. shape.graphics.lineStyle(0,0x000000);
  9.  
  10. var s:Number=100;
  11. var range:Number=.2;
  12. var b:Number=20;
  13. var a:Number =b*range;
  14.  
  15. addEventListener(Event.ENTER_FRAME, onLoop);
  16. function onLoop(evt:Event):void {
  17.     range=.2;
  18.     b=20;
  19.     a=b*range;
  20.     t=0;
  21.     shape.graphics.clear();
  22.     shape.graphics.lineStyle(0,0);
  23.     for (var i:int = 0; i<6000; i++) {
  24.         var sin:Number=Math.cos(t*int(mouseY/20));
  25.         r =  Math.sqrt(40 * b * (b - a * (Math.pow(sin, int(mouseX/20)))));
  26.         xp=r*Math.cos(t);
  27.         yp=r*Math.sin(t);
  28.  
  29.         if (t==0) {
  30.             shape.graphics.lineStyle(0, 0);
  31.             shape.graphics.moveTo(xp, yp);
  32.         } else {
  33.             shape.graphics.lineTo(xp, yp);
  34.         }
  35.         t+=speed;
  36.         if (t> (2 * Math.PI + speed)) {
  37.             range+=.3;
  38.             a=b*range;
  39.             t=0;
  40.         }
  41.     }
  42. }

This is an example of raw messy code that draws some interesting shapes. I was waiting somewhere with my laptop a few weeks back and I wrote this and e-mailed this to myself - it's completely unoptimized and unedited.

I was working on documentation for QuickBox2D today but didn't finish it - so I posted this instead. I hope to finish the docs for tomorrow...

Posted in misc | Tagged , | Leave a comment