#define new_vec2(x, y) list(x, y)
#define vec2_add(u, v) new_vec2(u+v, u+v)
#define vec2_sub(u, v) new_vec2(u-v, u+v)
#define vec2_mag(u) sqrt(u*u + u*u)
#define vec2_scale(u, s) new_vec2(u*s, u*s)
#define vec2_unit(u) vec2_scale(u, 1/vec2_mag(u))
The problem I have with the above is that it's creating a new /list for every operation. Would it be better to instead have them calculate on vector u itself, instead of returning a (new) resultant vector?
// or this:
// (operations are made on the vector itself instead of making a new /vector datum)
New() comp = args
for(var/d in max(comp.len, v.comp.len))
comp[d] += v.comp[d]
for(var/d in comp.len)
comp[d] *= s
With the first method, you can do multiple operations in just one line. For example, I have a gravity simulator. This is as much as I can remember off the top of my head, but I think it's a good example.
// src and m are objects with mass
// pos (position) is a (vector, /list) coordinate
var d = vec2_sub(m.pos, pos) // displacement vector
var r = vec2_mag(d) // distance between them
vel = vec2_add(vel, vec2_scale(d, grav_const * m.mass / r / r / r))
This makes a total of 3 /lists just to change one vector. One as a result of vec2_sub, another as a result of vec2_scale, and the last as a result of vec2_add. vel is a whole new /list in the end.
To do this with the second method:
var vector/d = new(m.pos.comp.Copy())
var r = d.mag()
d.scale(grav_const * m.mass / r / r / r)
This makes only one vector, d for displacement, which is modified and eventually added to vel.
Which way is best? Is there even a significant difference? Can we have native vector support?