// SketchLineAndFollow3D class Line { private float drawT; private float drawFollowPauseT; private float followT; private float fadeT; private float x1; private float y1; private float x2; private float y2; private float dx; private float dy; public Line(float _x1, float _y1, float _x2, float _y2) { float len; x1 = _x1; y1 = _y1; x2 = _x2; y2 = _y2; dx = x2 - x1; dy = y2 - y1; len = sqrt(dx*dx + dy*dy); dx *= 5/len; dy *= 5/len; drawT = 0; followT = 0; fadeT = 0; } public boolean done() { return fadeT >= 1.0; } public void draw() { float xt, yt; xt = x1 + (x2 - x1) * drawT; yt = y1 + (y2 - y1) * drawT; noStroke(); fill(0, 255 * (1 - fadeT)); quad(x1-dy,y1+dx, x1+dy,y1-dx, xt+dy,yt-dx, xt-dy,yt+dx); if (drawFollowPauseT >= 1.0) { xt = x1 + (x2 - x1) * followT; yt = y1 + (y2 - y1) * followT; fill(color(192, 0, 0, 255 * (1 - fadeT))); pushMatrix(); translate(xt,yt,5); sphere(10); popMatrix(); } } public void step() { if (drawT < 1.0) drawT += 0.01; else if (drawFollowPauseT < 1.0) drawFollowPauseT += 0.05; else if (followT < 1.0) followT += 0.01; else if (fadeT < 1.0) fadeT += 0.01; } public void moveCamera() { float xm, ym; float cx, cy, cz; xm = width/2 - mouseX; ym = mouseY/2; cx = 0; cy = 0; cz = height/2; if (drawFollowPauseT < 1.0) { cx = drawT*x1 + (1 - drawT)*xm; cy = drawT*y1 + (1 - drawT)*ym; } else if (followT < 1.0) { cx = x1 + followT*(x2 - x1); cy = y1 + followT*(y2 - y1); } else if (fadeT < 1.0) { cx = (1-fadeT)*x2 + fadeT*xm; cy = (1-fadeT)*y2 + fadeT*ym; } camera(cx, cy, cz, // eyeX, eyeY, eyeZ 0.0, 0.0, 0.0, // centerX, centerY, centerZ 0.0, 1.0, 0.0); // upX, upY, upZ } } Line randomLine() { return new Line(random(-1,1)*width/4, random(-1,1)*height/4, random(-1,1)*width/4, random(-1,1)*height/4); } Line activeLine; // - - - - - - - - void setup() { size(600, 400, P3D); frameRate(30); activeLine = randomLine(); } void draw() { background(0); lights(); activeLine.moveCamera(); noStroke(); fill(204); pushMatrix(); translate(0,0,-7); box(width/2,height/2,6); popMatrix(); stroke(225); line(-width/4, 0, -1, width/4, 0, -1); line(0, -height/4, -1, 0, height/4, -1); activeLine.draw(); activeLine.step(); if (activeLine.done()) activeLine = randomLine(); }