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

Sunday, 3 May 2015

First class keyword arguments in ruby-processing?

In vanilla processing there are many multi parameter arguments (sometimes overloaded, usual java stuff) that can cause offence to the average rubyist, take camera for example:-
// java/processing 9 float arguments
void camera(
     float eyeX,
     float eyeY,
     float eyeZ,
     float centerX,
     float centerY,
     float centerZ,
     float upX,
     float upY,
     float upZ){...}

// default values
camera(width/2.0, height/2.0, (height/2.0) / tan(PI*30.0 / 180.0), width/2.0, height/2.0, 0, 0, 1, 0);

Since we can now use ruby-2.0 syntax (with jruby-9.0.0.0) there is scope for improvement, by using keyword arguments and Vector object (need not be Vec3D a Struct would do):-
# ruby 3 named parameters (order would be unimportant)
def camera_vec(
               eye: Vec3D.new(width / 2.0, height / 2.0, (height / 2.0) / tan(PI * 30.0 / 180.0)),
               center: Vec3D.new(width / 2.0, height / 2.0, 0),
               up: Vec3D.new(0, 1.0, 0))
  camera(eye.x, eye.y, eye.z, center.x, center.y, center.z, up.x, up.y, up.z)
end

No comments:

Post a Comment

Followers

Blog Archive

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