Willkommen auf unserem Seminar-Blog

Immer auf dem aktuellen Stand bleiben

Dieser Seminar-Blog befindet sich noch im Aufbau und wird in den kommenden Tagen entsprechend verfeinert.

Member Login

Lost your password?

Registration is closed

Sorry, you are not allowed to register by yourself on this site!

You must either be invited by one of our team member or request an invitation by email at viad.info {at} zhdk {dot} ch.

Lesson 2.4 (Extra) Curve Reactor

Curve Reactor


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 example only their effect is additive.
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 = .01;
 
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);
  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);
  stroke(0);
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);
          noFill();
          stroke(0);
          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;
}

Exercise: Create a new example of that uses multiple reactor points based on a history of mouse locations. The history of mouse locations can be stored in a pvector list.