Basic utility functions
The last section of the chapter mentions some utility functions, which will be used in examples across the book, or just can be useful in your projects.
ofMap( v, v0, v1, out0, out1 )
: This function performs linear interpolation of a float valuev
from the segment[v0, v1]
to the segment[out0, out1]
. Note, it does not control boundaries ofv
, and just uses the formula (v-v0)/(v1-v0)*(out1-out0) + out0. For controlling boundaries, call this function with the last optional parameter set totrue
:ofMap( v, v0, v1, out0, out1, true )
. Then the result will be clamped to[out0, out1]
.ofClamp( v, v0, v1 )
: This function clamps the float valuev
to the segment[v0, v1]
that is returnsmin( max( v, v0 ), v1 )
.ofRandom( a, b )
: This function generates a pseudo-random number in the segment[a, b]
. (Actually, it returns a value always less than that ofb
).ofNoise ( x )
,ofNoise( x, y )
,ofNoise( x, y, z )
, andofNoise( x, y, z, w )
:These functions return Perlin noise value, see the details in Appendix B, Perlin Noise.ofToString( v )
: This function converts theint
orfloat
valuev
into string-returning valueofToInt( s )
andofToFloat( s )
: These functions convert the strings
intoint
orfloat
returning values respectively.ofGetWidth()
andofGetHeight()
: These functions return the current width and height of the project's screen, in pixels.ofGetElapsedTimef()
: This function returns the value of seconds lapsed from the project's start. This is afloat
value, measured with millisecond accuracy. For example, returned value 123.4 means 123 seconds and 400 milliseconds.ofShowCursor()
andofHideCursor()
: These functions show and hide the mouse cursor.
For printing information into a console window, you can use the standard cout
stream. For example, for printing time from the project's start, use the following code:
cout << "Time: " << ofGetElapsedTimef() << endl;
Here, endl
is a standard constant, which means starting a new line in the console.