User Tools

Site Tools


processing:compass_example

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
processing:compass_example [2013/08/03 03:39] mithatprocessing:compass_example [2015/08/01 21:11] (current) – [Refining randomness] mithat
Line 96: Line 96:
  
 ===== Adding randomness ===== ===== Adding randomness =====
 +
 +[[http://mithatkonar.com/processing/compass/compass_random/|{{:processing:processing_compass_random.png?300}}]]
  
 A random offset added to a fixed direction can begin to suggest something like blowing wind, which will have small essentially random variations in direction in time over a finite range. A random offset added to a fixed direction can begin to suggest something like blowing wind, which will have small essentially random variations in direction in time over a finite range.
 +
 +<code java>
 +/**
 + * Compass example:
 + * Show due north with random offsets.
 + 
 + * Copyright (C) 2013 Mithat Konar
 + */
 +
 +float ang;
 +
 +void setup() {
 +  frameRate(30);
 +  smooth();
 +  size(400, 300);
 +  ang = 0;
 +}
 +
 +void draw() {
 +  background(#ffffff);
 +  
 +  // draw the compass at "ang" degrees plus or minus 0-4 random degrees
 +  drawCompass(ang + random(-4.0,4.0), 20, 200, width/2, height/2);
 +}</code>
  
 ===== Refining randomness ===== ===== Refining randomness =====
  
-The above, while random, appears more jittery than a true natural processThis is because the random offsets are allowed to jump directly over the entire range that the indicator vacillates within.+[[http://mithatkonar.com/processing/compass/compass_random_accumulated/|{{:processing:processing_compass_random_accumulated.png?300}}]]
  
-A random offset process can be smoothed out by //accumulating small random offsets// rather than jumping completely randomly over some rangeThe small random offsets will average out to zero in the long term but prevent the indicator from jumping too far between any two frames. On the other hand, it's possible for the total offset to temporarily exceed the desired bounds.+The offsets in the "Adding randomness" example above appear noticeably more jittery than a true natural processThis is because the random offsets are allowed to jump directly over the entire range that the indicator bounces within.
  
 +A random offset process can be smoothed out by //accumulating small random offsets// rather than jumping completely randomly over some range. The small random offsets will average out to zero in the long run, but they will prevent the indicator from jumping too far between any two frames. The downsides are that it's possible for the total offset to temporarily exceed the desired bounds and that sometimes the accumulated offset can become so large that it never seems to return to zero.
 +
 +<code java>
 +/**
 + * Compass example:
 + * Show due north with accumulated random offsets.
 + 
 + * Copyright (C) 2013 Mithat Konar
 + */
 +
 +float ang;
 +
 +void setup() {
 +  frameRate(30);
 +  smooth();
 +  size(400, 300);
 +  ang = 0;
 +}
 +
 +void draw() {
 +  background(#ffffff);
 +
 +  // draw the compass
 +  drawCompass(ang, 20, 200, width/2, height/2);
 +
 +  // and then add some random offset to the angle for next time
 +  ang += random(-4.0, 4.0);
 +
 +  // and wrap around to 360 degrees
 +  ang %= 360;
 +}</code>
 ===== Perlin noise ===== ===== Perlin noise =====
  
-[[https://en.wikipedia.org/wiki/Perlin_noise|Perlin noise]]+[[http://mithatkonar.com/processing/compass/compass_noise/|{{:processing:processing_compass_noise.png?300}}]] 
 + 
 +[[https://en.wikipedia.org/wiki/Perlin_noise|Perlin noise]] is a **mapping function** that was designed to simulate natural random variations. It's called a mapping function because unlike Processing's ''[[http://processing.org/reference/random_.html|random]]'' function, which returns a new random value over a specified range each time you call it, Perlin noise has an input and an output. The input is some point in N-dimensional space, and the output is some value. Every time you invoke a Perlin noise function with the same input, you'll get the same output. If you shift the input slightly, the output value changes, and these changes are very similar to the kinds of changes found in nature. 
 + 
 +Processing has a function called ''[[http://processing.org/reference/noise_.html|noise]]'' that implements Perlin noise. We use the one-dimensional version of the ''noise'' function here to determine the offset of the indicator. 
 + 
 +<code java> 
 +/** 
 + * Compass example: 
 + * Show due north with Perlin noise offset. 
 + * 
 + * Copyright (C) 2013 Mithat Konar 
 + */ 
 + 
 +float ang; 
 +float noiseOffset;              // noise function's "x-value" 
 +final float NOISESCALE = 0.03;  // amount to move along noise function'
 +                                // "x-axis" each frame 
 +final float MAXOFFSET = 45;     // the maximum possible generated angle offset 
 + 
 +void setup() { 
 +  frameRate(30); 
 +  smooth(); 
 +  size(400, 300); 
 +  ang = 0; 
 +   
 +  // start the noise's "x-value" at some random place  
 +  noiseOffset = random(0, 1000); 
 +
 + 
 +void draw() { 
 +  float angleOffset;
  
 +  background(#ffffff);
 +  
 +  // increment the noise's "x-value" by a small amount
 +  noiseOffset += NOISESCALE;
  
 +  // calculate a noisy angle offset based on noise's "y-value"
 +  angleOffset = (2*MAXOFFSET) * (noise(noiseOffset)-0.5);
 +  
 +  // draw the compass
 +  drawCompass(ang+angleOffset, 20, 200, width/2, height/2);
 +}</code>
processing/compass_example.1375501192.txt.gz · Last modified: 2013/08/03 03:39 (external edit)

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki