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:21] mithatprocessing:compass_example [2015/08/01 21:11] (current) – [Refining randomness] mithat
Line 1: Line 1:
-====== Compass example ======+====== Of Compasses and Randomness ====== 
 + 
 +{{:processing:processing_compass.png?nolink&100|}}
  
 The following uses a simple compass to explore how randomness and noise can make representations seem more natural. The following uses a simple compass to explore how randomness and noise can make representations seem more natural.
Line 12: Line 14:
  
 <WRAP center round info 60%> <WRAP center round info 60%>
-The examples here were developed with Processing 2.0.1. The live examples use Processing.js 1.4.1+The examples here were developed with [[http://processing.org/|Processing 2.0.1]]. The live examples use [[https://s3.amazonaws.com/github/downloads/processing-js/processing-js/processing-1.4.1.min.js|Processing.js 1.4.1]].
 </WRAP> </WRAP>
  
 ===== A simple compass function ===== ===== A simple compass function =====
- 
-{{:processing:processing_compass.png?nolink&100|}} 
  
 Throughout all the examples on this page, we will use the following function to implement a simple compass: Throughout all the examples on this page, we will use the following function to implement a simple compass:
Line 62: Line 62:
 ===== Sweeping the indicator ===== ===== Sweeping the indicator =====
  
-[[http://mithatkonar.com/processing/compass/compass_sweep/|{{:processing:processing_compass_sweepFOO.png?300}}]]+[[http://mithatkonar.com/processing/compass/compass_sweep/|{{:processing:processing_compass_sweep.png?300}}]]
  
 Here we use the ''drawCompass'' function defined above to sweep the indicator through 360 degrees: Here we use the ''drawCompass'' function defined above to sweep the indicator through 360 degrees:
Line 95: Line 95:
 }</code> }</code>
  
 +===== 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.
 +
 +<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 =====
 +
 +[[http://mithatkonar.com/processing/compass/compass_random_accumulated/|{{:processing:processing_compass_random_accumulated.png?300}}]]
 +
 +The offsets in the "Adding randomness" example above appear noticeably more jittery than a true natural process. This 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 =====
 +
 +[[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's
 +                                // "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.1375500066.txt.gz · Last modified: 2013/08/03 03:21 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki