vec4 - documentation?

I could not help but notice that there is an option for vec4 but ironically enough theres no documentation for how its used?
With vec2 its just 2 coordinates, vec3 is 3 coordinates, but with vec4 why would we want to enter 4 coordinates?

Translations in three dimensional space can be represented using 4x4 matrices, which then operate on vectors with four dimensions (the fourth coordinate is set to 1). Perhaps that is why vec4 exists? Or it may be that colours have four dimensions (red, blue, green, alpha)?

It is for the transformation matrix as mpilgrem surmises.

it can also be a

I’ve been using it as the object for when I need to define colors outside of the normal fill() or stroke() functions. Just define something=vec4(x,y,z,a) and then fill(something)

What is the letter I’d of the fourth value? I’ve wondered is myself but never needed to use it…

Scratch that. It’s w.

Hi All,

I’m not familiar with vectors and am still learning here. My limited understanding is that vectors are small table/ arrays which can undergo specific matrix maths/transitions. So I suppose you can use them for whatever use you identify. Is that the case?



Absolutely. A vector is simply a finite ordered list of numbers. The indexing set simply needs an ordering. For an arbitrary vector, it’s usual to choose 0,1,…,n-1 but Lua (following mathematics) uses 1,2,…,n. Short vectors often have “special” labelling sets such as x,y or x,y,z or x,y,z,t (but here it seems to be a w) but after that, it gets confusing so this isn’t extended.

The main use of 4-vectors here is as a quick way to encode affine transformations on 3-space. A 3x3 matrix can only encode linear transformations which doesn’t include all the possibilities desired by Codea - in particular, translations are not encodeable by a 3x3 matrix. By using a sneaky trick of embedding 3-space in 4-space as the plane with 4th coordinate 1, it is possible to get translations also using 4x4 matrices.

Most of the time you don’t need to worry about this. Things like translate, scale, and rotate do all the maths for you. But if you ever want to manipulate the transformation matrix yourself, you’ll need to understand how the 4x4 matrix is built up. There’s some old posts where I was trying to work this out. I’ll see if I can dig them up and link to them.

But as Bri_G says, this is one use for 4-vectors but it doesn’t need to be the use. Other uses are possible.

(Incidentally, @Deamos, why not just use something = color(r,g,b,c) fill(something)?)

You know, thats probably a good idea. I could have sworn I had tried that route and it didn’t work, but i’ll go back and check.

I added a page documenting the vec4() function to the wiki. (Update) I’ve added something on v1:cross(v2), in the light of the vec4.c file in the Codea Runtime Library. (Further update): I’ve also added a page to the wiki documenting the undocumented features of the color() function.