[OpenSCAD] Accumulating variable in openSCAD

runsun runsun at gmail.com
Mon Oct 22 16:36:56 EDT 2018


Since Neon22 already figured out the formula to generate the offset (in the
dims), which is the critical part, there's actually no need to have 2
for-loops:

difference() {
    cube([90, 30, 5]);    
    for (a = [0: 1: 20]) {
        translate([4+(1+a/10)*2, 4, -1])
            cylinder(h=7, d=1+a/10);
    }
}

If the formula is too complicated to figure out (or you are too lazy), an
alternative is to use recursive module:

module holes(offset=0, d=d0, _i=0)
{
    // Draw the cylinder 

    translate( [offset,4,-1] ) cylinder( ... );
    
    // Draw next one with new variable values

    if(_i< number_of_holes)
        holes( offset= offset + ...
               ,  d = d + ...
               , _i = _i+1
               ); 
}

In this approach you only need to worry about the relationship between the
current iteration and the one right before it. 



-----

$  Runsun Pan, PhD $ libs: scadx , doctest , faces ( git ), offline doc ( git ), runscad.py ( 2 , git ), editor of choice: CudaText  ( OpenSCAD lexer ); $ Tips ; $ Snippets 

--
Sent from: http://forum.openscad.org/



More information about the Discuss mailing list