Name

add()

Class

PVector

Description

Adds x, y, and z components to a vector, adds one vector to another, or adds two independent vectors together. The version of the method that adds two vectors together is a static method and returns a new PVector, the others act directly on the vector itself. See the examples for more context.

Examples

  • PVector v1, v2;
    
    void setup() {
      noLoop();
      v1 = new PVector(40, 20, 0);
      v2 = new PVector(25, 50, 0); 
    }
    
    void draw() {
      ellipse(v1.x, v1.y, 12, 12);
      ellipse(v2.x, v2.y, 12, 12);
      v2.add(v1);
      ellipse(v2.x, v2.y, 24, 24);
    }
    
  • PVector v;
    
    void setup() {
      noLoop();
      v = new PVector(40, 20, 0);
    }
    
    void draw() {
      ellipse(v.x, v.y, 12, 12);
      ellipse(25, 50, 12, 12);
      v.add(25, 50, 0);
      ellipse(v.x, v.y, 24, 24);
    }
    
  • PVector v1, v2;
    
    void setup() {
      noLoop();
      v1 = new PVector(40, 20, 0);
      v2 = new PVector(25, 50, 0); 
    }
    
    void draw() {
      ellipse(v1.x, v1.y, 12, 12);
      ellipse(v2.x, v2.y, 12, 12);
      PVector v3 = PVector.add(v1, v2);
      ellipse(v3.x, v3.y, 24, 24);
    }
    

Syntax

  • .add(v)
  • .add(x, y)
  • .add(x, y, z)
  • .add(v1, v2)
  • .add(v1, v2, target)

Parameters

  • v(PVector)the vector to be added
  • x(float)x component of the vector
  • y(float)y component of the vector
  • z(float)z component of the vector
  • v1(PVector)a vector
  • v2(PVector)another vector
  • target(PVector)the target vector (if null, a new vector will be created)

Return

  • PVector