// car accidents // Written June 2006 by Jarek Rossignac color red=color(256,100,100), green=color(100,256,100), blue=color(100,100,256), yellow=color(256,230,100), grey=color(100,100,100), lightGrey=color(60,60,60); int nr=2; car C [] = new car [nr]; int ns=4; street S [] = new street [ns]; vec I=new vec (1,0); boolean displayPath = true; // shows whether control points for car paths are displayed boolean animating = false; // set to true while the scenario is animating float t = 0.0; // track time class street { float w=80; // street width pt B=new pt(700,100), F=new pt(600,700); // front and back end-points boolean pickedB=false, pickedF=false, picked=false; street (float bx, float by, float fx, float fy, float pw) { B.x=bx; B.y=by; F.x=fx; F.y=fy; w=pw; }; void paint() { fill(200,200,100); noStroke(); vec N=B.vecTo(F).left(); N.unit(); N.mul(w); beginShape(); B.movedBy(N).vert(); F.movedBy(N).vert(); N.back(); F.movedBy(N).vert(); B.movedBy(N).vert(); endShape(); }; void paintLine() { B.showLineTo(F); }; boolean selected() { pickedF = F.vecTo(mouse()).norm()<30; pickedB = B.vecTo(mouse()).norm()<30; picked=pickedF||pickedB; return(picked); } void track() { pt M=mouse(); if (pickedF) { F.setTo(M); } if (pickedB) { B.setTo(M); } } void paintDots() { if (!pickedB) { fill(yellow); B.show(20); }; if (!pickedF) { fill(yellow); F.show(20); }; }; void snap() { if (pickedB) { if(abs(B.x-F.x)<5) { B.x=F.x; }; if(abs(B.y-F.y)<5) { B.y=F.y; }; }; if (pickedF) { if(abs(F.x-B.x)<5) { F.x=B.x; }; if(abs(F.y-B.y)<5) { F.y=B.y; }; }; } } class car { float h=40, w=100; // car dimensions color col; // color of car pt B=new pt(0,0), F=new pt(w,0); // front and back points float a=0.0; // angle and increment boolean picked = false; // true if being dragged int numPts = 4; pt[] animPts = new pt[numPts]; // array of points that control the path the car follows when animating int animPicked = -1; // -1 if there is no control point being dragged, otherwise # is selected index pt B2, F2; float a2; car (float px, float py, float pa, color pc) { B.x=px; B.y=py; a=pa; col=pc; F.x=px+w; F.y=py; F.turnAround(B,-a); for (int i = 0; i < numPts; i++) { vec distVec = B.vecTo(F); distVec.mul((float)i); pt tempPt = F.make(); tempPt.addVec(distVec); animPts[i] = tempPt; } } // creation void paint() { pushMatrix(); translate(B.x,B.y); rotate(a); // paints car noStroke(); if (picked) { fill(yellow); } else { fill(col); }; rect(w/2,0,w,h); ellipse(0,0,w/7,h*0.95); ellipse(w,0,w/5,h*0.95); fill(240); ellipse(w*0.2,0,w*0.3,h); ellipse(w*0.2+w/3,0,w*0.4,h); if (picked) { fill(yellow); } else { fill(col); }; rect(w*0.2+w/6,0,2*w/6,h); popMatrix(); } boolean selected() { boolean pic = F.vecTo(mouse()).norm()=0; i--) { if (!found) { if(C[i].selected()) { found=true; }; if (displayPath) { // don't allow pts to be moved if not visible int ctrlPt = C[i].controlPtSelected(); if(ctrlPt != -1) { C[i].animPicked = ctrlPt; found=true; }; }; }; }; for (int i=ns-1; i>=0; i--) { if (!found) { if(S[i].selected()) { found=true; }; }; }; } void mouseDragged() { for (int i=0; i