repetitive operation?
in
Programming Questions
•
1 year ago
Hi !
Thanks !
I have started some experimentation with circle packing in Processing. My problem is that my code is very heavy, i'd like to lighten it, or to lighten the process ... for exemple, here is my main opération :
if (abs(x2 - x1) < R2 &&
abs(y2 - y1) < R2) {
x1 += random(-m, m);
y1 += random (-m, m);
}
x1, y1, x2, y2 are just the beginning of the serie.
Is it possible to use something like
n=2
p=1
if (abs(xn - xp) < Rn &&
abs(yn - yp) < Rn) {
xp += random(-m, m);
yp += random (-m, m);
}
here is the whole code :
char coord1, coord2, coord3;float x1, x2, x3, x4, x5, x6 ;float y1, y2, y3, y4, y5, y6 ;float R1, R2, R3, R4, R5, R6 ;float R1B, R2B; //bruitfloat R1O, R2O; //odeurfloat m; //mouvement
void setup () {frameRate(50);size (400, 400);background(100);m=5;// 01x1 = 200 ;y1 = 200 ;R1 = 30;R1B = 40;
//02x2 = 210;y2 = 210;R2 = 50;R2B = 60;
//03x3 = 230;y3 = 210;R3 = 60;
//04x4 = 220;y4 = 200;R4 = 40;
//05x5 = 220;y5 = 200;R5 = 40;
//06x6 = 220;y6 = 200;R6 = 40;}void draw () {smooth();fill (0, 5);rect (0, 0, width, height);noStroke();
// 1if (abs(x2 - x1) < R2 &&abs(y2 - y1) < R2) {x1 += random(-m, m);y1 += random (-m, m);}if (abs(x3 - x1) < R3 &&abs(y3 - y1) < R3) {x1 += random(-m, m);y1 += random (-m, m);}if (abs(x4 - x1) < R4 &&abs(y4 - y1) < R4) {x1 += random(-m, m);y1 += random (-m, m);}if (abs(x5 - x1) < R5 &&abs(y5 - y1) < R5) {x1 += random(-m, m);y1 += random (-m, m);}if (abs(x6 - x1) < R6 &&abs(y6 - y1) < R6) {x1 += random(-m, m);y1 += random (-m, m);}
//2if (abs(x1 - x2) < R1 &&abs(y1 - y2) < R1) {x2 += random(-m, m);y2 += random (-m, m);}if (abs(x3 - x2) < R3 &&abs(y3 - y2) < R3) {x2 += random(-m, m);y2 += random (-m, m);}if (abs(x4 - x2) < R4 &&abs(y4 - y2) < R4) {x2 += random(-m, m);y2 += random (-m, m);}if (abs(x5 - x2) < R5 &&abs(y5 - y2) < R5) {x2 += random(-m, m);y2 += random (-m, m);}if (abs(x6 - x2) < R6 &&abs(y6 - y2) < R6) {x2 += random(-m, m);y2 += random (-m, m);}
//3if (abs(x1 - x3) < R1 &&abs(y1 - y3) < R1) {x3 += random(-m, m);y3 += random (-m, m);}if (abs(x2 - x3) < R2 &&abs(y2 - y3) < R2) {x3 += random(-m, m);y3 += random (-m, m);}if (abs(x4 - x3) < R4 &&abs(y4 - y3) < R4) {x3 += random(-m, m);y3 += random (-m, m);}if (abs(x5 - x3) < R5 &&abs(y5 - y3) < R5) {x3 += random(-m, m);y3 += random (-m, m);}if (abs(x6 - x3) < R6 &&abs(y6 - y3) < R6) {x3 += random(-m, m);y3 += random (-m, m);}//04if (abs(x1 - x4) < R1 &&abs(y1 - y4) < R1) {x4 += random(-m, m);y4 += random (-m, m);}if (abs(x2 - x4) < R2 &&abs(y2 - y4) < R2) {x4 += random(-m, m);y4 += random (-m, m);}if (abs(x3 - x4) < R3 &&abs(y3 - y4) < R3) {x4 += random(-m, m);y4 += random (-m, m);}if (abs(x5 - x4) < R5 &&abs(y5 - y4) < R5) {x4 += random(-m, m);y4 += random (-m, m);}if (abs(x6 - x4) < R6 &&abs(y6 - y4) < R6) {x4 += random(-m, m);y4 += random (-m, m);}//05if (abs(x1 - x5) < R1 &&abs(y1 - y5) < R1) {x5 += random(-m, m);y5 += random (-m, m);}if (abs(x2 - x5) < R2 &&abs(y2 - y5) < R2) {x5 += random(-m, m);y5 += random (-m, m);}if (abs(x3 - x5) < R3 &&abs(y3 - y5) < R3) {x5 += random(-m, m);y5 += random (-m, m);}if (abs(x4 - x5) < R4 &&abs(y4 - y5) < R4) {x5 += random(-m, m);y5 += random (-m, m);}if (abs(x6 - x5) < R6 &&abs(y6 - y5) < R6) {x5 += random(-m, m);y5 += random (-m, m);}
//06if (abs(x1 - x6) < R1 &&abs(y1 - y6) < R1) {x6 += random(-m, m);y6 += random (-m, m);}if (abs(x2 - x6) < R2 &&abs(y2 - y6) < R2) {x6 += random(-m, m);y6 += random (-m, m);}if (abs(x3 - x6) < R3 &&abs(y3 - y6) < R3) {x6 += random(-m, m);y6 += random (-m, m);}if (abs(x4 - x6) < R4 &&abs(y4 - y6) < R4) {x6 += random(-m, m);y6 += random (-m, m);}if (abs(x5 - x6) < R5 &&abs(y5 - y6) < R5) {x6 += random(-m, m);y6 += random (-m, m);}
// influence circlefill(10, 179, 255, 5);stroke(10, 179, 255, 15);ellipse(x1, y1, R1, R1);ellipse(x2, y2, R2, R2);ellipse(x3, y3, R3, R3);ellipse(x4, y4, R4, R4);ellipse(x5, y5, R5, R5);ellipse(x6, y6, R6, R6);//real circlefill(255);ellipse(x1, y1, R1/1.5, R1/1.5);ellipse(x2, y2, R2/1.5, R2/1.5);ellipse(x3, y3, R3/1.5, R3/1.5);ellipse(x4, y4, R4/1.5, R4/1.5);ellipse(x5, y5, R5/1.5, R5/1.5);ellipse(x6, y6, R6/1.5, R6/1.5);
// print the coordonatesprintln (1);println (x1);println(y1);println(2);println(x2);println(y2);println(3);println(x3);println(y3);println(4);println(x4);println(y4);println(5);println(x5);println(y5);println(6);println(x6);println(y6);}
Thanks !
Sylvain.
1