Parameter = Struct.new(:cx, :cy, :ch, :cw) # required for future RATIO = 1.336 TMAX = 5_000 attr_reader :current, :default # required for future work def setup size(534, 400, P2D) @default = Parameter.new(-0.53 - (1.4 * RATIO), -1.4, 2.8, 2.8 * RATIO) @current = default load_pixels no_fill stroke(255) create_mandelbrot(width, height) no_loop end def create_mandelbrot(w, h) y_zero = ->(y, param) { y * param.ch / height + param.cy } x_zero = ->(x, param) { x * param.cw / width + param.cx } h.times do |j| y0 = y_zero.call(j, current) w.times do |k| x0 = x_zero.call(k, current) x, y = 0, 0 xsqr, ysqr = 0.0, 0.0 t = 0 while t < TMAX && (xsqr + ysqr < 4.0) y = x * y y += y + y0 x = xsqr - ysqr + x0 xsqr = x * x ysqr = y * y t += 1 end if t < TMAX smth = t - Math.log(xsqr + ysqr) pixels[k + j * width] = Java::JavaAwt::Color.HSBtoRGB(smth / 500, 0.8, 0.9) else pixels[k + j * width] = 0 end end end update_pixels end
Experiments with ruby-processing (processing-2.2.1) and JRubyArt for processing-3.0
Thursday, 25 December 2014
Towards a zoom-able mandelbrot ruby-processing
Working toward a zoom-able mandelbrot (albeit somewhat speed limited in ruby-processing)
Labels:
mandelbrot,
ruby-processing
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
Labels:
arcball,
LinkedList,
lorenz attractor,
Vec3D
Thursday, 18 December 2014
Reading and writing to a csv file ruby-processing (version two)
I find it really surprising that one of most popular posts for ruby-processing is the original version here is an alternative slightly cleaner version:-
# # Loading Tabular Data # after Daniel Shiffman, by Martin Prout. # # This example demonstrates how to use CSV # to retrieve data from a CSV file and make objects # from that data. # # Here is what the CSV looks like: # # x,y,diameter,name # 160,103,43.19838,Happy # 372,137,52.42526,Sad # 273,235,61.14072,Joyous # 121,179,44.758068,Melancholy # require 'csv' load_library 'bubble' attr_reader :bubbles, :data def setup size(640, 360) load_data end def draw background(255) # Display all bubbles bubbles.run text_align(LEFT) fill(0) text('Click to add bubbles.', 10, height - 10) end def load_data # Load CSV file into an Array of Hash objects # headers: option indicates the file has a header row @bubbles = BubbleData.new CSV.foreach('data/data.csv', headers: true) do |row| x = row['x'].to_f y = row['y'].to_f d = row['diameter'].to_f n = row['name'] # Make a Bubble object out of the data read bubbles << Bubble.new(x, y, d, n) end end def mouse_pressed bubbles << Bubble.new(mouse_x, mouse_y, rand(40..80), 'blah') # If there are more than 10 bubbles delete the oldest bubble bubbles.shift if bubbles.size > 10 # Writing the csv data back to the same file, (also specify UTF-8 format) CSV.open('data/data.csv', 'w:UTF-8') do |csv| csv << %w(x y diameter name) # create csv headers bubbles.each do |bubble| csv << bubble.to_a # write back bubble data end end # And reloading it load_data end # A run module module Runnable def run each(&:display) each { |item| item.rollover(mouse_x, mouse_y) } end end # Enumerable class holds bubble data class BubbleData extend Forwardable def_delegators(:@bubbles, :each, :<<, :size, :shift) include Enumerable, Runnable def initialize @bubbles = [] end end
Labels:
csv,
ruby-processing,
saveTable,
UTF-8
Tuesday, 16 December 2014
Experimenting with jruby-complete-SNAPSHOT-9.0.0.0 and ruby processing
Anyone wishing to explore ruby-2.1+ syntax with ruby-processing should get hold of jruby-complete-SNAPSHOT-9.0.0.0.jar (compile it yourself) rename the jar to jruby-complete.jar and manually replace the jruby-complete.jar in the ruby-processing gem (lives in lib/ruby folder). Gems typically live in .gem/ruby/2.1.0/gems folder depending on your system unless you use rvm or other such nasty stuff. To ensure jruby-complete use add 'JRUBY: false' to your ~/.rp5rc settings (or use the --nojuby flag). Also use watch rather than run to run sketches (to avoid the failure in draw loop bug). Here is a sketch I posted earlier using 2.1 syntax, but here I explore using 'format' to present rationals....
After experimentation you can easily restore the original jruby-complete by running 'rp5 setup install' (no download required since we keep copy of the original in the vendors folder).
PS: this one of the sketches not affected by draw loop bug. May'be we don't need to wait too long for a preview version?
def setup size 640, 250 background 10 f = createFont("Arial", 24, true) third = 1 / 3r # since ruby 2.1.0 quarter = 1 / 4r add = format("%s + %s = %s", third, quarter, third + quarter) subtract = format("%s - %s = %s", third, quarter, third - quarter) multiply = format("%s * %s = %s", third, quarter, third * quarter) text_font(f, 24) fill(220) text("Math blackboard ruby-processing", 80, 50) text(add, 110, 100) text(subtract, 110, 150) text(multiply, 110, 200) end
PS: this one of the sketches not affected by draw loop bug. May'be we don't need to wait too long for a preview version?
@aselder Rough timeframe for a final is hopefully end of January. Will attempt a preview after MRI 2.2 is out.
— Charles Nutter (@headius) December 16, 2014
Subscribe to:
Posts (Atom)
Followers
About Me
- monkstone
- I have developed JRubyArt and propane new versions of ruby-processing for JRuby-9.1.5.0 and processing-3.2.2