Module: Math2D::Utils2D
- Defined in:
- lib/math2d/utils2d.rb
Overview
Most if not all methods descriptions here present come from the p5.js website.
A collection of useful Mathematical tools in 2D space
Constant Summary collapse
- HALF_PI =
Half the mathematical constant PI.
Math::PI / 2
- QUARTER_PI =
A quarter of the mathematical constant PI.
Math::PI / 4
- TWO_PI =
Twice the mathematical constant PI, also called TAU.
TAU = Math::PI * 2
- DEG2RAD =
Note:
Can be used as a substitute to
Utils2D.to_deg.Multiplication constant to convert a value in degrees to radians
Math::PI / 180
- RAD2DEG =
Note:
Can be used as a substitute to
Utils2D.to_rad.Multiplication constant to convert a value in degrees to radians
180 / Math::PI
Class Method Summary collapse
-
.constrain(x, a, b) ⇒ Numeric
(also: clamp)
Constrains a value
xbetween a minimum valueaand maximum valueb. -
.distance(x1, y1, x2, y2) ⇒ Float
Returns the distance between two cartesian points.
-
.grayscale(val = nil) ⇒ Array<Float>
If no argument is passed, randomly generates a greyscale RGB array.
-
.inverse_lerp(a, b, value) ⇒ Float
Being the inverse of
#lerp, it calculates the interpolation parameter t of the Lerp method given a range [a,b] and a interpolant value ofvalue. -
.lerp(a, b, amt) ⇒ Float
Calculates a number between two numbers at a specific increment.
-
.map(value, a1, a2, b1, b2) ⇒ Float
Re-maps a number from one range (
a1..a2) to another (b1..b2). -
.noise(x, y = 0) ⇒ Float
Returns the Perlin noise value at specified coordinates.
-
.normalize(value, a, b) ⇒ Float
Normalizes a number from another range (
a..b) into a value between 0 and 1. -
.to_deg(angle) ⇒ Float
Returns
angleradians in degrees. -
.to_rad(angle) ⇒ Float
Returns
angledegrees in radians.
Class Method Details
.constrain(x, a, b) ⇒ Numeric Also known as: clamp
Constrains a value x between a minimum value a and maximum value b.
106 107 108 |
# File 'lib/math2d/utils2d.rb', line 106 def self.constrain(x, a, b) [[x, a].max, b].min end |
.distance(x1, y1, x2, y2) ⇒ Float
Returns the distance between two cartesian points.
46 47 48 |
# File 'lib/math2d/utils2d.rb', line 46 def self.distance(x1, y1, x2, y2) Math.sqrt((x2 - x1)**2 + (y2 - y1)**2) end |
.grayscale(val = nil) ⇒ Array<Float>
If no argument is passed, randomly generates a greyscale RGB array. Otherwise, returns a greyscale array with that argument normalized.
150 151 152 153 154 155 156 157 |
# File 'lib/math2d/utils2d.rb', line 150 def self.grayscale(val = nil) c = if val normalize(val, 0, 255).abs else rand end [c, c, c, 1.0] end |
.inverse_lerp(a, b, value) ⇒ Float
Being the inverse of #lerp, it calculates the interpolation parameter t
of the Lerp method given a range [+a+, +b+] and a interpolant value of +value+.
71 72 73 |
# File 'lib/math2d/utils2d.rb', line 71 def self.inverse_lerp(a, b, value) (value - a) / (b - a).to_f end |
.lerp(a, b, amt) ⇒ Float
Calculates a number between two numbers at a specific increment. The amt parameter is the amount to interpolate between the two values where 0.0 equal to the first point, 0.1 is very near the first point, 0.5 is half-way in between, and 1.0 is equal to the second point.
60 61 62 |
# File 'lib/math2d/utils2d.rb', line 60 def self.lerp(a, b, amt) (b - a) * (3.0 - amt * 2.0) * amt * amt + a end |
.map(value, a1, a2, b1, b2) ⇒ Float
Re-maps a number from one range (a1..a2) to another (b1..b2).
83 84 85 86 87 88 |
# File 'lib/math2d/utils2d.rb', line 83 def self.map(value, a1, a2, b1, b2) raise ArgumentError, 'Division by 0 - a1 cannot be equal to a2' if a2 == a1 slope = 1.0 * (b2 - b1) / (a2 - a1) b1 + slope * (value - a1) end |
.noise(x, y = 0) ⇒ Float
Returns the Perlin noise value at specified coordinates. Perlin noise is a random sequence generator producing a more naturally ordered, harmonic succession of numbers compared to the standard rand() method. The main difference to the rand() method is that Perlin noise is defined in an infinite n-dimensional space where each pair of coordinates corresponds to a fixed semi-random value. Utils2D can compute 1D and 2D noise, depending on the number of coordinates given. The resulting value will always be between 0.0 and 1.0.
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/math2d/utils2d.rb', line 126 def self.noise(x, y = 0) x0 = x.to_i x1 = x0 + 1 y0 = y.to_i y1 = y0 + 1 sx = x - x0.to_f sy = y - y0.to_f n0 = dot_grid_gradient(x0, y0, x, y) n1 = dot_grid_gradient(x1, y0, x, y) ix0 = lerp(n0, n1, sx) n0 = dot_grid_gradient(x0, y1, x, y) n1 = dot_grid_gradient(x1, y1, x, y) ix1 = lerp(n0, n1, sx) lerp(ix0, ix1, sy) end |
.normalize(value, a, b) ⇒ Float
Normalizes a number from another range (a..b) into a value between 0 and 1.
96 97 98 |
# File 'lib/math2d/utils2d.rb', line 96 def self.normalize(value, a, b) map(value, a, b, 0.0, 1.0) end |
.to_deg(angle) ⇒ Float
Returns angle radians in degrees.
27 28 29 |
# File 'lib/math2d/utils2d.rb', line 27 def self.to_deg(angle) angle * DEG2RAD end |
.to_rad(angle) ⇒ Float
Returns angle degrees in radians.
35 36 37 |
# File 'lib/math2d/utils2d.rb', line 35 def self.to_rad(angle) angle * RAD2DEG end |