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 › One conditional and a variable question
Page Index Toggle Pages: 1
One conditional and a variable question (Read 490 times)
One conditional and a variable question
Apr 19th, 2009, 11:06am
 
hi , I got this sketch, it draws ellipses, these ellipses create "lines of ellipses"

Can you explain me in normal english how does the if conditional behaves ?

...

I also dont understand what the rwHop do, and how it is related to Height_of_peaks

I am a newbie, please explain me in a way i can understand. thanks !!!

Here's the code

Code:
void setup(){
size (300,300);
smooth();
background (#FF034F);
frameRate(1);

}

void draw(){

fill(#FF034F,90);
noStroke();
rect(0,0,width,height);




// Declare Variables

int totalpoints =100 ;
float steps = totalpoints + 1;
int totalrows =3;
int space_between_row = height/totalrows;
float height_of_peaks = 1;
float rowhop =0 ;
int randnudge = 8;
int E=8;

noFill();
stroke ( 255);
strokeWeight(2);
// Algorithm



for (int i =space_between_row ; i<height ; i+=space_between_row) {
for ( int j = 1 ; j < steps ; j++){
rowhop-= height_of_peaks;
if (j% ( 1+(int)(random(randnudge))) == 0 ){
height_of_peaks*=-1;



// extra conditionalls




if (mouseX>width/2){
E=+50 ;

}
}
ellipse((width/steps)*j+random(-30,30), i+rowhop,E,E);




}
}
}

Re: One conditional and a variable question
Reply #1 - Apr 19th, 2009, 4:25pm
 
clarifying IF in processing: to compare values in a IF conditional you use == instead of only =. one equal sign denotes that something has a specific value, while == denotes a comparison (if x equals 0 is written if(x == 0){statements}, then. summarizing, when declaring a variable you use x = 0; while when comparing with a if conditional you use if(x == 0), for example.

in this particular conditional if by dividing j by (1+int(random(randnudge))) the rest equals 0 [j % (1+int(random(randnudge))) == 0] the statements inside the brackets are read.

this particular statement modify the Y position of the ellipse on the screen based on a randomly defined comparison (in this case the int(random(randnudge)) which returns a value from 0 to 8 and converts it to int with the int() function to be divided by j and then if it returns no rest the if statements run). if the result returns true height_of_peaks becomes (-height_of_peaks) {if height_of peaks was 1 it becomes -1 and if height_of_peaks was -1 it becomes 1}, incrementing rowhop in the opposite direction as before (rowhop is related to height_of_peaks because inside the for (int j = 1 ; j < steps et cetera) iteration it is stated that rowhop -= height_of_peaks; this way each time this for iteration runs the rowhop value increases or decreases by 1 depending on the result of the if(j%1 + int(...) et cetera) conditional.

rowhop is called on the ellipse() function together with the value of i to define what is the Y position of each ellipse. while the value of i defines in which row the ellipse must be drawn, the rowhop make each ellipse on the same line occupy a slightly different Y pos.
Page Index Toggle Pages: 1