Added 1st draft of sphere() and sphere0() methods

This commit is contained in:
walterhiggins 2013-01-14 22:54:24 +00:00
parent 719080ce78
commit 422f3da9c4
2 changed files with 86 additions and 6 deletions

View file

@ -572,12 +572,10 @@ var Drone = Drone || {
var xo = (a-x0);
var yo = (b-y0);
if (fill){
if (xo < 0 && yo < 0){
// bottom left quadrant
drone.fwd(yo).right(xo)
.box(block,Math.abs(xo*2)+1,height,Math.abs(yo*2)+1)
.back(yo).left(xo);
}
// wph 20130114 more efficient esp. for large cylinders/spheres
if (yo < 0){
drone.fwd(yo).right(xo).box(block,1,height,Math.abs(yo*2)+1).back(yo).left(xo);
}
}
gotoxy(xo,yo).box(block,1,height,1).move('center');
};

View file

@ -0,0 +1,82 @@
load(__folder + "drone.js");
Drone.extend('sphere', function(block,radius)
{
var lastRadius = radius;
var slices = [[radius,0]];
var diameter = radius*2;
for (var i = 0; i <= radius;i++){
var newRadius = Math.round(Math.sqrt(radius*radius - i*i));
if (newRadius == lastRadius)
slices[slices.length-1][1]++;
else
slices.push([newRadius,1]);
lastRadius = newRadius;
}
this.chkpt('sphere');
//
// mid section
//
this.up(radius - slices[0][1])
.cylinder(block,radius,(slices[0][1]*2)-1)
.down(radius-slices[0][1]);
var yOffset = -1;
for (var i = 1; i < slices.length;i++){
yOffset += slices[i-1][1];
var sr = slices[i][0];
var sh = slices[i][1];
// northern hemisphere
this.up(radius + yOffset).fwd(radius-sr).right(radius-sr)
.cylinder(block,sr,sh)
.left(radius - sr).back( radius - sr). down(radius + yOffset);
// southern hemisphere
this.up(radius - (yOffset+sh+1)).fwd(radius-sr).right(radius-sr)
.cylinder(block,sr,sh)
.left(radius - sr).back( radius - sr). down(radius - (yOffset+sh+1));
}
return this.move('sphere');
});
//
// sphere0 creates an empty sphere but the code needs work
// - there are gaps in the sphere due to rasterization.
//
Drone.extend('sphere0', function(block,radius)
{
var lastRadius = radius;
var slices = [[radius,0]];
var diameter = radius*2;
for (var i = 0; i <= radius;i++){
var newRadius = Math.round(Math.sqrt(radius*radius - i*i));
if (newRadius == lastRadius)
slices[slices.length-1][1]++;
else
slices.push([newRadius,1]);
lastRadius = newRadius;
}
this.chkpt('sphere0');
//
// mid section
//
this.up(radius - slices[0][1])
.cylinder0(block,radius,(slices[0][1]*2)-1)
.down(radius-slices[0][1]);
var yOffset = -1;
for (var i = 1; i < slices.length;i++){
yOffset += slices[i-1][1];
var sr = slices[i][0];
var sh = slices[i][1];
// northern hemisphere
this.up(radius + yOffset).fwd(radius-sr).right(radius-sr)
.cylinder0(block,sr,sh)
.left(radius - sr).back( radius - sr). down(radius + yOffset);
// southern hemisphere
this.up(radius - (yOffset+sh+1)).fwd(radius-sr).right(radius-sr)
.cylinder0(block,sr,sh)
.left(radius - sr).back( radius - sr). down(radius - (yOffset+sh+1));
}
return this.move('sphere0');
});