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

Tuesday, 12 May 2009

Nautilus Shell Using Ruby Processing (no magic numbers)

Here we create a nautilus like shape using vertex and line functions, without any magic numbers, well apart from the A and B constants (which are empirically chosen so no magic really). This now old-style, as we can write applets without explicitly including the Processing::App wrapper. The code is implicitly wrapped in an applet which is more like the way things work in the java processing ide see later post (Interactive Ruby and Ruby Processing), I kind of like this style though.

# nautilus.rb
require 'ruby-processing'
class Nautilus < Processing::App
  A = 0.8  # pitch constant
  B = 1.4  # radius constant
  def setup
    translate 230, 120 # offset x and y from origin
    rotate QUARTER_PI + HALF_PI # initial rotation
    smooth
    background 0
    stroke_weight 1
    stroke 255
    for z in 8 .. 40 do # draw the radial lines first (it looks nicer)
      line(get_x(z*A), get_y(z*A), get_x((z-8)*A), get_y((z-8)*A))
    end
    no_fill
    begin_shape # begin spiral 'shell' shape
    stroke_weight 4
    stroke 255, 0, 0
    for i in 0 .. 40 do
      vertex(get_x(i*A), get_y(i*A))
    end
    end_shape
    save_frame "nautilus.png"
  end
  def get_x theta
    A*Math.cos(theta)*Math.exp(theta/Math.tan(B))
  end
   def get_y theta
    A*Math.sin(theta)*Math.exp(theta/Math.tan(B))
  end    
end

Nautilus.new :width => 360, :height => 300, :title => "Approximate Nautilus"

Here is the result:-

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