+ color CalcPoint(xyz P) {
+ color c = 0;
+ P.RotateZ(mCtr, zSin, zCos);
+
+ if (CurAnim == 6 || CurAnim == 7) {
+ P.setNorm();
+ return lx.hsb(0,0, 100 * (
+ constrain(1-50*(1-pDensity.Val())*abs(P.y-sin(zTime*10 + P.x*(300))*.5 - .5),0,1) +
+ (CurAnim == 7 ? constrain(1-50*(1-pDensity.Val())*abs(P.x-sin(zTime*10 + P.y*(300))*.5 - .5),0,1) : 0))
+ );
+ }
+
+ if (iSymm == XSym && P.x > mMax.x/2) P.x = mMax.x-P.x;
+ if (iSymm == YSym && P.y > mMax.y/2) P.y = mMax.y-P.y;
+
+ for (int i=0;i<_ND; i++) if (N[i].Active()) {
+ NDat n = N[i];
+ float zx = zTime * n.speed * n.sinAngle,
+ zy = zTime * n.speed * n.cosAngle;
+
+ float b = (iSymm==RadSym ? noise(zTime*n.speed+n.xoff-Dist(P,mCtr)/n.xz)
+ : noise(P.x/n.xz+zx+n.xoff,P.y/n.yz+zy+n.yoff,P.z/n.zz+n.zoff))
+ *1.8;
+
+ b += n.den/100 -.4 + pDensity.Val() -1;
+ b += transAdd;
+ c = blendColor(c,color(n.hue,100*n.sat,c1c(b)),ADD);
+ }
+ return c;
+ }
+}
+//----------------------------------------------------------------------------------------------------------------------------------
+public class Play extends DPat
+{
+ public class rAngle {
+ float prvA, dstA, c;
+ float prvR, dstR, r;
+ float _cos, _sin, x, y;
+ float fixAngle (float a, float b) { return a<b ?
+ (abs(a-b) > abs(a+2*PI-b) ? a : a+2*PI) :
+ (abs(a-b) > abs(a-2*PI-b) ? a : a-2*PI) ; }
+ float getX(float r) { return mCtr.x + _cos*r; }
+ float getY(float r) { return mCtr.y + _sin*r; }
+ void move() { c = interp(t,prvA,dstA);
+ r = interp(t,prvR,dstR);
+ _cos = cos(c); _sin = sin(c);
+ x = getX(r); y = getY(r); }
+ void set() { prvA = dstA; dstA = random(2*PI); prvA = fixAngle(prvA, dstA);
+ prvR = dstR; dstR = random(mCtr.y); }
+ }
+
+ int nBeats = 0;
+ DParam pAmp, pRadius, pBounce;
+
+ float t,amp,rad,bnc;
+ float zTheta=0;
+ ArrayList<rWave> waves = new ArrayList<rWave>(10);
+
+ rAngle a1 = new rAngle(), a2 = new rAngle(),
+ a3 = new rAngle(), a4 = new rAngle();
+ xyz cPrev = new xyz(), cRand = new xyz(),
+ cMid = new xyz(), V = new xyz(),
+ Theta = new xyz(), TSin = new xyz(),
+ TCos = new xyz(), cMidNorm = new xyz(),
+ Pn = new xyz();
+ float LastBeat=3, LastMeasure=3;
+ int CurRandTempo = 1, CurRandTPat = 1;
+
+ Pick pTimePattern, pTempoMult, pShape;
+ int RandCube;
+
+ Play(GLucose glucose) {
+ super(glucose);
+ pRadius = addParam("Rad" , .1 );
+ pBounce = addParam("Bnc" , .2 );
+ pAmp = addParam("Amp" , .2 );
+ pTempoMult = addPick ("TMult" , 0 , 5 , new String[] {"1x", "2x", "4x", "8x", "16x", "Rand" } );
+ pTimePattern= addPick ("TPat" , 6 , 7 , new String[] {"Bounce", "Sin", "Roll", "Quant", "Accel", "Deccel", "Slide", "Rand"} );
+ pShape = addPick ("Shape" , 3 , 15 , new String[] {"Line", "Tap", "V", "RandV",
+ "Pyramid", "Wings", "W2", "Clock",
+ "Triangle", "Quad", "Sphere", "Cone",
+ "Noise", "Wave", "?", "?"} );
+ }
+
+ public class rWave {
+ float v0, a0, x0, t,damp,a;
+ boolean bDone=false;
+ final float len=8;
+ rWave(float _x0, float _a0, float _v0, float _damp) { x0=_x0*len; a0=_a0; v0=_v0; t=0; damp = _damp; }
+ void move(double deltaMs) {
+ t += deltaMs*.001;
+ if (t>4) bDone=true;
+ }
+ float val(float _x) {
+ _x*=len;
+ float dist = t*v0 - abs(_x-x0);
+ if (dist<0) { a=1; return 0; }
+ a = a0*exp(-dist*damp) * exp(-abs(_x-x0)/(.2*len)); // * max(0,1-t/dur)
+ return -a*sin(dist);
+ }
+ }
+
+ void StartPattern() { zTheta=0; }
+ void StartRun(double deltaMs) {
+ t = lx.tempo.rampf();
+ amp = pAmp.Val();
+ rad = pRadius.getValuef();
+ bnc = pBounce.getValuef();
+ zTheta += deltaMs*(pSpin .Val()-.5)*.01;
+
+ Theta .set(pRotX.Val()*PI*2, pRotY.Val()*PI*2, pRotZ.Val()*PI*2 + zTheta);
+ TSin .set(sin(Theta.x), sin(Theta.y), sin(Theta.z));
+ TCos .set(cos(Theta.x), cos(Theta.y), cos(Theta.z));
+
+ if (t<LastMeasure) {
+ if (random(3) < 1) { CurRandTempo = int(random(4)); if (CurRandTempo == 3) CurRandTempo = int(random(4)); }
+ if (random(3) < 1) { CurRandTPat = pShape.Cur() > 6 ? 2+int(random(5)) : int(random(7)); }
+ } LastMeasure = t;
+
+ int nTempo = pTempoMult .Cur(); if (nTempo == 5) nTempo = CurRandTempo;
+ int nTPat = pTimePattern.Cur(); if (nTPat == 7) nTPat = CurRandTPat ;
+
+ switch (nTempo) {
+ case 0: t = t; break;
+ case 1: t = (t*2. )%1.; break;
+ case 2: t = (t*4. )%1.; break;
+ case 3: t = (t*8. )%1.; break;
+ case 4: t = (t*16.)%1.; break;
+ }
+
+ int i=0; while (i< waves.size()) {
+ rWave w = waves.get(i);
+ w.move(deltaMs); if (w.bDone) waves.remove(i); else i++;
+ }
+
+ if ((t<LastBeat && !pKey.b) || DG.KeyPressed>-1) {
+ waves.add(new rWave(
+ pKey.b ? map(DG.KeyPressed,0,7,0,1) : random(1), // location
+ bnc*10, // bounciness
+ 7, // velocity
+ 2*(1-amp))); // dampiness
+ DG.KeyPressed=-1;
+ if (waves.size() > 5) waves.remove(0);
+ }
+
+ if (t<LastBeat) {
+ cPrev.set(cRand); cRand.setRand();
+ a1.set(); a2.set(); a3.set(); a4.set();
+ } LastBeat = t;
+
+ switch (nTPat) {
+ case 0: t = sin(PI*t); break; // bounce
+ case 1: t = norm(sin(2*PI*(t+PI/2)),-1,1); break; // sin
+ case 2: t = t; break; // roll
+ case 3: t = constrain(int(t*8)/7.,0,1); break; // quant
+ case 4: t = t*t*t; break; // accel
+ case 5: t = sin(PI*t*.5); break; // deccel
+ case 6: t = .5*(1-cos(PI*t)); break; // slide
+ }
+
+ cMid.set (cPrev); cMid.interpolate (t,cRand);
+ cMidNorm.set (cMid); cMidNorm.setNorm();
+ a1.move(); a2.move(); a3.move(); a4.move();
+ }
+
+ color CalcPoint(xyz Px) {
+ if (Theta.x != 0) Px.RotateX(mCtr, TSin.x, TCos.x);
+ if (Theta.y != 0) Px.RotateY(mCtr, TSin.y, TCos.y);
+ if (Theta.z != 0) Px.RotateZ(mCtr, TSin.z, TCos.z);
+
+ Pn.set(Px); Pn.setNorm();
+
+ float mp = min(Pn.x, Pn.z);
+ float yt = map(t,0,1,.5-bnc/2,.5+bnc/2);
+ float r,d;
+
+ switch (pShape.Cur()) {
+ case 0: V.set(Pn.x, yt , Pn.z); break; // bouncing line
+ case 1: V.set(Pn.x, map(cos(PI*t * Pn.x),-1,1,0,1) , Pn.z); break; // top tap
+ case 2: V.set(Pn.x, bnc*map(Pn.x<.5?Pn.x:1-Pn.x,0,.5 ,0,t-.5)+.5, Pn.z); break; // V shape
+ case 3: V.set(Pn.x, Pn.x < cMidNorm.x ? map(Pn.x,0,cMidNorm.x, .5,yt) :
+ map(Pn.x,cMidNorm.x,1, yt,.5), Pn.z); break; // Random V shape
+
+ case 4: V.set(Pn.x, .5*(Pn.x < cMidNorm.x ? map(Pn.x,0,cMidNorm.x, .5,yt) :
+ map(Pn.x,cMidNorm.x,1, yt,.5)) +
+ .5*(Pn.z < cMidNorm.z ? map(Pn.z,0,cMidNorm.z, .5,yt) :
+ map(Pn.z,cMidNorm.z,1, yt,.5)), Pn.z); break; // Random Pyramid shape
+
+ case 5: V.set(Pn.x, bnc*map((Pn.x-.5)*(Pn.x-.5),0,.25,0,t-.5)+.5, Pn.z); break; // wings
+ case 6: V.set(Pn.x, bnc*map((mp -.5)*(mp -.5),0,.25,0,t-.5)+.5, Pn.z); break; // wings
+
+ case 7: d = min(
+ distToSeg(Px.x, Px.y, a1.getX(70),a1.getY(70), mCtr.x, mCtr.y),
+ distToSeg(Px.x, Px.y, a2.getX(40),a2.getY(40), mCtr.x, mCtr.y));
+ d = constrain(30*(rad*40-d),0,100);
+ return lx.hsb(0,max(0,150-d), d); // clock
+
+ case 8: r = amp*200 * map(bnc,0,1,1,sin(PI*t));
+ d = min(
+ distToSeg(Px.x, Px.y, a1.getX(r),a1.getY(r), a2.getX(r),a2.getY(r)),
+ distToSeg(Px.x, Px.y, a2.getX(r),a2.getY(r), a3.getX(r),a3.getY(r)),
+ distToSeg(Px.x, Px.y, a3.getX(r),a3.getY(r), a1.getX(r),a1.getY(r)) // triangle
+ );
+ d = constrain(30*(rad*40-d),0,100);
+ return lx.hsb(0,max(0,150-d), d); // clock
+
+ case 9: r = amp*200 * map(bnc,0,1,1,sin(PI*t));
+ d = min(
+ distToSeg(Px.x, Px.y, a1.getX(r),a1.getY(r), a2.getX(r),a2.getY(r)),
+ distToSeg(Px.x, Px.y, a2.getX(r),a2.getY(r), a3.getX(r),a3.getY(r)),
+ distToSeg(Px.x, Px.y, a3.getX(r),a3.getY(r), a4.getX(r),a4.getY(r)),
+ distToSeg(Px.x, Px.y, a4.getX(r),a4.getY(r), a1.getX(r),a1.getY(r)) // quad
+ );
+ d = constrain(30*(rad*40-d),0,100);
+ return lx.hsb(0,max(0,150-d), d); // clock
+
+ case 10:
+ r = map(bnc,0,1,a1.r,amp*200*sin(PI*t));
+ return lx.hsb(0,0,c1c(.9+2*rad - dist(Px.x,Px.y,a1.getX(r),a1.getY(r))*.03) ); // sphere
+
+ case 11:
+ Px.z=mCtr.z; cMid.z=mCtr.z;
+ return lx.hsb(0,0,c1c(1 - CalcCone(Px,cMid,mCtr) * 0.02 > .5?1:0)); // cone
+
+ case 12: return lx.hsb(100 + noise(Pn.x,Pn.y,Pn.z + (NoiseMove+50000)/1000.)*200,
+ 85,c1c(Pn.y < noise(Pn.x + NoiseMove/2000.,Pn.z)*(1+amp)-amp/2.-.1 ? 1 : 0)); // noise
+
+ case 13: float y=0; for (rWave w : waves) y += .5*w.val(Pn.x);
+ V.set(Pn.x, .7+y, Pn.z);
+ break;
+
+ default: return lx.hsb(0,0,0);
+ }
+
+ return lx.hsb(0,
+ 150-c1c(1 - V.distance(Pn)/rad),
+ c1c(1 - V.distance(Pn)/rad));
+ }
+}
+//----------------------------------------------------------------------------------------------------------------------------------
+// 0 - TLB, L (b), BLB, B (l) // Fwd , Down, Back, Up
+// 4 - TLF, F (l), BLF, L (f) // Fwd , Down, Back, Up
+// 8 - TRF, R (f), BRF, F (r) // Back, Down, Fwd , Up
+// 12- TRB, B (r), BRB, R (b) // Back, Down, Fwd , Up
+// 1->7, 15->9
+
+int randDir() { return round(random(1))*2-1; }
+//----------------------------------------------------------------------------------------------------------------------------------
+boolean dDebug = true;
+class dCursor {
+ dVertex v, vNext;
+ int pos,posNext; // 0 - 65535
+ int stop; // 0-15
+ color clr;
+
+ dCursor(color _c) { clr=_c;}
+
+ boolean isDone() { return pos==stop<<12; }
+ void set(dVertex _v, int _pos) { pos = _pos; v = _v; vNext = null; stop=16; }
+
+ boolean Turn(dTurn t) {
+ if (t.pos0<<12 < pos) return false;
+ vNext = t.v;
+ stop = t.pos0;
+ posNext = t.pos1<<12;
+ if (randDir()<0) { vNext = t.v.opp; posNext = (16<<12)-posNext; }
+ return true;
+ }
+
+ void PickNext() {
+ if (vNext != null) { set(vNext, posNext); }
+ else { set(random(2) < 1 ? v.c0 : v.c1, 0); }
+ // plan to turn the corner
+ float r = random(3);
+ if (r<1 && v.t0 != null && Turn(v.t0)) return;
+ if (r<2 && v.t1 != null && Turn(v.t1)) return;
+ }
+ Point p1, p2; int i2;
+}
+
+//----------------------------------------------------------------------------------------------------------------------------------
+class Worms extends SCPattern {
+ float StripsPerSec = 10;
+ float TrailTime = 1000;
+ int Cursors = 25;
+ dCursor cur[] = new dCursor[Cursors];
+
+ int draw(dCursor c, int nAmount) {
+ int nFrom = (c.pos ) >> 12;
+ int nMv = min(nAmount, (c.stop<<12)-c.pos);
+ int nTo = min(15,(c.pos+nMv) >> 12);
+
+ if (dDebug) { c.p1 = c.v.getPoint(nFrom); float d = (c.p2 == null ? 0 : PointDist(c.p1,c.p2)); if (d>5) { println("too wide! quitting: " + d); exit(); }}
+ for (int i = nFrom; i <= nTo; i++) { colors[c.v.ci + c.v.dir*i ] = c.clr; }
+ if (c.v.same != null) for (int i = nFrom; i <= nTo; i++) { colors[c.v.same.ci + c.v.same.dir*i] = c.clr; }
+ if (dDebug) { c.p2 = c.v.getPoint(nTo); c.i2 = nTo; }
+
+ c.pos += nMv; return nAmount - nMv;
+ }
+
+ Worms(GLucose glucose) {
+ super(glucose);
+ if (DL_ == null) DL_ = new dLattice();
+ for (int i=0; i<Cursors; i++) { cur[i] = new dCursor(lx.hsb(135,100,100)); DL_.setRand(cur[i]); }
+ }
+
+ void run(double deltaMs) {
+ for (int i=0,s=model.points.size(); i<s; i++) {
+ color c = colors[i]; float b = brightness(c);
+ if (b>0) colors[i] = color(hue(c), saturation(c), (float)(b-100*deltaMs/TrailTime));
+ }
+
+ for (int i=0; i<Cursors; i++) {
+ int nLeft = floor((float)deltaMs*.001*StripsPerSec * 65536);
+ while(nLeft > 0) { nLeft = draw(cur[i], nLeft); if (cur[i].isDone()) cur[i].PickNext(); }