// Non-linear speed control project P1a for the Animation class // Written by Jarek Rossignac in August 2006 // comment out if not using OpenGL PImage carImage; // image used as tecture for car float t=0, t0=0, d=2000.0; // time measured in miliseconds, aniamtion start time, duration boolean run=false; // flag set when animation is running (space-bar id down) boolean graph=true; // flag to show/hide speed graph boolean s1=true, s2=false, s3=false, s4=false, s5=false, s6=false, s7=true; // flags to show/hide cars void setup(){ size(600, 600, P3D); // init with a window of 600x600. Replace with size(600, 600, P3D); when not using OpenGL colorMode(HSB,360); // color parameters (hue, saturation, brightness) in [0,360] carImage = loadImage("car.jpg"); // load image for texture initFunction(); // inits the function for f7 to be a constant speed ramp } void draw() { // executed at each frame background (0,0,360); // clear screen if (graph) { // if we are showing the graph stroke(300,360,0); line(width*0.05,0,width*0.05,height); line(width*0.95,0,width*0.95,height); // axes if (s1) {stroke(60,360,360); beginShape(LINE_STRIP); for (float x=0; x<=1.0; x+=0.01) vertex((f1(x)*0.9+0.05)*width,height*x); endShape();}; if (s2) {stroke(120,360,360); beginShape(LINE_STRIP); for (float x=0; x<=1.0; x+=0.01) vertex((f2(x)*0.9+0.05)*width,height*x); endShape();}; if (s3) {stroke(180,360,360); beginShape(LINE_STRIP); for (float x=0; x<=1.0; x+=0.01) vertex((f3(x)*0.9+0.05)*width,height*x); endShape();}; if (s4) {stroke(240,360,360); beginShape(LINE_STRIP); for (float x=0; x<=1.0; x+=0.01) vertex((f4(x)*0.9+0.05)*width,height*x); endShape();}; if (s5) {stroke(300,360,360); beginShape(LINE_STRIP); for (float x=0; x<=1.0; x+=0.01) vertex((f5(x)*0.9+0.05)*width,height*x); endShape();}; if (s6) {stroke(0,360,360); beginShape(LINE_STRIP); for (float x=0; x<=1.0; x+=0.01) vertex((f6(x)*0.9+0.05)*width,height*x); endShape();}; if (s7) {stroke(0,0,160); beginShape(LINE_STRIP); for (float x=0; x<=1.0; x+=0.01) vertex((f7(x)*0.9+0.05)*width,height*x); endShape();}; } textureMode(NORMALIZED); // texture parameters in [0,1]x[0,1] if (run) { t=millis()-t0; if (t>d) t=d; }; // time during animation if (graph) {stroke(300,0,100); line(width*0.05,t*height/d,width*0.95,t*height/d); }; // sweeps horizontal line as a function of time pushMatrix(); // to be able to restore the original coordinate system translate(0,height/20.0); pushMatrix(); translate(width/10+f1(t/d)*width*0.8,0); stroke(60,360,360); if (s1) showCar(20); popMatrix(); translate(0,height/10.0); pushMatrix(); translate(width/10+f2(t/d)*width*0.8,0); stroke(120,360,360); if (s2) showCar(20); popMatrix(); translate(0,height/10.0); pushMatrix(); translate(width/10+f3(t/d)*width*0.8,0); stroke(180,360,360); if (s3) showCar(20); popMatrix(); translate(0,height/10.0); pushMatrix(); translate(width/10+f4(t/d)*width*0.8,0); stroke(240,360,360); if (s4) showCar(20); popMatrix(); translate(0,height/10.0); pushMatrix(); translate(width/10+f5(t/d)*width*0.8,0); stroke(300,360,360); if (s5) showCar(20); popMatrix(); translate(0,height/10.0); pushMatrix(); translate(width/10+f6(t/d)*width*0.8,0); stroke(0,360,360); if (s6) showCar(20); popMatrix(); translate(0,height/10.0); pushMatrix(); translate(width/10+f7(t/d)*width*0.8,0); stroke(0,0,150); if (s7) showCar(20); popMatrix(); popMatrix(); // restore the original coordinate system stroke(150); if (s7) showConstraints(); }; void keyPressed() { if(key=='1') s1=!s1; if(key=='2') s2=!s2; if(key=='3') s3=!s3; if(key=='4') s4=!s4; if(key=='5') s5=!s5; if(key=='6') s6=!s6; if(key=='7') s7=!s7; if(key==' ') if (!run) {run=true; t0=millis();}; // start animation when pressed space bar if(key=='g') graph=!graph; // toggle graph display if(key=='s') {for (int i=0; i<1000; i++) smoother();}; // press s to smooth f7 } void keyReleased() {if(run) {run=false; t=0;};} // stop animation // to render image of car in local coordinates void showCar(float r) { beginShape(QUADS); texture(carImage); vertex(-2*r,-r,1,0); vertex(2*r,-r,0,0); vertex(2*r,r,0,1); vertex(-2*r,r,1,1); endShape(); }; // *** CODE FOR f7 int N=50; // number of samples float V[] = new float[N]; // values at samples float S[] = new float[N]; // constrained values at samples float L[] = new float[N]; // laplace adjustments boolean C[] = new boolean[N]; // if constrained void initFunction() { // sets inital values and constraint flags for (int i=0; i