Thor
YaBB Newbies
Offline
Posts: 8
Re: Breaking a 'for' loop
Reply #5 - May 14th , 2009, 5:44am
Quote: class deJongAttractor { PImage pi; float pa, pb, pc, pd, newx, newy, oldx, oldy, logmaxd; int N = width ; int maxdense = 0; int [][] density = new int [N][N]; float [][] previousx = new float [N][N]; void construct() { //Produces the four variables to pass to the attractor float sensitivity = 0.017; pa = map (mouseX , 0, width , -2, 2) * sensitivity; pb = map (mouseY , 0, height , -2, 2) * sensitivity; pc = map (mouseX , 0, width , 2, -2) * sensitivity; pd = map (mouseY , 0, height , 2, -2) * sensitivity; oldx = width /2; oldy = height /2; } void populate(int s, boolean c) { //Populate array with density info with s number of samples int samples = s; boolean clear = c; //Clear array or not if (clear) { for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { density[i][j] = 0; previousx[i][j] = 0; } } } for (int i = 0; i < samples; i++) { for (int j = 0; j < 10000; j++) { //De Jong's attractor newx = ((sin (pa * oldy) - cos (pb * oldx)) * N * 0.2) + N/2; newy = ((sin (pc * oldx) - cos (pd * oldy)) * N * 0.2) + N/2; //Smoothie newx += random (-0.001, 0.001); newy += random (-0.001, 0.001); //If coordinates are within range, up density count at its position if ((newx > 0) && (newx < N) && (newy > 0) && (newy < N) ) { density[int (newx)][int (newy)] += 1; previousx[int (newx)][int (newy)] = oldx; } oldx = newx; oldy = newy; } } //Put maximum density and its log()-value into variables for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { if (density[i][j] > maxdense) { maxdense = density[i][j]; logmaxd = log (maxdense); } } } } void updateloop() { //Loops the non-clearing update and plotting to produce low-noise render for (int i = 0; i < 128; i++) { if (stop) { println ("User cancelled" ); break ; } populate(16, false ); plot(0); pi.blend (0, 0, dj.pi.width , dj.pi.height , 0, 0, dj.pi.width , dj.pi.height , SOFT_LIGHT ); redraw (); } } void reparam() { //Fast reparametrization of variables dj.construct(); dj.populate(1, true ); dj.plot(100); redraw (); } PImage plot(int f) { int factor = f; //Plot image from density array pi = createImage (N, N, ARGB ); pi.loadPixels (); for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { if (density[i][j] > 0) { float myhue = map (previousx[i][j], 0, N, 128, 255); float mysat = map (log (density[i][j]), 0, logmaxd, 128, 0); float mybright = map (log (density[i][j]), 0, logmaxd, 0, 255) + factor; color c = color (myhue, mysat, mybright); pi.pixels [i * N + j] = c; } } } pi.updatePixels (); return pi; } }