Branche[] branch; float camx,camy,butx,buty,xc,yc,zc,colr,colg,colb; float rapport1, rapport2, rapport3; Vector trajet; Camera lacamera; boolean arret; void setup(){ size(750,500); rectMode(CORNER); ellipseMode(CENTER_DIAMETER); branch=new Branche[6]; lacamera=new Camera(); initier(); } void initier(){ xc=400; yc=300; zc=-3500; arret=false; trajet=new Vector(); rapport1=random(0f,7f); rapport2=random(0.2,7f); rapport3=random(0f,7f); float vx=random(1,5); float vy=random(1,5); float vz=random(1,5); float c=Math.round(random(1,5)); colr=random(0,100); colg=random(0,100); colb=random(0,100); background(colr,colg,colb); int n=(int)Math.round(random(3,5)); int m=(int)Math.round(random(3,5)); branch[0]=new Branche(vx,vx,vy,PI/n,0,PI/60); branch[1]=new Branche(vz,vy,vy,PI/n,800,-PI/120); branch[2]=new Branche(vz,vx,vz,-PI/(-1+n),0,-PI/60); branch[3]=new Branche(vy,vx,vx,PI/(-1+n),800,-PI/60); branch[4]=new Branche(vy,vy,vz,-PI/m,0,PI/40); branch[5]=new Branche(vx,vz,vx,-PI/m,800,-PI/120); } void loop(){ clear(); float traj=trajet.size(); if(traj<240){ fill(100);stroke(255); rect(0,400,700,22); float lo=700.0f/240*traj; fill(155); rect(0,400,lo,20); } dessinerTout(); } void mousePressed(){initier(); } void keyPressed(){ if (key=='+' )zc+=50f; if (key=='-' )zc-=50f; if (key=='s' )arret=true; } void dessinerTout(){ push(); translate(xc,yc,zc); butx=(height/2-mouseY)*0.03f; buty=(width/2-mouseX)*0.03f; camx+=(butx-camx)/20; camy+=(buty-camy)/20; rotateX(camx); rotateY(camy); if(trajet.size()>240){afficherTrajet();} pop(); if(trajet.size()<3200 && arret==false){ Vecteur w1=extremiteBras(-500+1000*cos(branch[0].tempo),0,0,branch[0],branch[1]); Vecteur w2=extremiteBras(500+1000*cos(branch[2].tempo),0,0,branch[2],branch[3]); float ra=rapport1+rapport2; Vecteur w3=Vecteur.comb(rapport1/ra,w1,rapport2/ra,w2); stroke(255,0,0); line(w1.x,w1.y,w1.z,w3.x,w3.y,w3.z) ; stroke(255,0,255); line(w2.x,w2.y,w2.z,w3.x,w3.y,w3.z) ; Vecteur w4=extremiteBras(0,0,1200*cos(branch[4].tempo),branch[4],branch[5]); float ra1=ra+rapport3; Vecteur w5=Vecteur.comb(rapport3/ra1,w4,ra/ra1,w3); stroke(255,0,0); line(w4.x,w4.y,w4.z,w5.x,w5.y,w5.z) ; stroke(255,0,255); line(w5.x,w5.y,w5.z,w3.x,w3.y,w3.z) ; push(); translate(w5.x,w5.y,w5.z);fill(255); ellipse(0,0,100,100); pop(); fill(255,255,255,70);noStroke(); beginShape(TRIANGLES); vertex(w1.x,w1.y,w1.z); vertex(w2.x,w2.y,w2.z); vertex(w4.x,w4.y,w4.z); endShape(); // dans absolu trajetAdd(w5); if(trajet.size()>2){ Vecteur position=Vecteur.comb(1,(Vecteur)trajet.elementAt(trajet.size()-1),1f,new Vecteur(0,300,0)); lacamera.placer(position,w5);} } } Vecteur extremiteBras(float px,float py,float pz,Branche b1,Branche b2){ push(); translate(xc,yc,zc); rotateX(camx); rotateY(camy); translate(px,py,pz); afficherRepere(400); b1.dessiner(); b2.dessiner(); translate(250,0,0); fill(255,255,0); box(500,30,30); translate(250,0,0); Vecteur w1=coorabs(0,0,0); pop();//absolu return w1; } void afficherRepere(float l){ stroke(255,0,0); line(0,0,0,l,0,0); stroke(0,255,0); line(0,0,0,0,l,0); stroke(0,0,255); line(0,0,0,0,0,l); } void afficherTrajet(){ Vecteur v0,v1,v,vp,vv,ww,no,coul; int i, j,k,rrr; float cot=cos(3*branch[0].tempo); float sit=sin(2*branch[0].tempo); noStroke(); if(trajet.size()>240){ beginShape(QUAD_STRIP); for( i=240;i242){ vertex(v.x,v.y,v.z); vertex(vp.x,vp.y,vp.z);} } endShape(); } } void trajetAdd(Vecteur w){ push(); rotateY(-camy); rotateX(-camx); translate(-xc,-yc,-zc); w=coorabs(w.x,w.y,w.z); pop(); trajet.addElement(w); if(trajet.size()>5000){initier();} } Vecteur coorabs(float x,float y,float z) { float xa = objectX(x, y, z); float ya = objectY(x, y, z); float za = objectZ(x, y, z); return new Vecteur(xa, ya, za); } //++++++++++++++++++++++++++++++++++++++++++++++ //++++++++++++++++++++++++++++++++++++++++++++++ public class Branche { float longueur,angle,tempo,inctempo,a,b,c; Vecteur axe; public Branche(float aa,float bb,float cc,float angl,float lon,float inc){ angle=angl; axe=new Vecteur(); longueur=lon; tempo=0; inctempo=inc; a=aa; b=bb; c=cc; } void dessiner(){ angle+=PI/7500; tempo+=inctempo; axe.x=a*(b+c*sin(tempo)); axe.y=b*(c+a*cos(tempo)); axe.z=c*(a+b*cos(tempo)); axe.normalize(); translate(longueur/2,0,0); fill(255,255,0);noStroke(); box(longueur,30,30); translate(longueur/2,0,0); rotateX(tempo); rotate(2*angle,axe.x,axe.y,axe.z); } } //------------------------------------------------------- //------------------------------------------------------- public static class Vecteur { public float x, y, z; public Vecteur() { } public Vecteur(float x, float y, float z) { this.x = x; this.y = y; this.z = z; } public Vecteur(float x, float y, float z,float lon) { float no=(float) Math.sqrt(x * x + y * y + z * z); no=lon/no; this.x = x*no; this.y = y*no; this.z = z*no; } public void place(float xx, float yy, float zz) { this.x = xx; this.y = yy; this.z = zz; } public Vecteur normalize() { float length = length(); x /= length; y /= length; z /= length; return this; } public Vecteur normalize(float k) { float length = length(); x=x/ length*k; y =y/ length*k; z =z/ length*k; return this; } public float length() { return (float) Math.sqrt(x * x + y * y + z * z); } public Vecteur cross(Vecteur v2) { Vecteur res = new Vecteur(); res.x = y * v2.z - z * v2.y; res.y =z * v2.x - x * v2.z; res.z = x * v2.y - y * v2.x; return res; } public static float dot(Vecteur v1, Vecteur v2) { return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z; } public float dot(Vecteur v1) { return v1.x * x + v1.y *y + v1.z *z; } public Vecteur mul( float d) { Vecteur res = new Vecteur(); res.x =x * d; res.y = y * d; res.z = z * d; return res; } public static Vecteur comb(float a,Vecteur v1,float b, Vecteur v2) { float rx =a* v1.x + b*v2.x; float ry =a* v1.y + b*v2.y; float rz =a* v1.z + b*v2.z; return new Vecteur(rx,ry,rz); } public Vecteur ajouter(Vecteur v,float m){ Vecteur res=new Vecteur(x+v.x*m, y+v.y*m,z+v.z*m); return res; } public Vecteur ajouter(float ax,float ay,float az){ Vecteur res=new Vecteur(x+ax, y+ay,z+az); return res; } public void afficher(String tex,BApplet ba){ ba.println(tex+"x= "+x+" y= "+y+" z= "+z); } public Vecteur projete(Vecteur laxe){ laxe.normalize(); Vecteur res=laxe.mul(this.dot(laxe)); return res; } public Vecteur projeteSurAxe(Vecteur laxe){ laxe.normalize(); Vecteur res=laxe.mul(this.dot(laxe)); return res; } public Vecteur projeteSurPlan(Vecteur laxe){ laxe.normalize(); Vecteur res=projeteSurAxe(laxe); res=this.ajouter(res,-1); return res; } public Vecteur symetrique(Vecteur laxe){ laxe.normalize(); Vecteur res=projeteSurAxe(laxe); res=this.ajouter(res,-2); return res; } public float donneAngle(Vecteur vc){ return (float) Math.acos(this.dot(vc)/(length()*vc.length())); } public Vecteur tourner(Vecteur axe,float angle){//axe est normé et orthogonal à this Vecteur vaxe=axe.mul(this.dot(axe)); Vecteur vx=this.ajouter(vaxe,-1); Vecteur ygrec=axe.cross(this); return vaxe.ajouter(Vecteur.comb((float)Math.cos(angle),this,(float)Math.sin(angle),ygrec),1); } public Vecteur cloner() { Vecteur res = new Vecteur(); res.x =x ; res.y = y ; res.z = z ; return res; } public Vecteur coorabs(BApplet ba) { float xa = ba.objectX(x,y,z); float ya = ba.objectY(x,y,z); float za = ba.objectZ(x,y,z); return new Vecteur(xa, ya, za); } // ******************************************* // ******************************************* // ******************************************* public static void afficherRepere(BApplet ba){ ba.stroke(255,0,0); ba.line(0,0,0,500,0,0); ba.stroke(0,255,0); ba.line(0,0,0,0,500,0); ba.stroke(0,0,255); ba.line(0,0,0,0,0,500); } // ******************************************* // ******************************************* // ******************************************* public void afficherLigne(float k,BApplet ba){ ba.line(x*k,y*k,z*k,-x*k,-y*k,-z*k); } } //////////////////////////////////////////////////// //CLASSE CAMERA //////////////////////////////////////////////////// class Camera { float fov, nearDist, farDist, aspect; public Camera(){ fov = 70; nearDist = 400 / 1000.0f; farDist = 400 * 10000.0f; aspect = (float)width/ (float)height; } void placer(Vecteur pos,Vecteur look){ beginCamera(); perspective(fov, aspect, nearDist, farDist); lookat(pos.x,pos.y,pos.z,look.x,look.y,look.z,0,1,0); endCamera(); } }