Thursday 2 April 2009

Chunk 46 - Structure

In this section we will move our patterns into functions and decide how each function will control the symmetry of its own pattern.
We want to make our program as easy to manage as possible so we decide we will have a global variable that will control the amount of lines each statement will use to be drawn to the screen. As the size of the pixels is different vertically and horizontally we will use two for next loops to contain the pattern controlling.
Here is a list of the variables we might end up with:

int numLines=20;//changing this controls the amount of lines in a statement
int stepH=0; //used in for loop for vertical steps
int stepW=0; //used in for loop for horizontal steps
int n=0; //allows the value of n to be accessed from the functions
int x=0; //horizontal step
int y=0; // vertical step
int rannum=0; //a random number between 0 and 2
int design=1; //an integer to identify the case statement to execute
int count=0; //used to compare with the number of patterns per picture
int drawCount=0; //counts how many patterns have been displayed so far

Here is the basic structure of the application:

void setup(){
size(400, 400);
stepH=height/numLines;
stepW=width/numLines;
strokeWeight(1);
smooth();
frameRate(30);
}

void draw(){
patsPerPic=round(random(2,7));
background(0);
count=0;
n=0;
while(count&ltpatsperpic){>
changeColour();
rannum=round(random(2));
design=round(random(2));


for(x=0;x <width+1;x=x+stepw){
for y=0;y<height+1;y+=stepH){">



switch(design){
case 0: function0(); break;
case 1: function1(); break;
case 2:function2();break;
} } }//end of fors
count++;
}//end of while
drawCount++;
}//end of draw

void changeColour(){
stroke(random(255),random(255),random(255),random(255));
}

void function0(){
line(whatever);
}
void function1(){
line(whatever);
}
void function2(){
line(whatever);
}

Lets look at one of our functions and how the value of rannum decides which patterns are drawn.

void bigArches(){
//rannum=0;
if(rannum==1||rannum==0){
line(x,0,width,x);//large mesh ur
line(0,y,y,width);}
if(rannum==2||rannum==0){
line(0,height-y,y,0);//large mesh u l
line(width,height-y,y,height);}
}

The way we have designed this function, allows 3 choices. If the value of rannum is zero, it can be seen that both if statements are executed. A value of 1 or two only executes one of the if statements. If we wanted the arches to be totally symmetrical, we could set the value of rannum to zero within the function.
If we are working on a function, then we can replace the design variable in the switch, with the number of the case statement we are working on.
e.g.
switch(3){
To add a function to the basic structure we would add the function, give it a case statement and increase the size of the random number used to choose the design. If a pattern only uses one statement there is little benefit in moving it to a function. How we decide the options availablr for each pattern is a matter of personal taste. Most of the functions will have a structure similar to the bigArches function above but you may decide that a pattern is too trivial to be shown on its own and so give it no options e.g.
void inwards(){
line(0,0,height-y,y);//inwards l d
line(width,height,y,height-y);//inwards r u
line(0,height,y,y);//inwards l u
line(width,0,y,y);//inwards r d
}
This means that when this design is chosen it will always show the four statements in its pattern,
Some statenents in a pattern could be shown on their own, in a pair or as one of four. This gives 7 choices and this is easy to implemrnt by choosing a random number in the function. e.g.
void butterfly(){
int rannum=round(random(6));
if(rannum==1||rannum==5||rannum==0)
line(x,height-y,width-x,0);
if(rannum==2||rannum==5||rannum==0)
line(x,y,width-x,height);
if(rannum==3||rannum==6||rannum==0)
line(width,width-y,x,y);
if(rannum==4||rannum==6||rannum==0)
line(0,y,x,height-y);
}
However, you may decide that you dont like the symmetry of the designs produced when only one statement of the four is used in a pattern and just use the rannum variable to control the oppositely located pairs of statements.
void butterfly(){
if(rannum==1||rannum==0){
line(x,height-y,width-x,0);
line(x,y,width-x,height);}
if(rannum==2||rannum2==0){
line(width,width-y,x,y);//butterfly r
line(0,y,x,height-y);}}
As was demonstrated in the last section the amount of choices available when there are eight possible statements in a pattern is considerable. When implementing such a design, we may decide to simplify it, if it is not bringing anything worthwhile to the program. e.g.
void triangle3(){
if(rannum==1||rannum==0){
line(0,0,y,height);//ray uld
line(0,0,width,height-y);//ray ulr
line(width-x,0,width,height);//ray d r u
line(0,y,width,height);//ray d r l
}
if(rannum==2||rannum==0){
line(width,0,0,height-y);//ray u r l
line(width,0,height-y,height);//ray u r d
line(0,height,width-x,0);// dl u
line(0,height,width,height-y);//ray d l r
}}

On the other hand we may decide that it is worth the added complexity if it brings some quality to our application.
Implementing pattern 7 will warrant some careful consideration.

No comments:

Post a Comment