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
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)

/
p5.js Curve Reactor
Updated Sep 23, 2021

p5.js Curve Reactor

Analytics

Loading data...

This example uses a curve as the reactor object. The curve is subdivided into several points, and those points used in the similar way as the previous reactor examples.

var NDIVS = 100; // # of subdivisions, higher is more accurate, but slower, and vice versa var cps = []; // curve controll points var gridWidth = 30; var gridHeight = 30; var shapeWidth = 19; var shapeHeight = 19; var reactorScaler = 0.006; function setup() { createCanvas(500,500); smooth(); for (var i=0; i<4; i++) { cps[i] = createVector(0,0); } cps[0].x = width+300; cps[0].y = -500; cps[1].x= 50; cps[1].y= 50; cps[2].x= width-50; cps[2].y= height-50; cps[3].x= -500; cps[3].y= height+300; } function draw() { background(255); drawGeometry(); } function drawGeometry() { stroke(255,0,0); noFill(); curve(cps[0].x,cps[0].y, cps[1].x,cps[1].y, cps[2].x,cps[2].y, cps[3].x,cps[3].y); // uncomment if you want to manipulate curve (BUT BE CAREFUL, it's heavy!) /* for (let pt of cps) { ellipse(pt.x, pt.y, 20, 20); } if (mouseIsPressed) { for (let pt of cps) { if (dist(mouseX, mouseY, pt.x, pt.y) < 20) { pt.x = mouseX; pt.y = mouseY; break; } } } */ fill(0); noStroke(); for(var i = 0; i<gridWidth; i++ ) { for(var j = 0; j<gridHeight; j++ ) { var myPos = createVector(i*shapeWidth, j*shapeHeight); var cpt = ClosestPointOnCatmullRom(cps,myPos,NDIVS); let reactorDistance = dist(cpt.x, cpt.y, myPos.x, myPos.y); let scaler = reactorDistance*reactorScaler; ellipse(myPos.x, myPos.y, shapeWidth*scaler, shapeHeight*scaler); } } } // see http://davebollinger.org/category/code/ /** * Returns the closest point on a catmull-rom curve relative to a search location. * This is only an approximation, by subdividing the curve a given number of times. * More subdivisions gives a better approximation but takes longer, and vice versa. * No concern is given to handling multiple equidistant points on the curve - the * first encountered equidistant point on the subdivided curve is returned. * * @param cps array of four PVectors that define the control points of the curve * @param pt the search-from location * @param ndivs how many segments to subdivide the curve into * @returns PVector containing closest subdivided point on curve */ function ClosestPointOnCatmullRom(cps,pt,steps) { var result = createVector(0,0); var bestDistanceSquared = 0; var bestT = 0; for (var i=0; i<=steps; i++) { let t = (float)(i) / (float)(steps); let x = curvePoint(cps[0].x,cps[1].x,cps[2].x,cps[3].x,t); let y = curvePoint(cps[0].y,cps[1].y,cps[2].y,cps[3].y,t); let dissq = dist(pt.x, pt.y, x, y); if (i==0 || dissq < bestDistanceSquared) { bestDistanceSquared = dissq; bestT = t; result.set(x,y,0); } } return result; }



Related content

p5.js Simple Reactor
p5.js Simple Reactor
Programming
Read with this
p5.js Reactors
p5.js Reactors
Programming
Read with this
p5.js Programming
p5.js Programming
Programming
Read with this
p5.js Introduction
p5.js Introduction
Programming
Read with this
p5.js Variables
p5.js Variables
Programming
Read with this
p5.js Events and Functions
p5.js Events and Functions
Programming
Read with this
{"serverDuration": 31, "requestCorrelationId": "3d9a2dcd83a3486693f9c1d628b098a5"}