# OpenSCAD User Manual/Transformations
### Basic concept

Transformations affect their child nodes and as the name implies transform them in various ways such as moving, rotating or scaling the child. Transformations are written before the object they affect e.g.
Notice that there is no semicolon following transformation command. Transformations can be applied to a group of child nodes by using '{' and '}' to enclose the subtree e.g.
Cascading transformations are used to apply a variety of transforms to a final child. Cascading is achieved by nesting statements e.g.
Combining transformations is a sequential process, going from right to left. Consider the following two transformations:
While these contain the same operations, the first rotates a cube around the origin and then moves it by the offset specified for the translate, before finally coloring it red. By contrast, the second sequence first moves a cube, and then rotates it around the origin, before coloring it green. In this case the rotation causes the cube to move along an arc centered at the origin. The radius of this arc is the distance from the origin, which was set by the preceding translation. The different ordering of the rotate and translate transformations causes the cubes to end up in different places.

### Advanced concept

As OpenSCAD uses different libraries to implement capabilities this can introduce some inconsistencies to the F5 preview behaviour of transformations. Traditional transforms (translate, rotate, scale, mirror & multimatrix) are performed using OpenGL in preview, while other more advanced transforms, such as resize, perform a CGAL operation, behaving like a CSG operation affecting the underlying object, not just transforming it. In particular this can affect the display of modifier characters, specifically "#" and "%", where the highlight may not display intuitively, such as highlighting the pre-resized object, but highlighting the post-scaled object.

### scale

Scales its child elements using the specified vector. The argument name is optional.

### resize

Modifies the size of the child object to match the given x,y, and z.
resize is a CGAL operation, and like others such as render operates with full geometry, so even in preview this takes time to process.
Usage Example:
If x,y, or z is 0 then that dimension is left as-is.
If the 'auto' parameter is set to true, it auto-scales any 0-dimensions to match. For example.
The 'auto' parameter can also be used if you only wish to auto-scale a single dimension, and leave the other as-is.

### rotate

Rotates its child 'a' degrees about the axis of the coordinate system or around an arbitrary axis. The argument names are optional if the arguments are given in the same order as specified.
The 'a' argument (deg_a) can be an array, as expressed in the later usage above; when deg_a is an array, the 'v' argument is ignored. Where 'a' specifies multiple axes then the rotation is applied in the following order: x then y then z. That means the code:
is equivalent to:
For example, to flip an object upside-down, you can rotate your object 180 degrees around the 'y' axis.
This is frequently simplified to
The optional argument 'v' is a vector that determines an arbitrary axis about which the object is rotated.
When specifying a single axis the 'v' argument allows you to specify which axis is the basis for rotation. For example, the equivalent to the above, to rotate just around y
When specifying a single axis, 'v' is a vector defining an arbitrary axis for rotation; this is different from the multiple axis above. For example, rotate your object 45 degrees around the axis defined by the vector [1,1,0],
Rotate with a single scalar argument rotates around the Z axis. This is useful in 2D contexts where that is the only axis for rotation. For example:

##### Rotation rule help

For the case of:
"a" is a rotation about the X axis, from the +Y axis, toward the +Z axis.
"b" is a rotation about the Y axis, from the +Z axis, toward the +X axis.
"c" is a rotation about the Z axis, from the +X axis, toward the +Y axis.
These are all cases of the Right Hand Rule. Point your right thumb along the positive axis, your fingers show the direction of rotation.
Thus if "a" is fixed to zero, and "b" and "c" are manipulated appropriately, this is the spherical coordinate system.
So, to construct a cylinder from the origin to some other point (x,y,z):

### translate

Translates (moves) its child elements along the specified vector. The argument name is optional.

### mirror

Transforms the child element to a mirror of the original, as if it were the mirror image seen through a plane intersecting the origin. The argument to mirror is the normal vector of the origin-intersecting mirror plane used, meaning the vector coming perpendicularly out of the plane. Each coordinate of the original object is altered such that it becomes equidistant on the other side of this plane from the closest point on the plane. For example, mirror([1,0,0]), corresponding to a normal vector pointing in the x-axis direction, produces an object such that all positive x coordinates become negative x coordinates, and all negative x coordinates become positive x coordinates.

#### Examples

The original is on the right side. Note that mirror doesn't make a copy. Like rotate and scale, it changes the object.
* hand; // original mirror([1,0,0]) hand;
* hand; // original mirror([1,1,0]) hand;
* hand; // original mirror([1,1,1]) hand;

### multmatrix

