Showing posts with label randomizer. Show all posts
Showing posts with label randomizer. Show all posts

Sunday, 7 December 2008

Variables First Draft

In programming everything has to have a unique identifier, the processor will remember the values stored for each variable but will we?
Giving variables significant names really cuts down on maintenance and the amount of commenting needed. The first character in a name should usually be a letter. Numbers and symbols wont work. An underscore can be used but this should be used to denote special cases. As long as the first character is a letter though, we will not go wrong. Its traditional for normal variables to start with a lowercase letter and to capitalise the first letter of each subsequent word making up the name. e.g.
account
goldAccount
superDuperAccount
This is known as camelCase format
Variable values are usually 'variable' and are changed within the program, but occasionally variables whose values dont change are needed. These are called constants because their value is constant and never changes. Constants are usually uppercase e.g.
MAXROWS
MINHEIGHT
NUMMONSTERS
Later on we will be using a structure known as a class, classes are used to define object types and they should always start with an uppercase letter. e.g:
Shape
Room
Person
We use built in classes to specify each variable's type and the most used types are String, int, float and boolean: e.g.:
String name;
int width;
float amount;
boolean isVisible;
Variable types need to be declared before the program starts. Later their values need to be declared e.g.:
name="circle";
width=800;
They can also be declared and initialised at the same time:
int float=0.01;
boolean isVisible=false;
Giving a variable a value is known as assignment, and always uses the = operator.
You must be very careful if you are comparing values to make sure that you use == and not = as they have very different consequences!
a=6+4; (result 10)
a==6+4 (result true or false-depending on the value of a, if a = 10 then true)
When comparing strings it is usual to use the equals() method for comparison. This is because == will only be true if the strings compared belong to the same object. If we have two strings a and b and they both have the same value then they will not be equal when using ==.
a="value";
b="value";
a==b result is false
a.equals(b) result is true
The scope of a variable affects where it is recognised.
Global variables are declared at the start of the program and can therefore be used in any code block. The variables in lines 11-18 are global.
Variables declared within a code block are known as local variables and are only usable within that block. The local variables declared on lines 33 and 34 are usable at any point within the draw function. None of the other functions are aware of their existence.
Local variables, declared within different blocks, may have the same identifier, but they will refer to different objects.
The integer n in the drawCircles(int) function is declared within a for loop. Line 110. If we were to try using it anywhere outside the for block, it would not be recognised. If we wanted to access its value in the drawCircles(int) function we would need to declare it outside the for block. eg:

void drawCircles(int gap){
int n;
for(n=0;n< round(random(width/2,width));n+=gap){
drawCircle(n); }
println("Final value of n: "+n);
}

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...)





  1:/*
2: title: Funky Chunk 16
3: description: random pattern generator
4: created: December 2, 2008
5: by: Rosie Wood
6:*/
7:
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));
16:
17://this variable is changed frequently by the application
18:int interval=30;
19:
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();}
30:
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){
87:
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();
92:
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: }
103:
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));}
107:
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);}}
112:
113://no parameters, no return valur
114:void drawCircle2(int diameter){
115: ellipse(xpos, ypos, diameter-random(5,diameter), diameter-random(5,diameter));}
116:
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) );}
120:
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);}
133:
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));}}
149:
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);}

Wednesday, 3 December 2008

So Simple to Program!



I coded the basic outline of the program for chunk 16. This only took a few hours to write and its amazing the amount of work achieved with very little research.
The code is in a right mess and needs to be commented and edited.
I will need to ensure that it conforms to the 100 lines constraint.
I dont think the comments are included in the line count but I will check.
The application draws a random pattern about every second. Colour, stroke weight and pattern function are completely randomized and this makes the application very interesting to watch. I could end up looking at it for hours!
The code is very simple and I have tried to keep it consistent with what the user will know after reading up to page 192.
I have only used the ellipses and rect functions so far but I may add some triangle and line functions if they are an improvement.
I discovered that the content of the draw function is drawn to the screen when the function has ended and I couldnt find an easy way of drawing a line at a time, that would fit in with the rest of the application.
This is no great disadvantage, as by using a for next loop the program can run many functions before each frame is drawn to the screen.
I couldn't get over how easy it was to put the program up on my web site. The viewers can watch the applet, run the standalone application or download a copy of the standalone, so they can run it totally separate from a browser and offline.
The applet runs with no problems in Internet Explorer but there is a permission problem when using FireFox. I'm not sure if this could be a problem with an absolute URL as it objects to host 'solundrian.com'. I'll try fixing this today...
I think it will be quite easy at some point to adjust the application, allowing the user to save some of their favourite pics so they can be viewed later.
You can see the finished randomizer here.