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

Sunday 16 February 2014

Experimenting with a Box class for ruby-processing (using ruby-2.0 syntax)

Right now you can use ruby-2.0 in ruby-processing, just set `compat.version=2.0` in your `.jrubyrc` file. Here I experiment with using that feature to create a axis bounding Box class (cf toxiclibs AABB class). If it actually makes it to a library I expect to do the rendering using PShape (with a possibly povray export option).
load_library :vecmath

attr_reader :my_box

def setup
  size 200, 200, P3D
  smooth 4
  camera(100.0, 100.0, 120.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0)
  @my_box = Box.new
  #my_box.scale(sz: 50)
  my_box.scale(x: 60, y: 50, z: 70)
end

def draw
  lights
  translate *my_box.center
  box *my_box.dimensions
end

class Box
  attr_reader :fl, :br

  def initialize(top_front_left: Vec3D.new(1.0, -1.0, 1.0), bottom_back_right: Vec3D.new(-1.0, 1.0, -1.0))
    @fl, @br = top_front_left, bottom_back_right
  end

  def scale(sz: 1.0, **args)
    if (args.empty?)
      @fl *= sz
      @br *= sz
    else
      fl.x *= args[:x] / 2.0 if args[:x]
      br.x *= args[:x] / 2.0 if args[:x]
      fl.y *= args[:y] / 2.0 if args[:y]
      br.y *= args[:y]  / 2.0 if args[:y]
      fl.z *= args[:z] / 2.0 if args[:z]
      br.z *= args[:z] / 2.0 if args[:z]
    end
  end

  def dimensions
    [fl.x - br.x, br.y - fl.y, fl.z - br.z]
  end

  def center
    dimensions.map{|x| x/-2.0}
  end
end

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