Skip to main content

  | मौसम…

411 समाचार
Public archive edition · Attributed source reporting with original dates · AI drafts are excluded

411 REFERENCE · Wikibooks

OpenSCAD User Manual/Vectors

Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.

part of the section on Values, Data Types, and Constants Vectors A vector or list is a sequence of zero or more OpenSCAD values. Vectors are collections of numeric or boolean values, variables, vectors, strings or any combination thereof. They can also be expressions that evaluate to one of these. Vectors handle the role of arrays found in many imperative languages. The information here also applies to lists and tables that use vectors for their data. A vector has square brackets, [] enclosing zero or more items (elements or members), separated by commas. A vector can contain vectors, which can contain vectors, etc. Examples [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 Vectors 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], [6,7,8,9] ] note increased nesting depth Vectors can be initialized using a for loop enclosed in square brackets. The following example initializes the vector result with a length n of 10 values to the value of a.n = 10; a = 0; result = [ for (i=[0:n-1]) a ]; echo(result); //ECHO: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]The following example shows a vector result with a n length of 10 initialized with values that are alternatively a or b respectively if the index position i is an even or an odd number.n = 10; a = 0; b = 1; result = [ for (i=[0:n-1]) (i % 2 == 0) ? a : b ]; echo(result); //ECHO: [0, 1, 0, 1, 0, 1, 0, 1, 0, 1] Indexing elements within vectors Elements 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 example elements with lengths from len() 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; s[a] undef 5 e[s[a]] 3 [ [10,11], [12,13,14], [[15,16],[17]] ] String indexing The elements (characters) of a string can be accessed: "string"[2] //resolves to "r" Vector Swizzling Dev Snapshot 2021.12.23 or later The components of vectors can be accessed using the following syntax: e = [1, 2, 3, 4]; v = e.x + e.y; // yields 1 + 2 = 3 This is called swizzling. You can use x, y, z, or w, referring to the first, second, third, and fourth components, respectively. Alternatively, r, g, b, and a can be used instead of x,y,z,w. Swizzling is named so because you can use it to repeat, reorder, or select a subset of components. v1 = e.xyxx; // -> [1,2,1,1] v2 = e.zyy; // -> [3,2,2] v3 = e.rrr; // -> [1,1,1] v4 = e.rg; // -> [1,2] You can use any combination of up to 4 of the letters to create a vector. Attempts to reference components which doesn't exist will result in an undef. Vector Functions concat() Function 2015.03 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" To change the nesting level, see OpenSCAD User Manual/List Comprehensions#Flattening a nested vector len() Function len() is a function that returns the length of vectors or strings. Indices of elements are from [0] to [length-1]. vector Returns the number of elements at this level. Single values, which are not vectors, raise an error. string Returns the number of characters in a string. a = [1,2,3]; echo(len(a)); // 3 See example elements with lengths Matrix A matrix is a vector of vectors. Example that defines a 2D rotation matrix mr = [ [cos(angle), -sin(angle)], [sin(angle), cos(angle)] ];