213
edits
Glozt100sob (talk | contribs) |
No edit summary |
||
Line 362: | Line 362: | ||
* [http://processing.org/reference/mouseY.html mouseY] | * [http://processing.org/reference/mouseY.html mouseY] | ||
* [http://processing.org/reference/random_.html random()] | * [http://processing.org/reference/random_.html random()] | ||
== 07.11.2011 Processing sketches == | |||
=== Exercise I: Bouncing Line (Advanced)=== | |||
<source lang="java"> | |||
/* BOUNCING LINE ADVANCED | |||
* | |||
* Notice the introduction of the variables | |||
* "speed" and "direction to control the speed | |||
* and direction of the moving line inependently. | |||
* | |||
* Frederic Gmeiner 2011 | |||
*/ | |||
// variable to hold the currect x position of the line | |||
float lineX = 0; | |||
// variable that controls the speed | |||
float speed = 2.3f; | |||
// variable that controls the direction | |||
// (either 1 or -1) | |||
int direction = 1; | |||
void setup(){ | |||
size(300,300); | |||
} | |||
void draw(){ | |||
background(204); | |||
// if the x position of the line exceeds the right border of the window | |||
// OR (||) the x position of the line exceeds the left border | |||
// of the window: | |||
if(lineX > width || lineX < 0){ | |||
direction = direction * -1; // change the direction | |||
} | |||
// here the new x-position is calculated by adding the speed and direction onto the | |||
// current x-position. if direction is -1, lineX decreases. if direction is 1, lineX | |||
// increases: | |||
lineX = lineX + (speed * direction); | |||
// draw the line based on the calculated x-position: | |||
line(lineX,0,lineX,height); | |||
} | |||
</source> | |||
<br /><br /> | |||
=== Exercise II: Solution=== | |||
<source lang="java"> | |||
/* A FRIGHTENED SQUARE | |||
* | |||
* A little sketch to illustrate the random() function | |||
* and a more complex boolean equation to check whether | |||
* the mouse pointer is inside a rectangular shape. | |||
* | |||
* Frederic Gmeiner 2011 | |||
*/ | |||
// the x an y positions of the square: | |||
float xPos = 200f; | |||
float yPos = 200f; | |||
// the width and height of the square: | |||
int s = 20; | |||
void setup(){ | |||
size(400,400); | |||
} | |||
void draw(){ | |||
background(204); | |||
// Here we check if the position of the mouse is somewhere inside of the | |||
// square. To know this, all these four contitions must be true ( && ): | |||
if( mouseX > xPos && mouseX < xPos + s && mouseY > yPos && mouseY < yPos + s){ | |||
xPos += random(-2,3); // add a random value between -2 and 3 to the xPos | |||
yPos += random(-2,3); | |||
fill(40); | |||
}else{ | |||
fill(255); | |||
} | |||
// finally draw the square: | |||
rect(xPos, yPos, s, s); | |||
} | |||
</source> | |||
<br /><br /> | |||
=== V for() loops=== | |||
<source lang="java"> | |||
/* DYNAMIC DISTRIBUTION | |||
* | |||
* This sketch demonstrates the usage of | |||
* a for-loop for calculating and drawing | |||
* a certain number of ellipses according | |||
* to the changing mouseX position. | |||
* | |||
* Frederic Gmeiner 2011 | |||
*/ | |||
// the number of circles: | |||
int numCircles = 15; | |||
void setup(){ | |||
size(500,200); | |||
stroke(255); | |||
fill(150); | |||
smooth(); | |||
} | |||
void draw(){ | |||
background(255); | |||
// calculate the distance between the circles | |||
// so that in total it results in the mouseX position: | |||
float distance = mouseX / (numCircles-1); | |||
// for every circle (numCircles) calculate the xPos | |||
// and draw it onto the screen: | |||
for(int i=0; i < numCircles; i++){ | |||
float xPos = i*distance; | |||
ellipse(xPos,100,10,10); | |||
} | |||
} | |||
</source> | |||
<br /><br /> | |||
=== VI The array: a collection of variables=== | |||
<source lang="java"> | |||
/* Random points in an array | |||
* | |||
* This demonstrates how to store values in | |||
* an array. This is useful when working with | |||
* many things which share the same parameters. | |||
* Here we use two arrays to store the coordinates | |||
* of circles. | |||
* | |||
* Frederic Gmeiner 2011 | |||
*/ | |||
// total number of circles: | |||
int numCircles = 40; | |||
// the radius of the circles | |||
float rad = 20f; | |||
// initializing two arrays for storing the x and y positions | |||
// of the circles: | |||
float[] xPositions = new float[numCircles]; | |||
float[] yPositions = new float[numCircles]; | |||
void setup(){ | |||
size(500,500); | |||
smooth(); | |||
noStroke(); | |||
fill(150); | |||
// iterating over the arrays to set each position with | |||
// a random value based on the screen size: | |||
for(int i=0; i< numCircles; i++){ | |||
xPositions[i] = random(0,width); | |||
yPositions[i] = random(0,height); | |||
} | |||
} | |||
void draw(){ | |||
background(204); | |||
// again iterating over the arrays. but this time we only | |||
// get the x and y coordinates to draw the circles: | |||
for(int i=0; i< numCircles; i++){ | |||
ellipse(xPositions[i], yPositions[i], rad, rad); | |||
} | |||
} | |||
</source> | |||
<br /><br /> | |||
[[Category:WS11]] | [[Category:WS11]] |
edits