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 & HelpPrograms › Random alpha'd ellipses
Page Index Toggle Pages: 1
Random alpha'd ellipses ? (Read 1007 times)
Random alpha'd ellipses ?
Apr 21st, 2010, 10:51am
 
I want to have all the ellipses in my grid with a different alpha each one.

so when they overlap they form new shapes based onthey overlapped parts.

like int he muck up :
...

a good idea would also be to restrict the random alpha values to 4 , like 15,25,35,65  for examle , so now it would pick randomly from these 4 options.


And one last question how to make the sketch show frames slower than 1 frame per second , for example ,1 frame per 3 seconds ?


thanx ! Smiley Smiley Smiley Smiley Smiley Smiley Smiley Smiley Smiley Smiley Smiley Smiley


and my code

Code:

int largo = 80;
int r = 0;
int g = 0 ;
int b = 0;
//int unit = width/10;
void setup() {
smooth();
size(800,800);
frameRate(2);

}

void draw()
{
float r = random(255);
float r2 = random(255);

for (int x = largo/2; x < width; x+= width/10-largo/2)
{

for (int y = largo/2; y < height; y+= width/10-largo/2)
{
noStroke();
fill(int( r));
fill(int( r));
ellipse(x,y,largo,largo);


}

}

}
Re: Random alpha'd ellipses ?
Reply #1 - Apr 21st, 2010, 12:18pm
 
One way of getting a random selection from a specific list of numbers is to put the list in an array and use random(yourArray.length) to choose the index of the item in the array (remembering that the index needs to be an int).

Strangely enough your second request is one circumstance where you could almost justify using delay(); just bear in mind that this impacts on when draw refreshes the screen and it may be problematic if you need other things to happen at a faster framerate.  The alternative and probably safer option would be to use a timer (see millis() or just search the site) to determine when the ellipse draw update code is run...
Re: Random alpha'd ellipses ?
Reply #2 - Apr 22nd, 2010, 3:44am
 
cool. thanx !


but what about hasving everyone alpha´d different ?

not that it just takes one random value for all ?

thanx !
Re: Random alpha'd ellipses ?
Reply #3 - Apr 22nd, 2010, 7:37am
 
Put the alpha values in an ArrayList.
Take the value at index taken between 0 and arrayList.size()-1
Remove this value from the array list: thus you draw the next value in the smaller list, and are sure not to draw again previous values.
Repeat.
Re: Random alpha'd ellipses ?
Reply #4 - Apr 22nd, 2010, 12:34pm
 
awesome guys , i got it working !

you can check it out here on openprocessing :

http://openprocessing.org/visuals/?visualID=9080

but i have one more question ,

i want to set largo as width/10 , so i can freely scale the size of the windoe without having to change largo

but if i run this code wich has largo as width/10 instead of a hard coded number , it wont run, any ideas

Code:
int[] als = {10,40,60,80,90,140 };
//int s = second();
int largo = width/10;// try changing width/10 for 40 , and it will work again
void setup() {
smooth();
size(400,400);

frameRate(2);

}

void draw()
{
background (255);
for (int x = largo/2; x < width; x+= width/10-largo/2)
{

for (int y = largo/2; y < height; y+= width/10-largo/2)
{
noStroke();
fill (0,als[ int( random (6) ) ] ) ;

ellipse(x,y,largo,largo);

}

}

}
Re: Random alpha'd ellipses ?
Reply #5 - Apr 22nd, 2010, 2:10pm
 
The main problem you're having is that you're trying to reference 'width' before setup has run and that's a really bad idea since IIRC it doesn't get set till after...  Anyway the answer is simple enough: You can declare the variable before setup so it's 'global' (i.e. can be referenced in draw()) and assign it a value in setup after size() has been called:

Code:
int[] als = {10,40,60,80,90,140 };
//int s = second();
// declare the variable as global
int largo;

void setup() {
 // size() should *always* come first:
 size(800,800);
 // assign the value to the variable
 largo = width/10;
 
 smooth();  
 frameRate(2);
}

void draw(){
 background (255);
 for (int x = largo/2; x < width; x+= width/10-largo/2){
   for (int y = largo/2; y < height; y+= width/10-largo/2)  {
     noStroke();  
     fill (0,als[ int( random (6) ) ] ) ;
     ellipse(x,y,largo,largo);
   }
 }
}


Mind you I think you may still have problems with some values as it looks like your draw loop contains a few magic number - e.g. width/10

Replacing the 10 with a variable, both in the assignment of largo and in those width/10 references, seems to work regardless of the value used (though it gets a bit slow if you set it too high):

Code:
int[] als = {10,40,60,80,90,140 };
//int s = second();
// declare the variable as global
int largo;
int divisor = 30;

void setup() {
 // size() should *always* come first:
 size(800,800);
 // assign the value to the variable
 largo = width/divisor;
 
 smooth();  
 frameRate(2);
}

void draw(){
 background (255);
 for (int x = largo/2; x < width; x+= width/divisor-largo/2){
   for (int y = largo/2; y < height; y+= width/divisor-largo/2)  {
     noStroke();  
     fill (0,als[ int( random (6) ) ] ) ;
     ellipse(x,y,largo,largo);
   }
 }
}


Page Index Toggle Pages: 1