IFD:PhysicalComp2011: Difference between revisions

From Medien Wiki
Line 566: Line 566:


== 14.11.2011 Processing sketches ==
== 14.11.2011 Processing sketches ==
=== EXERCISE III: SOLUTION ===
<source lang="java">
/* PULSING CIRCLES
*
* Mofification of the example above so that
* the circles will shrink and grow by using
* a sinewave.
*
* Frederic Gmeiner 2011
*/
int numCircles = 40;
float rad = 20f;
// initial angle of 0. this will increase during the
// runtime of the programm and will be used to calculate
// the sine based on this value in order so modify the size of
// circles.
float angle = 0.0f;
// how fast the angle will increase is defined by the speed variable:
float speed = 0.01;
float[] xPositions = new float[numCircles];
float[] yPositions = new float[numCircles];
void setup(){
  size(500,500);
  smooth();
  noStroke();
  fill(150);
 
  for(int i=0; i< numCircles; i++){
    xPositions[i] = random(0,width);
    yPositions[i] = random(0,height);
  }
}
void draw(){
  background(204);
  // add the speed value to the angle:
  angle += speed;
 
  // calculate the diamater by calculating the sine value of the
  // angle and scaling it up by the value of 30. This is necessary since
  // the sin() funtion returns only values between -1 and 1.
  float diameter = sin(angle)*30;
  for(int i=0; i< numCircles; i++){
    // draw the circles:
    ellipse(xPositions[i], yPositions[i], diameter, diameter);
  }
}
</source>
<br /><br />
=== VII.a Another sine and cosine example ===
<source lang="java">
/* ELLIPTICAL MOTION
*
* A little demonstration of the sine and
* cosine relation.
*
* Frederic Gmeiner 2011
*/
float speed = 0.1f;  // frequency
float angle = 0.0f; // you might also call it theta
void draw(){
 
  background(200);
  angle += speed;
 
  point(50,50);
  point(50+ (cos(angle)/2*50), 50+ (sin(angle)*50));
}
</source>
<br /><br />
=== VII.b Relation of degrees, PI and sine ===
<source lang="java">
int degr= 0;
println("0° Degrees:");
println("PI:" + radians(degr) / PI);
println("sine:" + round(sin(radians(degr))) + "\n");
degr= 90;
println("90° Degrees:");
println("PI:" + radians(degr) / PI);
println("sine:" + round(sin(radians(degr))) + "\n");
degr= 180;
println("180° Degrees:");
println("PI:" + radians(degr) / PI);
println("sine:" + round(sin(radians(degr))) + "\n");
degr= 270;
println("270° Degrees:");
println("PI:" + radians(degr) / PI);
println("sine:" + round(sin(radians(degr))) + "\n");
degr= 360;
println("360° Degrees:");
println("PI:" + radians(degr) / PI);
println("sine:" + round(sin(radians(degr))) + "\n");
</source>
<br /><br />


=== READING DATA FROM A TEXTFILE ===
=== READING DATA FROM A TEXTFILE ===