Food&Code Food&Code Workshop

Vector Everywhere


Home About

Course by for Food&Code

1) A line, a triangle, a sphere… vectors

A vector is a way to represent information. As itself, it’s nothing, it’s just a modelisation, a choice, a representation. In short, it’s meant to simplify your life. Let’s see how it does it.

A scalar is a simple value. 1; 3.2; -54. A vector is a container of multiple values: (3,4); (-3, 1, 56, 0, 2). Usually it’s linked with space, and hence has either two values (x and y, for positioning on a screen for instance) or three values (x,z,y) for handling 3D graphics.

    var vec;
    
    function setup() {
        createCanvas(windowWidth-20, windowHeight-20);
        noStroke();
        vec = createVector(width/2, height/2);
    }

    function draw() {
        background(0,0,255);
        var vecAdd = createVector(random(-20,20), random(-20,20));
        vec.add(vecAdd);
      
        fill(255,255,255);
        rect(vec.x, vec.y, 40, 40); // Usage
    }

2) Natural Behavior

        var vecAdd = createVector(randomGaussian(0,3), randomGaussian(0,3));
    var vecP, vecS, vecA;
    
    function setup() {
      createCanvas(windowWidth-20, windowHeight-20);
      
      noStroke();
      fill(255,255,255);
      
      vecP = createVector(width/2, height/2);
      vecS = createVector(0,0);
      vecA = createVector(0,0);
    }

    function draw() {
      background(0);

      // We start with no forces
      var sumF = createVector(0,0);
      // We add a random force
      sumF.add( createVector(randomGaussian(0,3)/10,
                             randomGaussian(0,3)/10) );
            
      // Acceleration, we command you!
      vecA = sumF;
      // Set of rules, update
      vecS.add(vecA);
      vecP.add(vecS);

      
      // Usage, ta-dah!
      rect(vecP.x, vecP.y, 40, 40); // Usage
    }     
    // Attraction toward the center
    sumF.add( createVector( (width/2-vecP.x)/5000, (height/2-vecP.y)/5000) );
    var planet_A;

    planet_A = createVector(random(width/3, 2*width/3),
                              random(height/3, 2*height/3));

    // Planetary attraction
    sumF.add( createVector( (planet_A.x-vecP.x)/2000, (planet_A.y-vecP.y)/2000) );

    ellipse(planet_A.x, planet_A.x, 40, 40);

3) In search of simplicity

var part;
      
    function setup() {
      createCanvas(windowWidth-20, windowHeight-20);
        background(0);
      part = {};
      
      noStroke();
      fill(255,255,255, 20); 
      planet_A = createVector(random(width/3, 2*width/3),
                              random(height/3, 2*height/3));
            part.p = createVector(width/4, height/4);
      part.s = createVector(4,0);
      part.a = createVector(0,0);
    }

    function draw() {
     
      // Definition of the forces
      var sumF = createVector(0,0);
      sumF.add( createVector(randomGaussian(0,3)/30,
                             randomGaussian(0,3)/30) );
            sumF.add( createVector( (width/2-part.p.x)/5000, (height/2-part.p.y)/5000) );
            
            
      // Acceleration, we command you!
      part.a = sumF;
      // Set of rules, update
      part.s.add(part.a);
            part.p.add(part.s);
      
      // Usage, ta-dah!
      rect(part.p.x, part.p.y, 40, 40); // Usage
      
    }     

4) In search of multiplicity