1 Vectors
John Horigan edited this page 2015-03-18 22:52:41 -07:00

Vectors are tuples of floating point numbers that can have a size between 2 and 99.

Creating Vectors

Vectors are created using the vector binding operator , (the comma). For example

v9 = 1, 2, 3, 4, 5, 6, 7, 8, 9

Creates a 9-tuple vector of the numbers 1 through 9.

Using Vectors

As Parameters

Vectors can be used as shape or user function parameters if you use vectorn as the type specifier, where n is a number between 2 to 99 specifying the vector length.

shape circ(vector2 sz) {
  CIRCLE[s sz]
}

As Variables

There is no type specifier for variable declarations. If the expression on the right of the = is a vector then the variable is a vector, as the v9 example above shows.

In Shape Adjustments

Some shape adjustments take two or three arguments (skew, size, x, time, transform, and targeted color changes). A vector2 or vector3 can be used in these cases. The circ shape example above shows a vector2 parameter being used for a size adjustment.

In Function Arguments

Some functions take two arguments. A single vector2 can be used in the place of the two arguments. This only works for built-in functions. User functions only accept vectors for arguments when they are explicitly declared in the function parameter list.

sz = 1, 5
 
rnd1(n1, n2) = rand(n1, n2)
rnd2(vector2 n12) = rand(n12)  // rand() accepts 0, 1, or 2 numbers or a vector2
 
foo1 = rnd1(sz)  // error, expects two numbers, not one vector2
foo2 = rnd2(sz)  // ok

As Function Return

None of the built-in functions currently return vectors, but user functions can if you declare the return type as vectorn.

vector2 rnd2(n1, n2) = rand(n1), rand(n2)
 
shape test {
  SQUARE [s rnd2(1, 5)]
}

Vector Access Syntax

Another way to use vectors is to extract numbers or subvectors from them and use those. The syntax for accessing a number in a vector is vectorname[index], where vectorname is the name of a variable or parameter that is a vector and index is an integer expression indicating which number in the vector to extract. Vectors are zero-indexed, the first element has index 0, the second has index 1, etc.

vector2 rnd2(vector2 n12) = rand(n12[0]), rand(n12[1])
 
sz = 1, 5
 
shape test {
  SQUARE [s rnd2(sz)]
}

Subvector Access

A smaller vector can be extracted from a vector using the subvector access syntax: vectorname[index, length] or vectorname[index, length, stride]. length is the length of the extracted vector and stride is the spacing between extracted numbers, which defaults to 1 if you don't specify it. For example, if v9 is a 3x3 matrix in row-major order then you could extract rows and columns like so:

v9 = 1, 2, 3, 
     4, 5, 6, 
     7, 8, 9
row1 = v9[3, 3]     // = 4, 5, 6
col1 = v9[1, 3, 3]  // = 2, 5, 8