Multiplies the geometry of all child elements with the given affine transformation matrix, where the matrix is 4×3 - a vector of 3 row vectors with 4 elements each, or a 4×4 matrix with the 4th row always forced to [0,0,0,1].
Usage: multmatrix(m = [...]) { ... }
This is a breakdown of what you can do with the independent elements in the matrix (for the first three rows):
| Scale X | Shear X along Y | Shear X along Z | Translate X |
| Shear Y along X | Scale Y | Shear Y along Z | Translate Y |
| Shear Z along X | Shear Z along Y | Scale Z | Translate Z |
The fourth row is forced to [0,0,0,1] and can be omitted unless you are combining matrices before passing to multmatrix, as it is not processed in OpenSCAD. Each matrix operates on the points of the given geometry as if each vertex is a 4 element vector consisting of a 3D vector with an implicit 1 as its 4th element, such as v=[x, y, z, 1]. The role of the implicit fourth row of m is to preserve the implicit 1 in the 4th element of the vectors, permitting the translations to work. The operation of multmatrix therefore performs m*v for each vertex v. Any elements (other than the 4th row) not specified in m are treated as zeros.
This example rotates by 45 degrees in the XY plane and translates by [10,20,30], i.e. the same as translate([10,20,30]) rotate([0,0,45]) would do.
The following example demonstrates combining affine transformation matrices by matrix multiplication, producing in the final version a transformation equivalent to rotate([0, -35, 0]) translate([40, 0, 0]) Obj;. Note that the signs on the sin function appear to be in a different order than the above example, because the positive one must be ordered as x into y, y into z, z into x for the rotation angles to correspond to rotation about the other axis in a right-handed coordinate system.
This example skews a model, which is not possible with the other transformations.
This example shows how a vector is transformed with a multmatrix vector, like this all points in a point array (polygon) can be transformed sequentially. Vector (v) is transformed with a rotation matrix (m), resulting in a new vector (vtrans) which is now rotated and is moving the cube along a circular path radius=v around the z axis without rotating the cube.

#### More?

Learn more about it here:
* Affine Transformations on wikipedia
* http://www.senocular.com/flash/tutorials/transformmatrix/

### color

Displays the child elements using the specified RGB color + alpha value. This is only used for the F5 preview as CGAL and STL (F6) do not currently support color. The alpha value defaults to 1.0 (opaque) if not specified.

#### Function signature:

Note that the r, g, b, a values are limited to floating point values in the range [0,1] rather than the more traditional integers { 0 ... 255 }. However, nothing prevents you from using R, G, B values from {0 ... 255} with appropriate scaling: color([ R/255, G/255, B/255 ]) { ... }
[Note: Requires version 2011.12] Colors can also be defined by name (case insensitive). For example, to create a red sphere, you can write color("red") sphere(5);. Alpha is specified as an extra parameter for named colors: color("Blue",0.5) cube(5);
[Note: Requires version 2019.05] Hex values can be given in 4 formats, #rgb, #rgba, #rrggbb and #rrggbbaa. If the alpha value is given in both the hex value and as separate alpha parameter, the alpha parameter takes precedence.
Warning: alpha processing (transparency) is order-sensitive. Transparent objects must be listed after non-transparent objects to display them correctly. Some combinations involving multiple transparent objects cannot be handled correctly. See issue #1390.
The available color names are taken from the World Wide Web consortium's SVG color list. A chart of the color names is as follows, (note that both spellings of grey/gray including slategrey/slategray etc are valid):
* Purples
* Lavender
* Thistle
* Plum
* Violet
* Orchid
* Fuchsia
* Magenta
* MediumOrchid
* MediumPurple
* BlueViolet
* DarkViolet
* DarkOrchid
* DarkMagenta
* Purple
* Indigo
* DarkSlateBlue
* SlateBlue
* MediumSlateBlue
* Reds
* IndianRed
* LightCoral
* Salmon
* DarkSalmon
* LightSalmon
* Red
* Crimson
* FireBrick
* DarkRed
* Blues
* Aqua
* Cyan
* LightCyan
* PaleTurquoise
* Aquamarine
* Turquoise
* MediumTurquoise
* DarkTurquoise
* CadetBlue
* SteelBlue
* LightSteelBlue
* PowderBlue
* LightBlue
* SkyBlue
* LightSkyBlue
* DeepSkyBlue
* DodgerBlue
* CornflowerBlue
* RoyalBlue
* Blue
* MediumBlue
* DarkBlue
* Navy
* MidnightBlue
* Pinks
* Pink
* LightPink
* HotPink
* DeepPink
* MediumVioletRed
* PaleVioletRed
* Greens
* GreenYellow
* Chartreuse
* LawnGreen
* Lime
* LimeGreen
* PaleGreen
* LightGreen
* MediumSpringGreen
* SpringGreen
* MediumSeaGreen
* SeaGreen
* ForestGreen
* Green
* DarkGreen
* YellowGreen
* OliveDrab
* Olive
* DarkOliveGreen
* MediumAquamarine
* DarkSeaGreen
* LightSeaGreen
* DarkCyan
* Teal
* Oranges
* LightSalmon
* Coral
* Tomato
* OrangeRed
* DarkOrange
* Orange
* Yellows
* Gold
* Yellow
* LightYellow
* LemonChiffon
* LightGoldenrodYellow
* PapayaWhip
* Moccasin
* PeachPuff
* PaleGoldenrod
* Khaki
* DarkKhaki
* Browns
* Cornsilk
* BlanchedAlmond
* Bisque
* NavajoWhite
* Wheat
* BurlyWood
* Tan
* RosyBrown
* SandyBrown
* Goldenrod
* DarkGoldenrod
* Peru
* Chocolate
* SaddleBrown
* Sienna
* Brown
* Maroon
* Whites
* White
* Snow
* Honeydew
* MintCream
* Azure
* AliceBlue
* GhostWhite
* WhiteSmoke
* Seashell
* Beige
* OldLace
* FloralWhite
* Ivory
* AntiqueWhite
* Linen
* LavenderBlush
* MistyRose
* Grays
* Gainsboro
* LightGrey
* Silver
* DarkGray
* Gray
* DimGray
* LightSlateGray
* SlateGray
* DarkSlateGray
* Black

