The minkowski operator, illustrated here on 2D Openscad shapes. Shown below is an example where we use only 2D shapes, that Openscad also supports (we then use square in place of cube, circle for spheres and so). a small sphere will "round" all the edges and surfaces of a given reference object. The effect is like when a shape is used to paint or "brush" a parent shape: e.g. But it is so slow that we almost never use it. The color components were chosen according to the size of the mug, so we get an automatic color gradient whatever the depth of the recursion. (an indicator is that the syntax is now colored: another long-waited for feature!)īy the way, you see also the use of the "color" operator, that highlights each of the sub-mugs. Make sure you have a version of Openscad that allows this It would not make any sense to go "deeper" in the recursion anyhow.Ī recursive mug. Remember the mug-with-a-cup that we made in part two? Here is how we can write a recursive mug, that will add smaller-mugs-in-mugs till it get smaller than the constant wall width. A test will be made to stop calling ourselves after some condition is met, e.g. To avoid looping indefinitely (and killing the computer memory and CPU), we must also introduce conditionals. What is recursion? Well, it happens when a module calls itself in order to build more versions of its own shape, smaller or in another place. constant! Advanced operators like "assign( )" helped circumvent some of the issues, but they made the source code even worse in my opinion. Previously, variables had the amazing property that they were. Only the latest version of Openscad made recursion possible. More importantly in my opinion, it lets you add statements later on, without the need to alter existing code - like the echo above. It makes the source code a little longer, but it makes it more consistent. Some seasoned programmers use braces all the time, even when they are unneeded. No problem when only one operation follows, as in the former cascade of loops.īut as soon as you need more than one command, you need to use braces:Ĭylinder(r=t,h=t) // concentric cylinders depends on "t" The loop variable (like r,x,y above) are usable only within the so-called "scope" of the loop, which is "below" in the hierarchy. You must also keep this in mind with for loops. If you recall, we use curly brackets to group items together, so that a modifier like translate shifts more than only the objects that follows immediately. Rotation is often preferred as it also "fixes" the orientation of the duplicated part.īut scaling operators also are useful to invert axes (click to see the code). Translations will keep the same orientation for the four parts! No biggie for a rotation-invariant cylinder, but you see below the issue with a shape that is no more a cylinder. ![]() ![]() Actually, translating as we did above is probably not the wisest choice, because it does not change the "orientation" of the duplicated object itself. ![]() I tend to prefer either rotation, or inverted axes (with the scale operator and -1 on some axes). There are still other ways to create the same design (eg. Then, the column seemingly positioned at (10,10) is in fact "rotated" to each of the four symmetrical points around the (0,0) central axis.Ĭube(, center=true) To do so, we use variable "r" to rotate around the Z axis below. before reaching 360° as it would be at the exact same place as 0° (and hence it would create a second, useless, cylinder there). This is done below, where r is set in turn to 0, then 90, and finally 180. Less code almost always means less bugs: a range can be specified as a list of values as above, but it can also be defined by a triplet. The "for" loops handle vectors of any size like, but we can benefit from a few additional variations, in order to improve the 4-cylinder example further. Note how they can be stored themselves in variables:Ĭube_size= // a triplet, aka 3-item vector storedĬube(cube_size) //. We already used vectors when specifying the three X,Y,Z arguments used to define cubes or translations for example.
0 Comments
Leave a Reply. |