Before vectors: 5. Oversimplified Strings Part 1 โ€” The Basics ๐ŸŸข

<aside> โš ๏ธ

Here, we are looking at vectors from the perspective of variables and data types, not as a mathematical concept.

</aside>

TL:DR

Operands What happens Example Result
Vector ร— Vector Component-by-component operation {1,2,3} * {4,5,6} {4,10,18}
Vector ร— Vector2 vector2 is promoted to vector with 0 {1,2,3} * {4,5} โ†’ {4,5,0} {4,10,0}
Vector ร— Float Float is applied to every component {1,2,3} * 2 {2,4,6}

Fundamentals:

A vector is a value made of multiple numbers that belong together.

vector direction = {1, 0, 0};

Here, direction contains three values:

x = 1
y = 0
z = 0

oversiplified_vectors_datatypes_gerardojustel_re1mon.png

Vector Operations

Vector by Vector

<aside>

A vector is applied component by component.

</aside>

// Create two vector variables
vector v1 = set(1, 2, 3);
vector v2 = set(4, 5, 6);

// Add the vectors component by component
vector vadd = v1 + v2;

// Subtract the vectors component by component
vector vsub = v1 - v2;

// Multiply the vectors component by component
vector vmul = v1 * v2;

// Divide the vectors component by component
vector vdiv = v1 / v2;

The operations are performed component by component:

v1 + v2 โ†’ {1+4, 2+5, 3+6} โ†’ {5, 7, 9}
v1 - v2 โ†’ {1-4, 2-5, 3-6} โ†’ {-3, -3, -3}
v1 * v2 โ†’ {1ร—4, 2ร—5, 3ร—6} โ†’ {4, 10, 18}
v1 / v2 โ†’ {1รท4, 2รท5, 3รท6} โ†’ {0.25, 0.4, 0.5}

Then your attribute output:

// Store the results as vector attributes
v@vadd = vadd;
v@vsub = vsub;
v@vmul = vmul;
v@vdiv = vdiv;

Vector by Float

<aside>

A float is applied to every component of the vector.

</aside>

// Create a vector variable
vector base = set(1, 2, 3);

// Create a float variable
float f = 2;

// Add the float to each component of the vector
vector vadd = base + f;
v@vadd = vadd;

// Multiply each component of the vector by the float
vector vmul = base * f;
v@vmul = vmul;

The float is applied to every component

base + f
{1, 2, 3} + 2
โ†’ {1+2, 2+2, 3+2}
โ†’ {3, 4, 5}

and

base * f
{1, 2, 3} ร— 2
โ†’ {1ร—2, 2ร—2, 3ร—2}
โ†’ {2, 4, 6}

Vector by Vector 2

<aside>

When operating on vectors of different sizes, VEX promotes the smaller vector to match the larger one.

</aside>

// Create a vector3 variable
vector base = set(1, 2, 3);

// Create a vector2 variable
vector2 u = set(4, 5);

// Store the vectors as attributes
v@base = base;
v@u = u;

// Add the vector2 to the vector3
// The vector2 is promoted to a vector3 by adding 0 as the third component
vector result_add = base + u;
v@result_add = result_add;

// Multiply the vector3 and vector2 component by component
// The vector2 is promoted to a vector3 by adding 0 as the third component
vector result_mult = base * u;
v@result_mult = result_mult;
base = {1, 2, 3}
u    = {4, 5}

base + u = {5, 7, 3}
base * u = {4, 10, 0}

The interesting part is the multiplication:

{1, 2, 3}
ร—
{4, 5, 0}
โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
{4, 10, 0}

<aside> โš ๏ธ

The missing component becomes 0, so multiplication can actually wipe out a component.

</aside>

Continue reading:

8. Oversimplified Arrays โ€” The Lineup ๐ŸŸก