Friday, 3 April 2009

Ch46 - An Array of Difficult Choices

After careful consideration of how to implement the choices available for pattern 7 we decide to use an array to hold the available choices.
boolean [] segments={false,false,false,false,false,false,false,false};
If we allowed any choices at all for pattern 7 what would be the outcome? We decide to try this out and implement a function to use our array. We need to reset the arrays values to false when we start to draw a pattern 7 design. Then we must randomly decide which segments to draw.

void oops(){
resetSegs();
for(int n=0;n<8;n++){
if(round(random(1))==0){segments[n]=true;}
}
println(segments);
if(segments[0]==true){
line(0,height-y,y,y);}//oopsleft down
if(segments[1]==true){
line(width-x,0,x,x);}//oopsup right
if(segments[2]==true){
line(x,0,width-x,x);}//oopsup left
if(segments[3]==true){
line(width,y,y,height-y);}//oopsright down
if(segments[4]==true){
line(width,height-y,y,y);}//oopsrightup
if(segments[5]==true){
line(width-x,height,x,x);}//oopsdown left
if(segments[6]==true){
line(x,height,width-x,x);}//oopsdown right
if(segments[7]==true){
line(0,y,y,height-y);}//oopsleft up

}
void resetSegs(){
for(int n=0;n<segments.length;n++){>
segments[n]=false;
}
}
When we run our application with this function, every time our function executes, all the segments are drawn. On careful inspection, we realise that resetting the array and filling in the choices should not be within the function or the for loops. We move the offending code from the function to the start of the while loop:
while(count<patsperpic){
resetSegs();
for(int n=0;n<8;n++){
if(round(random(1))==0){segments[n]=true;}
}
changeColour();

An example of running our application with no limits on the choices for pattern 7, is as shown in the images above. We may decide that this is acceptable or not...
The patterns used in Chunk 46 include 4 different meshes, which we may feel don't merit a case statement each. If we moved this code to one case statement then we would have more patterns and less meshes.
Example Source Code with space for 2 new patterns
The next section is optional and involves adding new patterns from another program's for loop.

No comments:

Post a Comment