Transitioning from scalar to vector

Hello,

I’m trying to rewrite my programs & classes to use vectors.
vectors themselves are easy, but going about the operations in p5.js has me getting lost in the syntax.

As a simple example I make 2 vectors to create a line :

v1 = createVector(x,y)
v2 = createVector(u,v)

line(x,y,u,v);

how would I rewrite this math?

u = y+x*2;
v = x-y*4;

I feel this isn’t the most efficient way to do this, but I’m only interested in the math operations without using mag or cross.

Thanks.

Given you wanna assign to the 2nd p5.Vector v2 the values of the 1st p5.Vector v1 you could simply use the method p5.Vector::set() for it:

const
  { x, y } = v1,
  u = y + x*2,
  v = x - y*4;

v2.set(u, v);

Thanks @GoToLoop .

Okay, I see the relationship here, And this helps, but I’m trying to understand working with vectors using

add(), sub(), mult() , when compared to simple scalar formulas.

I can see that using these methods would be inefficient, and maybe overly complex, so I’m wondering if it is possible.

Hello,

Some references for you to peruse:

https://natureofcode.com/book/chapter-1-vectors/

:)

1 Like