go back

vecn [source code]


api

#define vec(n) [source code]

macro to define a new vector with n dimensions.

vec(3) position_with_3_dimensions;

#define vecn(x) [source code]

get number of dimensions from vector x.

vec(3) position_with_3_dimensions;
vecn(position_with_3_dimensions); // -> 3

#define vecn2(name, ...) [source code]

create a variable with name using the parameters as dimensions. IMPORTANT! make sure to use decimal numbers or the macro won't work (for instance, don't pass 2 as a value, pass 2. or 2.0f)

vecn2(foo, 2.0f, 3.0f); // -> foo is 2.0f, 3.0f
vecn2(bar, 5.0f, 3.0f, 2.0f); // -> bar has three dimensions.

#define vfrom(dest, src) [source code]

create a vector dest from src.

vecn2(foo, 2.0f, 3.0f);
vfrom(bar, foo); // -> bar = foo

#define vcpy(a, b) [source code]

copy vector b to vector a.

vecn2(foo, 2.0f, 3.0f);
vecn2(bar, 3.0f, 4.0f, 5.0f);
vcpy(bar, foo); // bar will be 2.0f, 3.0f, 5.0f

#define vadd(a, b) [source code]

add vector b to a.

vecn2(foo, 2.0f, 4.0f);
vecn2(bar, 1.0f);
vadd(bar, foo); // bar will be 3.0f

#define vsub(a, b) [source code]

subtract vector b from a.

vecn2(foo, 2.0f, 4.0f);
vecn2(bar, 5.0f);
vsub(bar, foo); // bar will be -3.0f

#define vscale(a, b) [source code]

scale vector a by b.

vecn2(foo, 2.0f, 4.0f);
vscale(foo, 2.0f); // foo will be 4.0f, 8.0f

#define vlen2(a) [source code]

get length ^ 2 of vector a.

// ...

#define vlen(a) [source code]

get length of vector a.

// ...

#define vnorm(a) [source code]

normalize vector a.

// ...

#define vdist2(a, b) [source code]

get distance ^ 2 between a and b.

// ...

#define vdist(a, b) [source code]

get distance between vector a and b.

// ...

#define vdot(a, b) [source code]

dot product between vector a and b.

// ...

#define vdir2(a, b) [source code]

calculate direction (normalized) between a and b and store the result in a.

// ...

#define vlerp(a, b, t) [source code]

linear interpolation from vector a to vector b using t as weight. t being a normalized ([0, 1]) value.

// ...

#define vdebug(a) [source code]

print vector a to stderr. useful for debug purposes.

vecn2(position, 3.0f, 2.0f);
vdebug(position); // prints "position -> (3.0f, 2.0f)\n" 

#define vset(x, ...) [source code]

set vector x with supplied parameters.

vec(2) position;
vset(position, 2.0f, 3.0f); // position will be 2.0f, 3.0f
vset(position, 2.0f, 3.0f, 4.0f); // safe, parameters exceeding the dimensions will be ignored.
vset(position, 5.0f); // position will be 5.0f, 3.0f