Atlassian uses cookies to improve your browsing experience, perform analytics and research, and conduct advertising. Accept all cookies to indicate that you agree to our use of cookies on your device. Atlassian cookies and tracking notice, (opens new window)
Confluence
/
p5.js Vector Fields
Updated Sep 01, 2021

p5.js Vector Fields

Analytics

Loading data...
const gridWidth = 40; const gridHeight = 40; const screenWidth = 700; const screenHeight = 700; var distX; var distY; var reactorPosition; var vectLineList = []; function setup() { reactorPosition = createVector(0, 0); createCanvas(700, 700); background(255); stroke(0); let distX = width/gridWidth+1; let distY = height/gridHeight+1; for (var i = 0; i<gridWidth; i++ ) { for (var j = 0; j<gridHeight; j++ ) { vectLineList.push(new VectorLine(i*distX, j*distY)); } } } function draw() { background(255); for (var i = 0; i<vectLineList.length; i++ ) { vectLineList[i].show(reactorPosition); } } function mouseMoved() { reactorPosition.x = mouseX; reactorPosition.y = mouseY; } class VectorLine { constructor(x,y){ this._vector = createVector(0, 5); this._pos = createVector(x, y); this._vector.add(this._pos); this.reactorScaler = 0.07; } show(reactor) { this._reactor = reactor.copy(); this.reactorDistance = dist(this._reactor.x, this._reactor.y, this._pos.x, this._pos.y); this.scaler = this.reactorDistance * this.reactorScaler; this._reactor.sub(this._pos); // subtract VectorLines position from the reactors position, this effectively gives a reactor coordinate relative to our VectorLine coordinate this._vector = this._reactor.copy(); this._vector.normalize(); this._vector.rotate(PI/4); this._vector.mult(this.scaler); this._vector.add(this._pos); // line(this._pos.x, this._pos.y, this._vector.x, this._vector.y); ellipse(this._vector.x,this._vector.y, 5, 5); } }

 

Exercise


Use the vector code in the example to produce a flowing arrangement of geometric forms.

 

Example Solution using tear-drop form

 

 

const gridWidth = 20; const gridHeight = 20; const screenWidth = 700; const screenHeight = 700; var distX; var distY; var reactorPosition; var vectLineList = []; function setup() { reactorPosition = createVector(0, 0); createCanvas(700, 700); background(255); stroke(0); let distX = width/gridWidth+1; let distY = height/gridHeight+1; for (var i = 0; i<gridWidth; i++ ) { for (var j = 0; j<gridHeight; j++ ) { vectLineList.push(new VectorLine(i*distX, j*distY)); } } } function draw() { background(255); for (var i = 0; i<vectLineList.length; i++ ) { vectLineList[i].show(reactorPosition); } } function mouseMoved() { reactorPosition.x = mouseX; reactorPosition.y = mouseY; } class VectorLine { constructor(x,y){ this._vector = createVector(0, 5); this._pos = createVector(x, y); this._vector.add(this._pos); this.reactorScaler = 0.07; } show(reactor) { this._reactor = reactor.copy(); this.reactorDistance = dist(this._reactor.x, this._reactor.y, this._pos.x, this._pos.y); this.scaler = this.reactorDistance * this.reactorScaler; this._reactor.sub(this._pos); // subtract VectorLines position from the reactors position, this effectively gives a reactor coordinate relative to our VectorLine coordinate this._vector = this._reactor.copy(); this._vector.normalize(); this._vector.rotate(PI/4); this._vector.mult(this.scaler); this._vector.add(this._pos); // this._pointTwo = this._vector.copy(); this._pointTwo.rotate(PI/2); this._pointTwo.mult(8); this._pointTree = this._pointTwo.copy(); this._pointTree.rotate(PI/2); this._pointFour = this._pointTree.copy(); this._pointFour.rotate(PI/2); this._pointTwo.add(this._pos); this._pointTree.add(this._pos); this._pointFour.add(this._pos); this._vector.mult(this.scaler); this._vector.add(this._pos); noStroke(); fill(0); beginShape(); curveVertex(this._vector.x, this._vector.y); curveVertex(this._vector.x, this._vector.y); curveVertex(this._pointTwo.x, this._pointTwo.y); curveVertex(this._pointTree.x, this._pointTree.y); curveVertex(this._pointFour.x, this._pointFour.y); curveVertex(this._vector.x, this._vector.y); curveVertex(this._vector.x,this. _vector.y); endShape(); } }



For you

Programming
Results will update as you type.
  • Tiling and Repetition
  • Reactors
  • Programming Basics: Parametric and Generative Graphic Design 2016
  • Archive
  • High Scores
  • Artificial Neural Network
  • Alternatives to the Processing IDE
  • p5.js Programming
    • p5.js Introduction
    • p5.js Variables
    • p5.js Random Numbers
    • p5.js SVG + Images
    • p5.js WebGL
    • p5.js Classes and Objects
    • p5.js Events and Functions
    • p5.js Loops
    • p5.js Coordinates
    • P5js Nested Loops
    • p5.js Animation Exercise 1
    • p5.js Animation Exercise 2
    • p5.js Conditionals
    • p5.js Arrays and Lists
    • p5.js Simple Collision Detection
    • p5.js Reactors
      • p5.js Curve Reactor
      • p5.js Simple Reactor
      • p5.js Vector Fields
      • P5js Bitmap or SVG Permutations
    • p5.js Tiling and Repetition
    • p5.js Vectors
    • p5.js Animation Solution with Objects
    • p5.js Easing
    • p5.js Perlin Noise
    • p5.js Particle System
    • p5.js Sound
    • p5j.s Typography
    • P5js Archive
  • Programming in Processing (java)

{"serverDuration": 19, "requestCorrelationId": "6048aa39c9a24e67aebc71acd0bf8e06"}