processing:compass_example
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
processing:compass_example [2013/08/03 03:39] – mithat | processing:compass_example [2015/08/01 21:11] (current) – [Refining randomness] mithat | ||
---|---|---|---|
Line 96: | Line 96: | ||
===== Adding randomness ===== | ===== Adding randomness ===== | ||
+ | |||
+ | [[http:// | ||
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(# | ||
+ | | ||
+ | // draw the compass at " | ||
+ | drawCompass(ang + random(-4.0, | ||
+ | }</ | ||
===== Refining randomness ===== | ===== Refining randomness ===== | ||
- | The above, while random, appears 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 vacillates within. | + | [[http:// |
- | A random offset process can be smoothed out by // | + | The offsets |
+ | A random offset process can be smoothed out by // | ||
+ | |||
+ | <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(# | ||
+ | |||
+ | // draw the compass | ||
+ | drawCompass(ang, | ||
+ | |||
+ | // and then add some random offset to the angle for next time | ||
+ | ang += random(-4.0, | ||
+ | |||
+ | // and wrap around to 360 degrees | ||
+ | ang %= 360; | ||
+ | }</ | ||
===== Perlin noise ===== | ===== Perlin noise ===== | ||
- | [[https:// | + | [[http:// |
+ | |||
+ | [[https:// | ||
+ | |||
+ | Processing has a function called '' | ||
+ | |||
+ | <code java> | ||
+ | /** | ||
+ | * Compass example: | ||
+ | * Show due north with Perlin noise offset. | ||
+ | * | ||
+ | * Copyright (C) 2013 Mithat Konar | ||
+ | */ | ||
+ | |||
+ | float ang; | ||
+ | float noiseOffset; | ||
+ | final float NOISESCALE = 0.03; // amount to move along noise function' | ||
+ | // " | ||
+ | final float MAXOFFSET = 45; // the maximum possible generated angle offset | ||
+ | |||
+ | void setup() { | ||
+ | frameRate(30); | ||
+ | smooth(); | ||
+ | size(400, 300); | ||
+ | ang = 0; | ||
+ | |||
+ | // start the noise' | ||
+ | noiseOffset = random(0, 1000); | ||
+ | } | ||
+ | |||
+ | void draw() { | ||
+ | float angleOffset; | ||
+ | background(# | ||
+ | | ||
+ | // increment the noise' | ||
+ | noiseOffset += NOISESCALE; | ||
+ | // calculate a noisy angle offset based on noise' | ||
+ | angleOffset = (2*MAXOFFSET) * (noise(noiseOffset)-0.5); | ||
+ | | ||
+ | // draw the compass | ||
+ | drawCompass(ang+angleOffset, | ||
+ | }</ |
processing/compass_example.1375501192.txt.gz · Last modified: 2013/08/03 03:39 (external edit)