From 422f3da9c428a66dd1b6692864adecab81f736fa Mon Sep 17 00:00:00 2001 From: walterhiggins Date: Mon, 14 Jan 2013 22:54:24 +0000 Subject: [PATCH] Added 1st draft of sphere() and sphere0() methods --- js-plugins/drone/drone.js | 10 ++--- js-plugins/drone/sphere.js | 82 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 6 deletions(-) create mode 100644 js-plugins/drone/sphere.js diff --git a/js-plugins/drone/drone.js b/js-plugins/drone/drone.js index 978701e..e91a89c 100644 --- a/js-plugins/drone/drone.js +++ b/js-plugins/drone/drone.js @@ -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'); }; diff --git a/js-plugins/drone/sphere.js b/js-plugins/drone/sphere.js new file mode 100644 index 0000000..766ec64 --- /dev/null +++ b/js-plugins/drone/sphere.js @@ -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'); +});