Branche[] branch; float camx,camy,xc,yc,zc,colr,colg,colb,temps; Vector trajet; BFont lafonte; void setup(){ size(700,500); rectMode(CORNER); lafonte=loadFont("Meta-Bold.vlw.gz"); trajet=new Vector(); branch=new Branche[5]; xc=400; yc=300; zc=-2700; initier(); } void initier(){ trajet.removeAllElements(); temps=0; push(); rotateX(PI/round(random(0.9f,12))); rotateY(PI/round(random(0.9f,12))); rotateZ(PI/round(random(0.9f,12))); Vecteur v=coorabs(1,0,0); pop(); float c=round(random(1,5)); colr=round(random(0,100)); colg=round(random(0,100)); colb=round(random(0,100)); background(colr,colg,colb); int n=(int)round(random(2,12)); branch[0]=new Branche(v.x,v.y,v.z,PI/n,0,PI/60); branch[1]=new Branche(v.x,v.y,v.z,PI/n,300,PI/120); branch[2]=new Branche(v.x,v.y,v.z,PI/n,300,PI/120); branch[3]=new Branche(v.x,v.y,v.z,PI/n,300,PI/120); branch[4]=new Branche(v.x,v.y,v.z,PI/n,300,PI/120); } void loop(){ clear(); temps+=0.1f; float traj=trajet.size(); if(traj<240){ fill(100); rect(0,400,700,20); float lo=700.0f/240*traj; fill(200); rect(0,402,lo,16); fill(255); rect(0,404,lo,12); fill(40,40,80); textFont(lafonte, 18); text("initialisation d'une nouvelle surface....",lo-250,415); textFont(lafonte, 200); } Vecteur w= dessinerTout(); // dans absolu trajetAdd(w); } void mousePressed(){initier(); } Vecteur dessinerTout(){ push(); translate(xc,yc,zc); camx=(height/2-mouseY)*0.01f+temps/10.0f; camy=(width/2-mouseX)*0.01f+temps/10.0f; rotateX(camx); rotateY(camy); afficherRepere(100); afficherTrajet(); branch[0].dessiner(); branch[1].dessiner(); branch[2].dessiner(); branch[3].dessiner(); branch[4].dessiner(); translate(250,0,0); fill(255,255,0); box(500,15,15); translate(250,0,0); Vecteur w=coorabs(0,0,0); pop();//absolu return w; } 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; float cot=cos(temps); float sit=sin(temps); noStroke(); beginShape(QUAD_STRIP); for( i=trajet.size()-2;i>240;i--){ v0=(Vecteur)trajet.elementAt(i+1); j=i-240*(i%2); k=2*i-240-j; v=(Vecteur)trajet.elementAt(j); vp=(Vecteur)trajet.elementAt(k); vv=vp.ajouter(v,-1); ww=vp.ajouter(v0,-1); no=(vv.cross(ww)).normalize(); coul=(new Vecteur(cot,2*sit*cot,1+2*cot*sit)).normalize(); float scal=255-255*(abs(Vecteur.dot(no,coul))); fill(scal+colr,scal+colg,scal+colb); v0=v.cloner(); 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()>2000){trajet.removeElementAt(0);} } 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(1,1,1); longueur=lon; tempo=0; inctempo=inc; a=aa; b=bb; c=cc; } void dessiner(){ angle+=PI/8000; tempo+=inctempo; axe.x=2*a+a*sin(tempo); axe.y=3*b+b*sin(tempo); axe.z=b*cos(tempo)+c*sin(tempo); axe.normalize(); translate(longueur/2,0,0); fill(255,255,0); box(longueur,15,15); 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; } /*projection sur le plan vectoriel de normale "laxe" public Vecteur projeteSurPlan(Vecteur laxe,Vecteur unpoint){ laxe.normalize(); Vecteur res=projeteSurPlan(laxe); return res.ajouter(unpoint.projeteSurAxe(res),1); }*/ 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); } }