nsi:langages:processing:solutions:toile_mobile
Toile mobile
Solution du TD Toile mobile
final int N = 100;
final float d = 10;
final float k = 0.01;
final float Rmax = 50;
final float mu = 0.9999;
Node[] nodes = new Node[N];
void setup() {
size(800, 600);
fill(255);
stroke(255);
for (int i=0; i<N; i++) {
float x0 = random(width);
float y0 = random(height);
nodes[i] = new Node(x0, y0);
}
}
void draw() {
background(100,100,200);
for (int i=0; i<N; i++) {
for (int j=i+1; j<N; j++) {
nodes[i].acceleration(nodes[j]);
}
}
for (int i=0; i<N; i++) {
nodes[i].update();
nodes[i].display();
}
for (int i=0; i<N; i++) {
for (int j=i+1; j<N; j++) {
nodes[i].drawLine(nodes[j]);
}
}
}
class Node {
float x, y, vx, vy;
Node(float x0, float y0) {
vx = 0;
vy = 0;
x = x0;
y = y0;
}
void update() {
x += vx;
y += vy;
vx *= mu;
vy *= mu;
if (y > height) {
y = 2*height - y;
vy = -vy;
} else if (y< 0) {
y = -y;
vy = - vy;
}
if (x > width) {
x = 2*width - x;
vx = -vx;
} else if (x< 0) {
x = -x;
vx = - vx;
}
}
void acceleration(Node n) {
// modifie vx et vy selon les particules proches
float dist2 = (x-n.x)*(x-n.x) + (y - n.y)*(y - n.y);
if (dist2 > Rmax*Rmax) {
return;
}
float dvx = (n.x - x) * k;
float dvy = (n.y - y) * k;
vx += dvx;
vy += dvy;
n.vx -= dvx;
n.vy -= dvy;
}
void drawLine(Node n) {
float dist2 = (x-n.x)*(x-n.x) + (y - n.y)*(y - n.y);
if (dist2 < Rmax*Rmax) {
line(x, y, n.x, n.y);
}
}
void display() {
circle(x, y, d);
}
}
nsi/langages/processing/solutions/toile_mobile.txt · Dernière modification : de goupillwiki
