Actionscript:
-
// from http://codebase.dbp-site.com/code/find-difference-between-two-angles-25
-
function angleDifference(angle0:Number, angle1:Number):Number{
-
return Math.abs((angle0 + 180 - angle1) % 360 - 180);
-
}
-
-
trace("get the angle between:");
-
trace("350 and 10 =", angleDifference(350, 10));
-
trace("180 and -1 =", angleDifference(180, -1));
-
trace("-10 and 5 =", angleDifference(-10, 5));
-
trace("725 and -45 =", angleDifference(725, -45));
-
/* outputs:
-
get the angle between:
-
350 and 10 = 20
-
180 and -1 = 179
-
-10 and 5 = 15
-
725 and -45 = 50
-
*/
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 actionscript, flash |
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.
Actionscript:
-
var size:Number = 800;
-
var canvas:BitmapData = new BitmapData(size,size,false, 0x000000);
-
addChild(new Bitmap(canvas, "auto", true));
-
-
scaleX = scaleY = .5;
-
var pix:Number = size * size;
-
var scale:Number = 1/(size/3);
-
-
for (var i:Number = 0; i<pix; i++){
-
var xp:Number = (i % size);
-
var yp:Number = int(i / size);
-
var xt:Number = xp * scale;
-
var yt:Number = yp * scale;
-
var ca:Number = (Math.abs(Math.tan(yt) * Math.pow(Math.sin(xt),3)) * 100 ) % 155;
-
var cb:Number = (Math.abs(Math.tan(xt) * Math.pow(Math.sin(yt),3)) * 100) % 155;
-
ca|= cb;
-
canvas.setPixel(xp, yp, ca <<16 | ca <<8 | ca);
-
}
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
Actionscript:
-
var xp:Number=0,yp:Number=0;
-
var r:Number=0,t:Number=0;
-
var speed:Number=.01;
-
var scale:Number=10;
-
var shape:Shape = Shape(addChild(new Shape()));
-
shape.x=stage.stageWidth/2;
-
shape.y=stage.stageHeight/2;
-
shape.graphics.lineStyle(0,0x000000);
-
-
var s:Number=100;
-
var range:Number=.2;
-
var b:Number=20;
-
var a:Number =b*range;
-
-
addEventListener(Event.ENTER_FRAME, onLoop);
-
function onLoop(evt:Event):void {
-
range=.2;
-
b=20;
-
a=b*range;
-
t=0;
-
shape.graphics.clear();
-
shape.graphics.lineStyle(0,0);
-
for (var i:int = 0; i<6000; i++) {
-
var sin:Number=Math.cos(t*int(mouseY/20));
-
r = Math.sqrt(40 * b * (b - a * (Math.pow(sin, int(mouseX/20)))));
-
xp=r*Math.cos(t);
-
yp=r*Math.sin(t);
-
-
if (t==0) {
-
shape.graphics.lineStyle(0, 0);
-
shape.graphics.moveTo(xp, yp);
-
} else {
-
shape.graphics.lineTo(xp, yp);
-
}
-
t+=speed;
-
if (t> (2 * Math.PI + speed)) {
-
range+=.3;
-
a=b*range;
-
t=0;
-
}
-
}
-
}
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 actionscript, flash |