We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › square pattern generator
Page Index Toggle Pages: 1
square pattern generator (Read 887 times)
square pattern generator
Jul 14th, 2007, 8:41pm
 
hi everybody,
I would like to built a sketch with proce55ing, but my limited knowledge doesn't allow me to solve a problem so i ask you a suggestion.

My goal is to have a generator of random squares that cover the entire stage, to obtain a pattern.
I succeded in building two growing crosses that stop when they collide.

I would like to have an higher number of crosses, repeting the same behaviour.
I've worked in a crafting way to built the first two crosses, so, is there a way to multiply this objects or do i have to choose another metod?

I'm afraid i should use classes or array, but i'm not able and i have not enough time to spent for learning.


thanks for any suggestion.

Quote:



float aar = random(200);
int ar = int(aar);
float bbr = random(200);
int br = int(bbr);

int cr = ar;
int dr = br;

int er = ar;
int fr = br;

int gr = ar;
int hr = br;


float aar2 = random(200);
int ar2 = int(aar2);
float bbr2 = random(200);
int br2 = int(bbr2);

int cr2 = ar2;
int dr2 = br2;

int er2 = ar2;
int fr2 = br2;

int gr2 = ar2;
int hr2 = br2;


void setup()
{
 size(200, 200);
 background(200,0,0);
 fill(0);

}



void draw()
{


ar++;
 point(ar, br);
 color a1 = get(ar+1, br);
 color a2 = get(ar, br);
 if(a1 == a2) {
   ar=ar-1;
 }
cr--;
 point(cr, dr);
 color b1 = get(cr, dr-1);
 color b2 = get(cr, dr);
 if(b1 == b2) {
   cr=cr+1;
 }

fr++;
 point(er, fr);
 color c1 = get(er, fr+1);
 color c2 = get(er, fr);
 if(c1 == c2) {
   fr=fr-1;
 }


hr--;
 point(gr, hr);
 color d1 = get(gr, hr-1);
 color d2 = get(gr, hr);
 if(d1 == d2) {
   hr=hr+1;
 }
 
 
ar2++;
 point(ar2, br2);
 color e1 = get(ar2+1, br2);
 color e2 = get(ar2, br2);
 if(e1 == e2) {
   ar2=ar2-1;
 }
cr2--;
 point(cr2, dr2);
 color f1 = get(cr2, dr2-1);
 color f2 = get(cr2, dr2);
 if(f1 == f2) {
   cr2=cr2+1;
 }

fr2++;
 point(er2, fr2);
 color g1 = get(er2, fr2+1);
 color g2 = get(er2, fr2);
 if(g1 == g2) {
   fr2=fr2-1;
 }


hr2--;
 point(gr2, hr2);
 color h1 = get(gr2, hr2-1);
 color h2 = get(gr2, hr2);
 if(h1 == h2) {
   hr2=hr2+1;
 }
 
}


Re: square pattern generator
Reply #1 - Jul 18th, 2007, 9:54am
 
Haven't given much thought to it, but here's a not-so-intelligent way to do the crosses thing.

if your stage size is 200x200, then create a 2D array of 200x200 size, for storing pixel positions. All elements in the array should be 0.

Next, initialize a class which has values like rightValue, leftValue, upValue, dnValue to store the value of the length that the cross has progressed in the four directions. You'll also need variables to store the x & y origin points of the crosses. (generate them randomly)
The number of objects you create with this class, will be the number of crosses you want.

Now just have a for loop which cycles through these classes and increases the length of the crosses in the right, left, up and down directions, by marking a '1' in the 200x200 array. When the next position to put a '1' is already a '1', it means you've hit another cross, and the current arm of the cross gets disabled.

The pixels are drawn only wherever the '1' is seen in the array.
Re: square pattern generator
Reply #2 - Jul 18th, 2007, 10:29am
 
thanks a lot, This will be a good moment to learn Arrays and Classes.

thanks
Re: square pattern generator
Reply #3 - Jul 18th, 2007, 6:34pm
 
To further simplify things, consider that your "cross" could be considered as 4 independent straight line walkers.

So create a class that contains x,y,dx,dy and knows how to move like x+=dx; y+=dy; then check if it's on top of a pixel already plotted and stop if so.

To spawn a "cross" simply create 4 walkers at the same x/y with all combinations of +/-1 dx/dy.

So your class is 4 times simpler, but you have 4 times as many instances.  Make sense?
Page Index Toggle Pages: 1