#### Example

Here's a code fragment that draws a wavy multicolor object
↗ Being that -1<=sin(x)<=1 then 0<=(1/2 + sin(x)/2)<=1 , allowing for the RGB components assigned to color to remain within the [0,1] interval.
Chart based on "Web Colors" from Wikipedia

#### Example 2

In cases where you want to optionally set a color based on a parameter you can use the following trick:
Setting the colorname to undef keeps the default colors.

### offset

[Note: Requires version 2015.03]
Offset generates a new 2d interior or exterior outline from an existing outline. There are two modes of operation: radial and delta.
* The radial method creates a new outline as if a circle of some radius is rotated around the exterior (r > 0) or interior (r < 0) of the original outline.
* The delta method creates a new outline with sides having a fixed distance outward (delta > 0) or inward (delta < 0) from the original outline.
The construction methods produce an outline that is either inside or outside of the original outline. For outlines using delta, when the outline goes around a corner, it can be given an optional chamfer.
Offset is useful for making thin walls by subtracting a negative-offset construction from the original, or the original from a positive offset construction.
Offset can be used to simulate some common solid modeling operations:
* Fillet: offset(r=-3) offset(delta=+3) rounds all inside (concave) corners, and leaves flat walls unchanged. However, holes less than 2*r in diameter vanish.
* Round: offset(r=+3) offset(delta=-3) rounds all outside (convex) corners, and leaves flat walls unchanged. However, walls less than 2*r thick vanish.

#### Parameters

The first parameter may be passed without a name, in which case it is treated as the r parameter below. All other parameters must be named if used.
r or delta
    Number. Amount to offset the polygon. When negative, the polygon is offset inward.
   * r (default parameter if not named) specifies the radius of the circle that is rotated about the outline, either inside or outside. This mode produces rounded corners. The name may be omitted; that is, offset(c) is equivalent to offset(r=c).
   * delta specifies the distance of the new outline from the original outline, and therefore reproduces angled corners. No inward perimeter is generated in places where the perimeter would cross itself.

#### chamfer

    Boolean. (default false) When using the delta parameter, this flag defines if edges should be chamfered (cut off with a straight line) or not (extended to their intersection). This parameter has no effect on radial offsets.
$fa, $fs, and $fn
    The circle resolution special variables may be used to control the smoothness or facet size of curves generated by radial offsets. They have no effect on delta offsets.
Examples

### fill

[Note: Requires version Development snapshot]
Fill removes holes from polygons without changing the outline. For convex polygons the result is identical to hull.
Examples

### minkowski

Displays the minkowski sum of child nodes.
Usage example:
Say you have a flat box, and you want a rounded edge. There are multiple ways to do this (for example, see hull below), but minkowski is elegant. Take your box, and a cylinder:
Then, do a minkowski sum of them (note that the outer dimensions of the box are now 10+2+2 = 14 units by 14 units by 2 units high as the heights of the objects are summed):
NB: The origin of the second object is used for the addition. The following minkowski sums are different: the first expands the original cube by +1 in -x, +x, -y, +y from cylinder, expand 0.5 units in both -z, +z from cylinder. The second expands it by +1 in -x, +x, -y, +y and +z from cylinder, but expand 0 in the -z from cylinder.
Warning: for high values of $fn the minkowski sum may end up consuming lots of CPU and memory, since it has to combine every child node of each element with all the nodes of each other element. So if for example $fn=100 and you combine two cylinders, then it does not just perform 200 operations as with two independent cylinders, but 100*100 = 10000 operations.
Warning: if one of the inputs is compound, such as:
it may be treated as two separate inputs, resulting in an output which is too large, and has features between surfaces that should be unaltered with respect to one another. If so, use union.

### hull

Displays the convex hull of child nodes.
Usage example:
The Hull of 2D objects uses their projections (shadows) on the xy plane, and produces a result on the xy plane. Their Z-height is not used in the operation.
Referring to the illustration of a convex hull of two cylinders, it is computationally more efficient to use hull on two 2D circles and linear_extrude the resulting 2D shape into a 3D shape, rather than using hull on two cylinders, even though the resulting object appears identical. Complex geometries involving hull can be rendered faster by starting out in 2D, if possible.