Creating layouts: the Koch curve

Macro file: fractal.lym

This application creates a <a href="http://en.wikipedia.org/wiki/Koch_snowflake">Koch curve</a> which is constructed by the recursive application of a generation recipe. In our case, this recipe is implemented by instantiating cells. An exact implementation would require a cell to call itself, but this is not allowed in this framework. Instead, a set of up to 20 cells is created with each cell calling the successive one in the same fashion.

When zooming deeply into the curve, the viewer gets pretty slow which is a consequence of the performance degradation of the underlying quad tree when the quads get really small. However, since this application is a pretty artificial one, I hope that this is not a serious imperfection ...

Source code

module Examples
 
  #  create one level of recursion
  #  unless the level is deeper than the maximum level, create a new cell and instantiate this in 
  #  the given upper cell four times rendering the Sierpinski curve.
  
  def self.create_recursion(layout, cell_id, layer_id, rec_level)
  
    if rec_level > 20
  
      #  max. recursion depth reached: just paint a straight line 
      edge = RBA::Edge.new(RBA::Point::new(0, 0), RBA::Point::new(3000, 0))
      layout.cell(cell_id).shapes(layer_id).insert_edge(edge) 
  
    else
     
      #  create a new cell 
      new_cell = layout.add_cell("level" + rec_level.to_s)
      cell = layout.cell(cell_id)
  
      #  and add four instances of it
      insts = [
        RBA::CellInstArray::new(new_cell, RBA::CplxTrans::new(1.0 / 3.0, 0.0, false, RBA::DPoint::new(0.0, 0.0))),
        RBA::CellInstArray::new(new_cell, RBA::CplxTrans::new(1.0 / 3.0, 60.0, false, RBA::DPoint::new(1000.0, 0.0))),
        RBA::CellInstArray::new(new_cell, RBA::CplxTrans::new(1.0 / 3.0, -60.0, false, RBA::DPoint::new(1500.0, 1000 * Math::sin(Math::PI / 3.0)))),
        RBA::CellInstArray::new(new_cell, RBA::CplxTrans::new(1.0 / 3.0, 0.0, false, RBA::DPoint::new(2000.0, 0.0)))
      ]
      insts.each { |i| cell.insert(i) }
    
      #  recursively create new cells on this one
      create_recursion(layout, new_cell, layer_id, rec_level + 1)
  
    end
  
  end    
  
  app = RBA::Application.instance
  mw = app.main_window
  
  #  create a new layout 
  mw.create_layout(0)
  view = mw.current_view
  
  #  create a new layer in that layout
  layout = view.cellview(0).layout 
  linfo = RBA::LayerInfo.new 
  layer_id = layout.insert_layer(linfo)
  
  #  create a layer view for that layer
  ln = RBA::LayerPropertiesNode::new
  ln.dither_pattern = 0
  ln.fill_color = 0xffff00
  ln.frame_color = 0xffff00
  ln.width = 1
  ln.source_layer_index = layer_id
  view.insert_layer(view.end_layers, ln)
  
  #  create a top cell and start the recursion on this
  topcell_id = layout.add_cell("top")
  create_recursion(layout, topcell_id, layer_id, 1)
  
  #  select his cell as the top cell, fit all and switch on all hierarchy levels
  view.select_cell_path([topcell_id], 0)
  view.update_content
  view.zoom_fit
  view.max_hier

end