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

Tuesday 23 December 2014

Lorenz Attractor Ruby-Processing

Here is a processing sketch that used a LinkedList (not needed in ruby version, an array is sufficient)
LENGTH = 10_000
SPEED = 10

load_library 'vecmath'

attr_accessor :particles, :a, :b, :c, :d

def setup
  size 512, 512, P3D
  ArcBall.init(self)
  frame_rate 30
  stroke color(0, 0, 0, 33)
  stroke_weight 2
  @a = 10
  @b = 28
  @c = 8 / 3
  @d = 0.75
  init_particles
end

def draw
  background(255)
  advance_particles(SPEED)
  draw_particles
end

def init_particles
  @particles = []
  LENGTH.times do
    add_particle(Vec3D.new 1.5, -1.5, 1.5) # need to start somewhere
  end
end

def add_particle(p)
  step = Vec3D.new(a * (p.y - p.x), p.x * (b - p.z) - p.y, p.x * p.y - c * p.z)
  step *= (d / step.mag)
  particles << p + step
end

def advance_particles(count)
  (1..count).each do
    particles.shift
    add_particle(particles.last)
  end
end

def draw_particles
  scale(8)
  particles.each_cons(2) do |a, b|
    stroke(color((b.x - a.x) * 255, (b.y - a.y) * 255, (b.z - a.z) * 255, 88))
    line(a.x, a.y, a.z - 30, b.x, b.y, b.z - 30)
  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