User:LABoyd2/Vector stand alone page 150927
Vectors
editVectors in OpenSCAD are a collection (or list or table) of numeric values, boolean values, variables, vectors, strings or any combination thereof. They can also be expressions which evaluate to one of these. A vector has square brackets, [] enclosing zero or more items (elements or members), separated by commas. A vector can contain vectors, which contain vectors, etc. The information here also applies to lists and tables which use vectors for their data.
examples
edit[1,2,3] [a,5,b] [] [5.643] ["a","b","string"] [[1,r],[x,y,z,4,5]] [3, 5, [6,7], [[8,9],[10,[11,12],13], c, "string"] [4/3, 6*1.5, cos(60)]
use in OpenSCAD: cube( [width,depth,height] ) // optional spaces shown for clarity translate( [x,y,z] ) polygon( [ [x0,y0], [x1,y1], [x2,y2] ] )
creation
editVectors are created by writing the list of elements, separated by commas, and enclosed in square brackets. Variables are replaced by their values.
cube([10,15,20]); a1 = [1,2,3]; a2 = [4,5]; a3 = [6,7,8,9]; b = [a1,a2,a3]; // [ [1,2,3], [4,5], [5,7,8,9] ] note increased nesting depth
elements within vectors
editElements within vectors are numbered from 0 to n-1 where n is the length returned by len(). Address elements within vectors with the following notation:
e[5] // element no 5 (sixth) at 1st nesting level e[5][2] // element 2 of element 5 2nd nesting level e[5][2][0] // element 0 of 2 of 5 3rd nesting level e[5][2][0][1] // element 1 of 0 of 2 of 5 4th nesting level
e = [ [1], [], [3,4,5], "string", "x", [[10,11],[12,13,14],[[15,16],[17]]] ]; // length 6 address length element e[0] 1 [1] e[1] 0 [] e[5] 3 [ [10,11], [12,13,14], [[15,16],[17]] ] e[5][1] 3 [ 12, 13, 14 ] e[5][2] 2 [ [15,16], [17] ] e[5][2][0] 2 [ 15, 16 ] e[5][2][0][1] undef 16 e[3] 6 "string" e[3 ][2] 1 "r" s = [2,0,5]; a = 2; e[s[a]] 3 [ [10,11], [12,13,14], [[15,16],[17]] ]
Operators
editconcat
edit[Note: Requires version 2015.03 or later]
Concat() combines the elements of 2 or more vectors into a single vector. No change in nesting level is made.
vector1 = [1,2,3]; vector2 = [4]; vector3 = [5,6]; new_vector = concat(vector1, vector2, vector3) // [1,2,3,4,5,6] string_vector = concat("abc","def"); // ["abc", "def"] one_string = str(string_vector[0],string_vector[1]); // "abcdef"
len
editlen() is a function which return the length of vectors or strings.
- vector
- Returns the number of elements at this level.
- Indices of elements are from [0] to [length-1].
- Single values, which are not vectors, return undef.
- string
- Returns the number of characters in string.
Vector Math Operators
editThe vector-number operators take a vector and a number as operands and produce a new vector.
* | multiply all vector elements by number |
/ | divide all vector elements by number |
Vector Operators
editThe vector operators take vectors as operands and produce a new vector.
+ | add element-wise |
- | subtract element-wise |
The "-" can also be used as prefix operator to element-wise negate a vector.
Vector Dot-Product Operator
editThe vector dot-product operator takes two vectors as operands and produces a scalar.
* | sum of vector element products |
Matrix Multiplication
editMultiplying a matrix by a vector, vector by matrix and matrix by matrix
* | matrix/vector multiplication |
notes
edithttps://en.wikibooks.org/wiki/OpenSCAD_User_Manual/List_Comprehensions
Flatten a nested list
edit- input
- nested list
- output
- list with the outer level nesting removed
function flatten(l) = [ for (a = l) for (b = a) b ] ;
nested_list = [ [ 1, 2, 3 ], [ 4, 5, 6 ] ];
echo(flatten(nested_list)); // ECHO: [1, 2, 3, 4, 5, 6]
- from polygon.scad
a=[[0,0],[0,10],[10,0],[0,-10],[-10,0],[8,15],[15,8],[-8,15],[15,-8],[8,-15],[8,8],[8,-8]];
PlotPoints(a,"green");
module PlotPoints(PointList,Color){ color(Color){ for ( i = [0 : len(PointList)-1] ){ translate(PointList[i]){ text(str(i),size=2); cube(size=0.5); } } } }
// generate a list with all values defined by a range list1 = [ for (i = [0 : 2 : 10]) i ]; echo(list1); // ECHO: [0, 2, 4, 6, 8, 10]
list1 = [ for (i = [0 : 10]) [i*2,[i,i]] ]; echo(list1); // ECHO: ECHO: [[0, [0, 0]], [2, [1, 1]], [4, [2, 2]], [6, [3, 3]], [8, [4, 4]], [10, [5, 5]], [12, [6, 6]], [14, [7, 7]], [16, [8, 8]], [18, [9, 9]], [20, [10, 10]]]
// map input list to output list list = [ for (i = [2, 3, 5, 7, 11]) i * i ]; echo(list); // ECHO: [4, 9, 25, 49, 121]
// extract every second character of a string str = "SomeText"; list2 = [ for (i = [0 : 2 : len(str) - 1]) str[i] ]; echo(list2); // ECHO: ["S", "m", "T", "x"]
a = [[0,0],[1,1],[2,2],[3,3],[4,4]]; p = [4,0,3]; list2 = select(a,p); echo(list2); // ECHO: [[4, 4], [0, 0], [3, 3]]
function select(source_vector,select_vector)=
[ for (i = [0 : len(select_vector) - 1]) source_vector[select_vector[i]] ];
e = [ [1], [], [3,4,5], "string", "x", [[10,11],[12,13,14],[[15,16],[17]]] ]; // length 6
s= [2,0,5]; a = 2; echo( e[s[a]] );