Skip to content

Utility Functions

gaw1ik edited this page Oct 7, 2021 · 3 revisions

Below are the current set of utility functions in genartlayers. This includes many random number utilities as well as some useful conversion functions.

Random number utilities:

Note about random number utilities: All random number utility functions use a global random number generator, which is seeded with the integer 1 unless the user specifies a seed parameter in the layer's parameters. Simply defining a parameter with the name "seed" will allow the user to reseed the random number generator, which (in my experience, at least) is an essential aspect of generative art. The same seed will always be able to reproduce the same sequence of random numbers, so artists will be able to recreate their work, even when it is based in randomization. It's important to understand that shifting the order of random utility functions in your algorithms or changing the number of random utility functions used will alter the sequence of random numbers and thus change your design. It's important to "solidify" your algorithm before getting attached to a particular set of seeds.

getRandomInt(min, max)

Gives a random integer in the range defined by min and max. The range includes min and max.


getRandomFloat(min, max) 

Gives a random float in the range defined by min and max. The range includes min and max.


vary(centerValue, percentage)

Randomly varies a parameter by a range defined as the parameter's center value plus or minus a percentage of that center value. Example: vary(100,15) will give a random value in the range 85-115; (returns a float).


varyAbove(parameter, percent)

Randomly varies a parameter by a range defined as the parameter's center value plus a percentage of that center value.


varyUnder(parameter, percent)

Randomly varies a parameter by a range defined as the parameter's center value minus a percentage of that center value.


plusOrMinus(centerValue, maxAmount)

Returns centerValue plus or minus a randomly selected number in the range 0-maxAmount. Example: plusOrMins(50,7) would return a value in the range 43-57;


randomSign() 

Returns either 1 or -1 at random.


chooseFromArray(array) 

Returns a randomly chosen value from an array.


Conversion utilities:

 deg2rad(thetaD)

Converts the value thetaD (which is assumed to be in degrees) to the equivalent value in radians.


rad2deg(thetaR)

Converts the value thetaR (which is assumed to be in radians) to the equivalent value in degrees.


Interpolation and Bezier Functions:

[x, y] = lin(  t, [x1,y1], [x2,y2] )

Given a linear function defined by the points [x1,y1] and [x2,y2], returns the interpolated point [x,y] based on the given t-value.

Example: lin(0.6, [0,0], [100,100]) will return the value [60,60].


[x, y] = bez3( t, [x1,y1], [xC1,yC1], [x2,y2] )

Given a quadratic bezier function defined by the endpoints [x1,y1] and [x2,y2] and control point [xC1,yC1], returns the interpolated point [x,y] based on the given t-value.


[x, y] = bez4( t, [x1,y1], [xC1,yC1], [xC2,yC2], [x2,y2] )

Given a cubic bezier function defined by the endpoints [x1,y1] and [x2,y2] and control points [xC1,yC1] and [xC2,yC2], returns the interpolated point [x,y] based on the given t-value.


Clone this wiki locally