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.
Math::PI * 2
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.
-
.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.
87 88 89 |
# File 'lib/math2d/utils2d.rb', line 87 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.
39 40 41 |
# File 'lib/math2d/utils2d.rb', line 39 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.
131 132 133 134 135 136 137 138 |
# File 'lib/math2d/utils2d.rb', line 131 def self.grayscale(val = nil) c = if val normalize(val, 0, 255).abs else rand end [c, c, c, 1.0] 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.
52 53 54 |
# File 'lib/math2d/utils2d.rb', line 52 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).
64 65 66 67 68 69 |
# File 'lib/math2d/utils2d.rb', line 64 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.
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/math2d/utils2d.rb', line 107 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.
77 78 79 |
# File 'lib/math2d/utils2d.rb', line 77 def self.normalize(value, a, b) map(value, a, b, 0.0, 1.0) end |
.to_deg(angle) ⇒ Float
Returns angle radians in degrees.
20 21 22 |
# File 'lib/math2d/utils2d.rb', line 20 def self.to_deg(angle) (180 * angle) / Math::PI end |
.to_rad(angle) ⇒ Float
Returns angle degrees in radians.
28 29 30 |
# File 'lib/math2d/utils2d.rb', line 28 def self.to_rad(angle) (Math::PI * angle) / 180 end |