# 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.

# 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;
for (int i=0; i<4; i++) {
cps[i] = new PVector();
}
cps.x = width+300;
cps.y = -500;
cps.x= 50;
cps.y= 50;
cps.x= width-50;
cps.y= height-50;
cps.x= -500;
cps.y= height+300;
}

void draw() {
background(255);
drawGeometry();
}

void drawGeometry() {
stroke(255,0,0);
curve(cps.x,cps.y, cps.x,cps.y, cps.x,cps.y, cps.x,cps.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.x,cps.x,cps.x,cps.x,t);
float y = curvePoint(cps.y,cps.y,cps.y,cps.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.