lib/rubysketch/processing.rb in rubysketch-0.3.6 vs lib/rubysketch/processing.rb in rubysketch-0.3.7

- old
+ new

@@ -139,13 +139,13 @@ # # @return [Vector] interporated vector # def lerp (*args, amount) v = toVector__ *args - 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.x = x + (v.x - x) * amount + self.y = y + (v.y - y) * amount + self.z = z + (v.z - z) * amount self end # Returns the interpolated vector between 2 vectors. # @@ -726,31 +726,96 @@ end end# Touch + # Camera object. + # + class Capture + + # Returns a list of available camera device names + # + # @return [Array] device name list + # + def self.list () + ['default'] + end + + # Initialize camera object. + # + def initialize () + @camera = Rays::Camera.new + end + + # Start capturing. + # + # @return [Capture] self + # + def start () + raise "Failed to start capture" unless @camera.start + self + end + + # Stop capturing. + # + # @return [nil] nil + # + def stop () + @camera.stop + nil + end + + # Returns is the next captured image available? + # + # @return [Boolean] true means object has next frame + # + def available () + @camera.active? + end + + # Reads next frame image + # + def read () + @camera.image + end + + # @private + def getInternal__ () + @camera.image || dummyImage__ + end + + # @private + private def dummyImage__ () + @dummy ||= Rays::Image.new 1, 1 + end + + end# Capture + + # Drawing context # module GraphicsContext - Vector = Processing::Vector + # PI + # + PI = Math::PI # PI / 2 # - HALF_PI = Math::PI / 2 + HALF_PI = PI / 2 # PI / 4 # - QUARTER_PI = Math::PI / 4 + QUARTER_PI = PI / 4 # PI * 2 # - TWO_PI = Math::PI * 2 + TWO_PI = PI * 2 # PI * 2 # - TAU = Math::PI * 2 + TAU = PI * 2 # RGB mode for colorMode(). # RGB = :RGB @@ -815,11 +880,11 @@ def setup__ (painter) @painter__ = painter @painter__.miter_limit = 10 - @drawing = false + @drawing__ = false @hsbColor__ = false @colorMaxes__ = [1.0] * 4 @angleScale__ = 1.0 @rectMode__ = nil @ellipseMode__ = nil @@ -841,15 +906,15 @@ end def beginDraw () @matrixStack__.clear @styleStack__.clear - @drawing = true + @drawing__ = true end def endDraw () - @drawing = false + @drawing__ = false end def width () @image__.width end @@ -1409,12 +1474,13 @@ # # @return [nil] nil # def image (img, a, b, c = nil, d = nil) assertDrawing__ - x, y, w, h = toXYWH__ @imageMode__, a, b, c || img.width, d || img.height - @painter__.image img.getInternal__, x, y, w, h + i = img.getInternal__ + x, y, w, h = toXYWH__ @imageMode__, a, b, c || i.width, d || i.height + @painter__.image i, x, y, w, h nil end # Copies image. # @@ -1586,17 +1652,17 @@ popMatrix popStyle end # @private - private def getInternal__ () + def getInternal__ () @image__ end # @private private def assertDrawing__ () - raise "call beginDraw() before drawing" unless @drawing + raise "call beginDraw() before drawing" unless @drawing__ end end# GraphicsContext @@ -1604,21 +1670,27 @@ # class Graphics include GraphicsContext + # Initialize graphics object. + # def initialize (width, height) @image__ = Rays::Image.new width, height setup__ @image__.painter end + # Start drawing. + # def beginDraw () @painter__.__send__ :begin_paint super push end + # End drawing. + # def endDraw () pop super @painter__.__send__ :end_paint end @@ -1626,27 +1698,31 @@ end# Graphics # Processing context # - module Context + class Context - include GraphicsContext, Math + include GraphicsContext + Vector = Processing::Vector + Capture = Processing::Capture + Graphics = Processing::Graphics + @@context__ = nil def self.context__ () @@context__ end # @private - def setup__ (window) + def initialize (window) @@context__ = self @window__ = window @image__ = @window__.canvas - super @window__.canvas_painter.paint {background 0.8} + setup__ @window__.canvas_painter.paint {background 0.8} @loop__ = true @redraw__ = false @frameCount__ = 0 @mousePos__ = @@ -1863,11 +1939,13 @@ # def redraw () @redraw__ = true end - module_function + # + # Utilities + # # Returns the absolute number of the value. # # @param value [Numeric] number # @@ -1905,10 +1983,30 @@ # def round (value) value.round end + # Returns the natural logarithm (the base-e logarithm) of a number. + # + # @param value [Numeric] number (> 0.0) + # + # @return [Numeric] result number + # + def log (n) + Math.log n + end + + # Returns Euler's number e raised to the power of value. + # + # @param value [Numeric] number + # + # @return [Numeric] result number + # + def exp (n) + Math.exp n + end + # Returns value raised to the power of exponent. # # @param value [Numeric] base number # @param exponent [Numeric] exponent number # @@ -1926,10 +2024,20 @@ # def sq (value) value * value end + # Returns squared value. + # + # @param value [Numeric] number + # + # @return [Numeric] squared value + # + def sqrt (value) + Math.sqrt value + end + # Returns the magnitude (or length) of a vector. # # @overload mag(x, y) # @overload mag(x, y, z) # @@ -2198,9 +2306,17 @@ # # @return [Vector] new vector # def createVector (*args) Vector.new *args + end + + # Creates a camera object as a video input device. + # + # @return [Capture] camera object + # + def createCapture (*args) + Capture.new *args end # Creates a new off-screen graphics context object. # # @param width [Numeric] width of graphics image