Henon Phase Diagram

@tabreturn @villares Jorg Kantel often posts stuff on twitter that should be of interest to python processing fans on twitter @jkantel (usually links to his blog). Inspired by one of his latest posts I created the following JRubyArt sketch (thanks to Paul Bourke).

MAX = 10.0e+9

attr_reader :a, :h, :cosa, :sina, :x0, :y0

def settings
  size(500, 500)
end

def setup
  sketch_title 'Henon Phase Diagram'
  @a = -10
  @h = 0.02
  @x0 = 0.01
  @y0 = -0.02
  @cosa = cos(a)
  @sina = sin(a)
  fill(20)
  color_mode HSB, 250, 1.0, 1.0
  rect(0, 0, width, height)
  x = x0
  y = y0
  (0..50).each do |j|
    (0..1500).each do |i|
      next unless (x < MAX) && (y < MAX)

      x1 = x * cosa - (y - x * x) * sina
      y = x * sina + (y - x * x) * cosa
      x = x1
      p = (x + 1.25) * 180
      q = (1.4 - y) * 180
      fill(sqrt((250 - p)**2 + (250 - q)**2), 1.0, 1.0)
      circle(p, q, 2)
    end
    x = x0 + j * h
    y = y0 + j * h
  end
  fill(255)
  text_size(14)
  msg = format('Quadratic Henon-Equation for a = %.4f', a)
  text(msg, 20, width - 20)
end

henon
Here’s an interesting variant where x**2 is replaced by x * |x|
modulo

I’ve a question for moderators, why is my ‘png’ file getting converted to jpeg it’s not good?
Here’s another Henon Phase Diagram with a value from Paul Bourke
fractal

5 Likes

I’ve created a new improved version allowing choice of value and between x**2 and x * |x|

load_library :control_panel
H = 0.02
MAX = 10.0e+9
attr_reader :a, :cosa, :sina, :points, :center, :choice, :modulo

def settings
  size(500, 500)
end

def setup
  sketch_title 'Henon Phase Diagram Explorer'
  control_panel do |c|
    c.title 'Select a'
    c.look_feel 'Nimbus'
    c.menu(:choice, %w[-10 1.2 1.6 1.57 2.0 2.2], '2.0') { |m| m }
    c.checkbox :modulo
    c.button :reset!
  end
  @modulo = false
  @center = Vec2D.new(width / 2, height / 2)
  color_mode HSB, 350, 1.0, 1.0
  reset!
end

def reset!
  calculate
  redraw
end

def calculate(x0 = 0.01, y0 = -0.02)
  x = x0
  y = y0
  @a = choice.to_f
  @cosa = cos(a)
  @sina = sin(a)
  @points = []
  funcx = if modulo
            ->(cx) { cx * cx.abs }
          else
            ->(cx) { cx * cx }
          end
  (0..50).each do |j|
    1500.times do
      next unless (x < MAX) && (y < MAX)

      x1 = x * cosa - (y - funcx.call(x)) * sina
      y = x * sina + (y - funcx.call(x)) * cosa
      x = x1
      points << Vec2D.new((x + 1.25) * 180, (1.4 - y) * 180)
    end
    x = x0 + j * H
    y = y0 + j * H
  end
end

def draw
  background 100
  points.each do |pos|
    fill(center.dist(pos), 1.0, 1.0)
    circle(pos.x, pos.y, 2)
  end
  fill(10, 1, 1)
  text_size(14)
  mod = 'x * |x|'
  text(mod, 10, 30) if modulo
  msg = format('Quadratic Henon-Equation for a = %3.4f', a)
  text(msg, 10, height - 30)
  img = format('henon_%3.4f.jpg', a)
  save_frame(data_path(img))
  no_loop
end

1 Like

Wonderful!

Side question: Is this control_panel lib a JRubyArt only thing?
I have played with using some choice dropdowns from java.swing, but I suppose they are what one calls “modal” (is it the right term?), and to make a second window/surface is such a hassle…

Yeah control_panel is only a ruby-processing thing, it’s swing based. Takes advantage of some cool jruby/ruby features.

1 Like