-
const TWO_PI:Number = Math.PI * 2;
-
const PI_HALF_PI:Number = Math.PI + Math.PI / 2;
-
x = stage.stageWidth / 2;
-
y = stage.stageHeight / 2;
-
-
var runHemisphere:Function = makeHemisphere(0,0, 100,0xFF0000, 0xFFCC00);
-
var dx:Number = 0;
-
var dy:Number = 0;
-
-
addEventListener(Event.ENTER_FRAME, onLoop);
-
function onLoop(evt:Event):void {
-
dx += (mouseX / 50 - dx) / 4;
-
dy += (mouseY - dy) / 4;
-
runHemisphere(dx, dy);
-
}
-
function makeHemisphere(x:Number, y:Number, size:Number, colA:uint, colB:uint):Function{
-
var s:Sprite = Sprite(addChild(new Sprite()));
-
s.x = x;
-
s.y = y;
-
var bg:Sprite = Sprite(s.addChild(new Sprite()));
-
-
with (bg.graphics) clear(), beginFill(colA), halfCircle(bg.graphics, 0,0 , size);
-
var scol:uint = colB;
-
var slice:Sprite = Sprite(s.addChild(new Sprite()));
-
return function(theta:Number, rot:Number){
-
theta %= TWO_PI;
-
if (theta <0){
-
theta = TWO_PI - -theta;
-
}
-
var scale:Number = Math.cos(theta);
-
if (theta> 1.57){
-
scol = colA;
-
} else {
-
scol = colB;
-
}
-
if (theta> Math.PI){
-
bg.rotation = 180;
-
}else{
-
bg.rotation = 0;
-
}
-
if (theta> PI_HALF_PI){
-
scol = colB;
-
}
-
with(slice.graphics) clear(), beginFill(scol), scaleYcircle(slice.graphics,size, scale);
-
s.rotation = rot;
-
}
-
}
-
// original circle function by senocular (www.senocular.com) from here http://www.actionscript.org/forums/showthread.php3?s=&threadid=30328
-
function halfCircle(g:Graphics, x:Number,y:Number,r:Number):void {
-
var c1:Number = r * (Math.SQRT2 - 1);
-
var c2:Number = r * Math.SQRT2 / 2;
-
var xr:Number = x + r, yr:Number = y + r;
-
var x_r:Number = x - r, yc1:Number = y + c1;
-
var yc2:Number = y + c2, xc1:Number = x + c1, xc2:Number = x + c2;
-
var x_c1:Number = x - c1;
-
g.moveTo(xr, y);
-
g.curveTo(xr, yc1, xc2, yc2);
-
g.curveTo(xc1, yr, x, yr);
-
g.curveTo(x - c1,yr, x - c2, yc2);
-
g.curveTo(x_r, yc1, x_r, y);
-
}
-
// circle that can be scaled on the y axis
-
function scaleYcircle(g:Graphics, r:Number, s:Number = 1):void {
-
var c1:Number = r * (Math.SQRT2 - 1);
-
var c2:Number = r * Math.SQRT2 / 2;
-
var rs:Number = r * s, c1s:Number = c1 * s, c2s:Number = c2 * s;
-
var x_r:Number = -r, y_r:Number = -rs, x_c2:Number = -c2;
-
var y_c2:Number = -c2s, x_c1:Number = -c1, y_c1:Number = -c1s
-
g.moveTo(r, 0), g.curveTo(r, c1s, c2, c2s);
-
g.curveTo(c1, rs, 0, rs), g.curveTo(x_c1,rs, x_c2, c2s);
-
g.curveTo(x_r, c1s, x_r, 0), g.curveTo(x_r,y_c1,x_c2,y_c2);
-
g.curveTo(x_c1,y_r,0,y_r), g.curveTo(c1,y_r,c2,y_c2);
-
g.curveTo(r,y_c1,r,0);
-
}
The above snippet draws a 3D hemisphere using a technique I first saw at vectorpark.com. Vectorpark.com has been around for years and it's an amazing site... if you've never seen it before I highly recommend spending some time there...
When I decided to post the half-circle snippet yesterday... I knew I would be writing this today. It's one of those things I've always thought about doing, but never gotten around to. I show vectorpark.com to my animation class (almost no coding in that class) and we emulate some of the 3D techniques for simple primitives with pure timeline animation. The above is the first time I've tried to emulate one of these techniques with code.
I was pleasantly surprised when visiting vectorpark.com this morning... Patrick Smith has posted some new work there. Really excellent stuff...
UPDATE:
Katopz converted this to a doc class and put it up on wonderfl...
6 Comments
wow! This is very cool. Anytime you’re doing math like this I learn so much!
Thanks Jon glad you like it…
result for lazy ppl like me
http://wonderfl.kayac.com/code/62b8340fc4edf6729746f077b504bb8ef994a972
thanks for putting that on wonderfl…
Awesome, Zevan! Thanks for sharing, it could be well used.