Friday, 5 December 2008

Chunk16 - Source Code

Thanks to Martin, I downloaded JEdit, this makes it much easier to cut and paste the code. After installing JEdit the plug in manager can install codeToHTML.
JEdit adds line numbers too, so this should also help explain the relevant statements in the text.I cant see there being much changes apart from making the filled circles smaller when they are at the top of the picture.
Clicking on the picture will start the randomizer application (If you have a Java runtime environment installed...)

2: title: Funky Chunk 16
3: description: random pattern generator
4: created: December 2, 2008
5: by: Rosie Wood
8:// define Global variable values
9://These are constant variables and therefore they are not changed
10://by the program. Setting their value here in one place helps with maimtenance
11:int sizeWindow=600;
12:int centre=sizeWindow/2;
13:int xpos = centre;
14:int ypos = centre;
15:int patternsPerPicture=round(random(2,50));
17://this variable is changed frequently by the application
18:int interval=30;
20:// set the sketch window size and background
21://turn off fill so that patterns underneath can be seen
22://set the rate that the screen refreshes at (found this value was best for this application?)
23:void setup(){
24: size(sizeWindow, sizeWindow);
25: background(0);
26: stroke(204, 102, 0);//colour red
27: noFill();
28: frameRate(.2);
29: draw();}
31:void draw(){
32: //local variables
33: int count=0;
34: int design=0;
35: for(count=0;count<patternsPerPicture;count++){
36: if(count%100==0)background(0); //set the backgound to black sometimes
37: interval=round(random(5,70));//increment by interval in while loops
38: changeColour();
39: design=round(random(1,9)); //a random numver for which design to use in the switch
40: switch(design){
41: case 1:
42: while(interval<width){ //while intervel is less than the width of the window
43: drawCircle2(interval-round(random(40,width/2)));//call function drawCircle2 with a random parameter
44: interval+=round(random(3,70)); }
45: break;
46: case 2:
47: drawCircles(round(random(20,round(random(1,50))))); //call the drawCircles function
48: break;
49: case 3:
50: strokeWeight(getSW());//set the strokewright to the number returned by the getSW function
51: while(interval<width){
52: drawCircle2(interval-round(random(1,width/2)));//use the drawCircle2 function
53: interval+=round(random(1,width/2));}
54: break;
55: case 4:
56: strokeWeight(getSW());
57: while(interval<width){
58: drawCircle3(interval-round(random(2,width/4)));
59: interval+=round(random(1,30));}
60: break;
61: case 5: //As this case doesnt have a break it uses the same code as case 6
62: //the following code can be uncommented to add lines but I dont like them much
63: /*strokeWeight(getSW());
64: for(int b=1;b<width;b=b+round(random(1,20))){
65: int down=round(random(width/2));
66: int across=round(random(width/2));
67: line(xpos,ypos,across-b,down-b);
68: line(xpos,ypos,across+b+xpos,down-b);
69: line(xpos,ypos,across-b,width-down+b);
70: line(xpos,ypos,across+b+xpos,width-down+b);}
71: break;
72: */
73: case 6:
74: while(interval<width){
75: fill(random(255),random(255),random(255)); //use a random colour to fill circle
76: drawCircle2(interval);
77: interval+=round(random(2,200));}
78: noFill();//switch off fill
79: break;
80: case 7:
81: while(interval<width){
82: drawCircle2(interval);
83: interval+=random(1,40);}
84: break;
85: case 8:
86: while(interval<width){
88: fill(random(255),random(255),random(255));//fill squares with a random colour
89: drawSquares((round(random(1,100))));//call function drawSquares with a random number
90: interval+=round(random(100,400));}//made this interval larger to stop filled squares covering over preceeding patterns
91: noFill();
93: break;
94: case 9:
95: while(interval<width){
96: changeColour();
97: drawSquares(interval-round(random(1,width/2)));//draws unfilled squares
98: interval+=20;}
99: break;
100: }}
101: patternsPerPicture=round(random(2,50));//change number of patterns for next picture
102: }
104://function to change the stroke colour doesnt accept parameter and doesnt return anything
105:void changeColour(){
106: stroke(random(255),random(255),random(255));}
108://function draws circles accepts parameter gap and uses it to increment loop untill
109:void drawCircles(int gap){
110: for(int n=0;n<round(random(width/2,width));n+=gap){
111: drawCircle(n);}}
113://no parameters, no return valur
114:void drawCircle2(int diameter){
115: ellipse(xpos, ypos, diameter-random(5,diameter), diameter-random(5,diameter));}
117://accepts 1 parameter
118:void drawCircle3(int diameter){
119: ellipse(xpos, ypos, (diameter*random(5,diameter))/(random(5,diameter)*19), (diameter*random(5,diameter))/(random(5,diameter)*19) );}
121:void drawSquares(int diameter){
122: int r= round(random(1,4));
123: //if/ifelse/else block
124: if(r==1){
125: rectMode(CENTER);}
126: else if(r==2){
127: rectMode(RADIUS);}
128: else if(r==3){
129: changeColour();}
130: else{
131: stroke(round(random(255)),round(random(255)),round(random(255)),round(random(255)));}
132: rect(xpos,ypos,diameter,diameter);}
134://returns integer valur for stroke weight depending on framecount
135:int getSW(){
136: int fc=round(random(frameRate*500)/5);
137: //case statement with default value
138: switch(fc){
139: case 1:return 1;
140: case 2:return 2;
141: case 3:return 3;
142: case 4:return 4;
143: case 5:return 5;
144: case 6:return 6;
145: case 7:return 7;
146: case 8:return 8;
147: case 9:return 9;
148: default :return round(random(1,4));}}
150:void drawCircle(int diameter){
151: ellipse(xpos, ypos, diameter, diameter);
152: ellipse(xpos, ypos, diameter-interval, diameter);
153: ellipse(xpos, ypos, diameter, diameter-interval);
154: ellipse(xpos, ypos, diameter, diameter);}


  1. If you want to have syntax highlighting you will need to copy the css style into the header of your blog.

  2. Silly me you also need to have setup up the *.pde mode for jEdit as well this I think is explained in the link from my October post, you can even use abbreviations (short-cut) such as #size#200#200# etc.

  3. NOTE
    I must remember to remove line 154 because it doesnt do anything and change sizeWindow and centre to uppercase as they are constants!
    An array will be used for the modifications sections and will likely hold coordinates, height and width for a shape in each of 9 sections and centre. This will be moved to a new function which will be a sizable one. The four corners, four centre squares and centre will be treated as independent groups and circles, aquares or nothing may be drawn to each group. Similar code in the case sections will be factored out and encapsulated in functions.

    change the comment on line 108 as this function is parameterised!