lib/rubysketch/processing.rb in rubysketch-0.3.4 vs lib/rubysketch/processing.rb in rubysketch-0.3.5

- old
+ new

@@ -139,13 +139,13 @@ # # @return [Vector] interporated vector # def lerp (*args, amount) v = toVector__ *args - self.x = x + (v.x - x) * amount - self.y = y + (v.y - y) * amount - self.z = z + (v.z - z) * amount + self.x = Context.lerp x, v.x, amount + self.y = Context.lerp y, v.y, amount + self.z = Context.lerp z, v.z, amount self end # Returns the interpolated vector between 2 vectors. # @@ -730,10 +730,12 @@ # Drawing context # module GraphicsContext + Vector = Processing::Vector + # PI / 2 # HALF_PI = Math::PI / 2 # PI / 4 @@ -928,11 +930,11 @@ end nil end # @private - protected def toAngle__ (angle) + def toAngle__ (angle) angle * @angleScale__ end # Sets rect mode. Default is CORNER. # @@ -1861,34 +1863,12 @@ # def redraw () @redraw__ = true end - # - # Utility functions - # + module_function - # Converts degree to radian. - # - # @param degree [Numeric] degree to convert - # - # @return [Numeric] radian - # - def radians (degree) - degree * DEG2RAD__ - end - - # Converts radian to degree. - # - # @param radian [Numeric] radian to convert - # - # @return [Numeric] degree - # - def degrees (radian) - radian * RAD2DEG__ - end - # Returns the absolute number of the value. # # @param value [Numeric] number # # @return [Numeric] absolute number @@ -2078,10 +2058,101 @@ # def constrain (value, min, max) value < min ? min : (value > max ? max : value) end + # Converts degree to radian. + # + # @param degree [Numeric] degree to convert + # + # @return [Numeric] radian + # + def radians (degree) + degree * DEG2RAD__ + end + + # Converts radian to degree. + # + # @param radian [Numeric] radian to convert + # + # @return [Numeric] degree + # + def degrees (radian) + radian * RAD2DEG__ + end + + # Returns the sine of an angle. + # + # @param angle [Numeric] angle in radians + # + # @return [Numeric] the sine + # + def sin (angle) + Math.sin angle + end + + # Returns the cosine of an angle. + # + # @param angle [Numeric] angle in radians + # + # @return [Numeric] the cosine + # + def cos (angle) + Math.cos angle + end + + # Returns the ratio of the sine and cosine of an angle. + # + # @param angle [Numeric] angle in radians + # + # @return [Numeric] the tangent + # + def tan (angle) + Math.tan angle + end + + # Returns the inverse of sin(). + # + # @param value [Numeric] value for calculation + # + # @return [Numeric] the arc sine + # + def asin (value) + Math.asin value + end + + # Returns the inverse of cos(). + # + # @param value [Numeric] value for calculation + # + # @return [Numeric] the arc cosine + # + def acos (value) + Math.acos value + end + + # Returns the inverse of tan(). + # + # @param value [Numeric] value for valculation + # + # @return [Numeric] the arc tangent + # + def atan (value) + Math.atan value + end + + # Returns the angle from a specified point. + # + # @param y [Numeric] y of the point + # @param x [Numeric] x of the point + # + # @return [Numeric] the angle in radians + # + def atan2 (y, x) + Math.atan2 y, x + end + # Returns the perlin noise value. # # @overload noise(x) # @overload noise(x, y) # @overload noise(x, y, z) @@ -2092,9 +2163,23 @@ # # @return [Numeric] noise value (0.0..1.0) # def noise (x, y = 0, z = 0) Rays.perlin(x, y, z) / 2.0 + 0.5 + end + + # Returns a random number in range low...high + # + # @overload random(high) + # @overload random(low, high) + # + # @param low [Numeric] lower limit + # @param high [Numeric] upper limit + # + # @return [Float] random number + # + def random (low = nil, high) + rand (low || 0).to_f...high.to_f end # Creates a new vector. # # @overload createVector()