Experiments with ruby-processing (processing-2.2.1) and JRubyArt for processing-3.0

Tuesday 5 May 2009

Inner classes, workaround for ruby processing

Developments are moving a pace over on the processing implementation discourse http://processing.org/discourse/yabb2/YaBB.pl?num=1238452139. As you may have realized that classes in processing are generally inner classes, and so have access to methods of the enclosing PApplet class. This is not the same in ruby however Jeremy Ashkenas aka jashkenas has been busy developing a workaround for ruby processing (including processing as a mixin) which seems quite interesting. Here is an example using my Saucer class, I'm not super keen about my render method it doesn't look too ruby like (inherited from my java processing code):-

# saucer.rb

include Math

class Saucer

        include Processing::Proxy

        # include processing as Mixin

        R1 = 4.0  # class constants that together

        R2 = 3.0  # with PI constants are used

        R3 = 6.0  # to define the Saucer shape

        A1 = 0.25

        A2 = 0.60

        A3 = 0.0

        MAX_THETA = QUARTER_PI/2

        attr_reader :xpos, :ypos, :my_scale, :tilt

        def initialize xpos, ypos

                @xpos = xpos

                @ypos = ypos

                @my_scale = 10.0

                @tilt = 0

        end

        def get_point radius, theta

                x = xpos + (radius * Math.cos(theta + tilt)) *my_scale

                y = ypos + (radius * Math.sin(theta + tilt)) *my_scale

                return x, y

        end

        def set_tilt(theta)

                theta = Saucer::MAX_THETA if theta > Saucer::MAX_THETA

                theta = -Saucer::MAX_THETA if theta < -Saucer::MAX_THETA

                @tilt = theta

        end

        def set_scale(scale)

                @my_scale = scale unless scale < 0

        end

        def render

                smooth

                curve_tightness -0.05

                begin_shape

                x, y = get_point(Saucer::R1, Saucer::A1)

                curve_vertex(x, y)

                x, y = get_point(Saucer::R2, TWO_PI - Saucer::A2)

                curve_vertex(x, y)

                x, y = get_point(Saucer::R2, PI + Saucer::A2)

                curve_vertex(x, y)

                x, y = get_point(Saucer::R1, PI - Saucer::A1)

                curve_vertex(x, y)

                x, y = get_point(Saucer::R2, PI + Saucer::A2)

                vertex(x, y)

                x, y = get_point(Saucer::R3, PI)

                vertex(x, y)

                vertex(x, y)

                x, y = get_point(Saucer::R1, PI - Saucer::A1)

                vertex(x, y)

                vertex(x, y)

                x, y = get_point(Saucer::R1, Saucer::A1)

                vertex(x, y)

                vertex(x, y)

                x, y = get_point(Saucer::R3, Saucer::A3)

                vertex(x, y)

                vertex(x, y)

                end_shape CLOSE

        end

end



Here is the main code:-


require 'saucer'

class MixinsTest < Processing::App

        def setup

                @saucer = Saucer.new 70, 50

                @saucer.set_tilt QUARTER_PI/2

                @saucer1 = Saucer.new 130, 170

                @saucer1.set_scale 15

                @saucer1.set_tilt QUARTER_PI/3

                @saucer2 = Saucer.new 210, 340

                @saucer2.set_scale 25

        end

        def draw

                background 11, 17, 67

                fill 116, 39, 39

                @saucer.render

                fill 255, 0, 0

                @saucer1.render

                @saucer2.render

                save_frame("saucer.png")

        end

end



MixinsTest.new :width => 400, :height => 400, :title => "Inner Test"



Heres the result of running the code 'rp5 run main.rb'

No comments:

Post a Comment

Followers

About Me

My photo
I have developed JRubyArt and propane new versions of ruby-processing for JRuby-9.1.5.0 and processing-3.2.2