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

Wednesday 8 January 2014

More keyword argument experiments with JRubyArt

Here we create a factory module, that allows us to create a RubyStruct (entry order important) using a keyword hash (order not important).
#
# Esfera
# by David Pena.  
# Somewhat re-factored for ruby-processing
# by Martin Prout
# Distribucion aleatoria uniforme sobre la superficie de una esfera. 
#

QUANTITY = 16000

attr_reader :orb, :radius, :rx, :ry

# signature-specific aliases for overloaded methods
java_alias :fill_int, :fill, [Java::int]
java_alias :stroke_int, :stroke, [Java::int]
java_alias :stroke_float_float, :stroke, [Java::float, Java::float]

module HairFactory
  Hair = Struct.new(:z, :phi, :len, :theta)
  def self.hair(args)
    Hair.new(
      args[:z],
      args[:phi],
      args[:len],
      args[:theta]
      )
    end
end

def setup
  size(800, 600, P3D)
  @rx = 0
  @ry = 0
  no_smooth
  @radius = height/3.5
  @orb = []
  # lets define some ruby lambdas
  rnd = ->(rad){rand(-rad .. rad)}
  asine = ->(zl, rl){Math.asin(zl / rl)}
  rl = ->(a, b){rand(a .. b)}
  rpi = -> {rand * Math::PI * 2}

  # lets populate the orb with hairs
  QUANTITY.times do
    z = rnd.call(radius) # needed twice so need to define outside constructor
    orb << HairFactory.hair(  # note by using keyword arguments their order is not important
      z: z,
      phi: rpi.call,
      len: rl.call(1.15, 1.2),
      theta: asine.call(z, radius)
      )
  end
  noise_detail(3)
end

def draw
  off = ->(a, b, c){a + ((noise(c * 0.0005, Math.sin(b)) - 0.5) * 0.3)}
  offb = ->(a, b, c){a + ((noise(c * 0.0007, Math.sin(b) * 0.01) - 0.5) * 0.3)}
  background(0)
  translate(width/2,height/2)
  rxp = ((mouse_x - (width/2))*0.005)
  ryp = ((mouse_y - (height/2))*0.005)
  @rx = (rx*0.9)+(rxp*0.1)
  @ry = (ry*0.9)+(ryp*0.1)
  rotate_y(rx)
  rotate_x(ry)
  fill_int 0
  no_stroke
  sphere(radius)
  orb.each do |hair|
    thetaff = off.call(hair.theta, hair.phi, millis())
    costhetaff = Math.cos(thetaff)
    coshairtheta = Math.cos(hair.theta)
    phff = offb.call(hair.phi, hair.z, millis())
    xa = radius * coshairtheta * Math.cos(hair.phi)
    ya = radius * coshairtheta * Math.sin(hair.phi)
    za = radius * Math.sin(hair.theta)
    xo = radius * costhetaff * Math.cos(phff)
    yo = radius * costhetaff * Math.sin(phff)
    zo = radius * sin(thetaff)
    xb, yb, zb = xo * hair.len, yo * hair.len, zo * hair.len
    stroke_weight(1)
    begin_shape(LINES)
    stroke_int(0)
    vertex(xa, ya, za)
    stroke_float_float(200, 150)
    vertex(xb, yb, zb)
    end_shape()
  end
  if (frame_count % 10 == 0)
    puts(frame_rate)
  end
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