vec [source code]
- these functions don't allocate memory.
- these functions use float for each dimension. the number of dimensions are 4: x, y, z, w. even though this may be wasteful, it's a tradeoff i'm willing to do.
- these functions are provided as is, use it at your own risk.
api
union vec; [source code]
typedef union vec {
struct { float x, y, z, w; };
struct { float r, g, b, a; };
} vec;
vec vadd(vec a, vec b); [source code]
add vectors a and b.
vec a = {2, 3};
vec b = {3, 4};
vadd(a, b) // -> {5.0f, 7.0f, 0, 0}
vec vsub(vec a, vec b); [source code]
subtract vectors a and b.
vec a = {2, 4};
vec b = {1, 2};
vsub(a, b) // -> {1.0f, 2.0f, 0, 0}
vec vscale(vec a, float x); [source code]
scale vector a by x.
vec a = {2, 4};
vscale(a, 3) // -> {6.0f, 12.0f, 0, 0}
float vlen2(vec src); [source code]
get length of src ^ 2. useful to avoid using sqrtf when not needed.
vec a = {2, 3};
vlen2(a) // -> 13.0f
float vlen(vec src); [source code]
get length of src. note: uses sqrtf.
vec a = {2, 3};
vlen(a) // -> 3.60f
vec vnorm(vec src); [source code]
get normalized vector from src.
vec a = {2, 3};
vnorm(a) // -> {0.55f, 0.83f, 0, 0}
float vdist2(vec src, vec target); [source code]
calculate distance ^ 2 between src and target.
vec a = {2, 3};
vec b = {6, 9};
vdist2(a, b) // -> 52.0f
float vdist(vec src, vec target); [source code]
calculate distance between src and target.
vec a = {2, 3};
vec b = {6, 9};
vdist2(a, b) // -> 7.21f
float vdot(vec a, vec b); [source code]
calculate dot product from a and b.
vec a = {2, 3};
vec b = {6, 9};
vdot(a, b) // ->
vec vcross(vec a, vec b); [source code]
calculate cross product from a and b.
vec a = {2, 3};
vec b = {6, 9};
vcross(a, b) // ->
vec vdir2(vec src, vec target); [source code]
calculate direction from src to target. returns normalized vector.
vec a = {2, 3};
vec b = {6, 9};
vdir2(a, b) // ->
float vangle2(vec src, vec target); [source code]
calculate angle in radians from src to target. to convert from radians to degrees use radians * 180.0f / pi
vec a = {2, 3};
vec b = {6, 9};
vangle2(a, b) // ->
vec vlerp(vec src, vec to, float t); [source code]
calculate linear interpolation between src and to using t as weight. t being a normalized value ([0, 1])
vec a = {2, 3};
vec b = {6, 9};
vlerp(a, b, 0.5f) // ->