using System; using System.Collections.Generic; namespace frame { public class physik { public physik () { } } public class ball{ public double x,y,vx,vy; public ball(int xx,int yy){ x = xx; y = yy; vx = 0; vy = 0; } public void bouge(){ if (vx < -1.0){ vx = -1.0;} if (vx > 1.0){ vx = 1.0;} if (vy < -1.0){ vy = -1.0;} if (vy > 1.0){ vy = 1.0;} x = x + vx; y = y + vy; } public void wall(int w, int h){ if (x<3){ vx = -vx*0.618; x = 4; } if (x> w-3){ vx = -vx*0.618; x = w-4; } if (y < 3){ vy = -vy*0.618; y = 4; } if (y > h-3){ vy = -vy*0.618; y= h-4; } } } /*--------------------------------------------------------------*/ /*implémentation simple du Spring System*/ /*Ajusté par essaie érreure*/ /*pour des raisons de performance, nL/L est remplacé par nL^2/L^2*/ public class spring{ public ball b1,b2; double length, ratio; public spring(ball bb1, ball bb2, double rr=0.0707){ b1 = bb1; b2 = bb2; ratio = rr; //length = Math.Sqrt(Math.Pow(b2.x - b1.x, 2) + Math.Pow(b2.y - b1.y, 2)); length = Math.Pow(b2.x-b1.x,2)+Math.Pow(b2.y-b1.y,2); } public void tire(){ double deltaX = b2.x - b1.x; double deltaY = b2.y - b1.y; //double newlength = Math.Sqrt(Math.Pow(deltaX, 2) + Math.Pow(deltaY, 2)); //double force = ((newlength - length) * ratio)/2; double newlength = Math.Pow(deltaX,2) + Math.Pow(deltaY,2); double force = ((newlength/(length*2))-0.5); b1.vx += deltaX*force; b1.vy += deltaY*force; b2.vx -= deltaX*force; b2.vy -= deltaY*force; } } /*--------------------------------------------------------------*/ /*un simple triangle pour tester les springs*/ public class blob{ public List balls = new List(); public List springs = new List(); public blob(){ ball a = new ball(100,130); ball b = new ball(50,50); ball c = new ball(170,70); balls.Add(a); balls.Add(b); balls.Add(c); springs.Add(new spring(a,b)); springs.Add(new spring(c,b)); springs.Add(new spring(a,c)); } public void bouge(){ foreach(ball tmp in balls){ tmp.bouge(); } foreach(spring tmp in springs){ tmp.tire(); } } } /*traduire la string du L-system en structure de spring*/ /*chaque point est join au deux plus bas par des spring*/ public class LTree{ public List balls = new List(); public List bezlst = new List(); public List springs = new List(); public LTree(string Lsys){ Stack stack1 = new Stack(); Stack stack2 = new Stack(); ball[] bezone; ball a = new ball(700,500); ball b = new ball(700,480); ball c; springs.Add(new spring(a,b)); double radian = 0.0; double x=700.0; double y = 460.0; //pour le rendu graphic bezone=new ball[] {a,b,null}; for (int i =0; i < Lsys.Length ; i=i+1){ if (Lsys[i] == '+'){ //tourne a gauche radian = radian- (Math.PI/7); } else if (Lsys[i]=='-'){ //tourna a droite radian = radian+(Math.PI /7); } else if(Lsys[i]=='['){ //stock position et angle stack1.Push(new double[]{x,y,radian}); stack2.Push(new ball[]{a,b}); //pour le rendu graphic bezlst.Add(bezone); bezone = new ball[]{a,b,null}; } else if(Lsys[i]==']'){ //récupere position et angle double[] tmp1 = stack1.Pop(); ball[] tmp2 = stack2.Pop(); x = tmp1[0]; y = tmp1[1]; radian = tmp1[2]; a = tmp2[0]; b = tmp2[1]; //pour le rendu graphic bezlst.Add(bezone); bezone = new ball[]{a,b,null}; } else if ((Lsys[i]=='F')||(Lsys[i]=='E')){ //faire une ligne avancer x = x + (20.0*Math.Sin(radian)); y = y - (20.0*Math.Cos(radian)); c = new ball((int)x,(int)y); balls.Add(c); springs.Add(new spring(c,a,0.5)); springs.Add(new spring(c,b)); a = b; b = c; //pour le rendu graphic bezone[2] = c; } bezlst.Add(bezone); } } } }