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
/
(Extra) Curve Reactor

(Extra) Curve Reactor

Nov 07, 2017

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.

int NDIVS = 100; // # of subdivisions, higher is more accurate, but slower, and vice versa
PVector [] cps; // curve controll points
int gridWidth = 30; 
int gridHeight = 30; 
float shapeWidth = 19; 
float shapeHeight = 19; 
float reactorScaler = .006;

void setup() {
  size(500,500);
  smooth();

  cps = new PVector[4];
  for (int i=0; i<4; i++) {
    cps[i] = new PVector();
  }
  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;
}
 
void draw() {
  background(255);
  drawGeometry();
}
 
void 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);
  fill(0);
  noStroke();
for(int i = 0; i<gridWidth; i++ ) {
      for(int j = 0; j<gridHeight; j++ ) {
         
         
          PVector myPos = new PVector(i*shapeWidth, j*shapeHeight);
          PVector cpt = ClosestPointOnCatmullRom(cps,myPos,NDIVS);
          float reactorDistance = dist(cpt.x, cpt.y, myPos.x, myPos.y);
          float scaler = reactorDistance*reactorScaler;
          //translate(posX, posY);
     
          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
 */
PVector ClosestPointOnCatmullRom(PVector [] cps, PVector pt, int steps) {
  PVector result = new PVector();
  float bestDistanceSquared = 0;
  float bestT = 0;
  for (int i=0; i<=steps; i++) {
    float t = (float)(i) / (float)(steps);
    float x = curvePoint(cps[0].x,cps[1].x,cps[2].x,cps[3].x,t);
    float y = curvePoint(cps[0].y,cps[1].y,cps[2].y,cps[3].y,t);
    float dissq = dist(pt.x, pt.y, x, y);
    if (i==0 || dissq < bestDistanceSquared) {
      bestDistanceSquared = dissq;
      bestT = t;
      result.set(x,y,0);
    }
  }
  return result;
}
, multiple selections available,
Related content
Reactors
Reactors
Luke Franzke
p5.js Simple Reactor
p5.js Simple Reactor
Luke Franzke
3D Reactors
3D Reactors
Luke Franzke
p5.js Reactors
p5.js Reactors
Luke Franzke
p5.js Particle System
p5.js Particle System
paulina.zybinska
Modifying scale with distribution
Modifying scale with distribution
Luke Franzke
For you

Programming
Results will update as you type.
  • Tiling and Repetition
  • Reactors
    • Simple Reactor
    • Vector Fields
    • Bitmap or SVG Permutations
    • (Extra) Curve Reactor
    • Lesson 2.4 - Exercises
    • 3D Reactors
  • Programming Basics: Parametric and Generative Graphic Design 2016
  • Archive
  • High Scores
  • Artificial Neural Network
  • Alternatives to the Processing IDE
  • p5.js Programming
  • Programming in Processing (java)

{"serverDuration": 13, "requestCorrelationId": "944e26adec1f4212a831a9a4d8b6d46b"}