packages feed

mandulia-0.4: config/random.lua

--[[--

Mandulia -- Mandelbrot/Julia explorer
Copyright (C) 2010  Claude Heiland-Allen <claudiusmaximus@goto10.org>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

--]]--

require("defaults")
require("transition")

do

  --[[-- generate random views and transition between them --]]--

  -- randomness
  math.randomseed(os.time())

  -- for time-limited recording
  local frameLimit = 10 * 60 * 25
  local frames = 0

  -- magic numbers
  local phi  = (math.sqrt(5) + 1) / 2
  local phi1 = (math.sqrt(5) - 1) / 2

  -- animation parameters
  local defaultSpeed = 0.25
  local defaultWeight = 25

  -- view bounds
  local minimum = { x = -1.5, y = -1.1, z =  1 }
  local maximum = { x =  0.5, y =  1.1, z = 32 }

  -- default view
  local function zero() return { x = 0, y = 0, z = minimum.z } end

  -- animation variables
  local source = zero()
  local target = zero()
  local t = 0
  local s = 0
  local f = function(t) return zero() end
  local dz = minimum.z
  local speed = defaultSpeed
  local weight = defaultWeight

  -- keyboard bindings
  local keys =
    { Escape   = function() mandulia.quit() end
    , F11      = function() mandulia.fullscreen = not mandulia.fullscreen end
    , Home     = function()
                   source = f(t) ; target = zero() ; t = 0
                   s, f = transition(source, target, weight, phi)
                 end
    , PageUp   = function() weight = weight * 0.95 end
    , PageDown = function() weight = weight / 0.95 end
    , End      = function() weight = defaultWeight end
    , ["["]    = function() speed = speed * 0.95 end
    , ["]"]    = function() speed = speed / 0.95 end
    , ["#"]    = function() speed = defaultSpeed end
    }
  function mandulia.keyboard(key)
    if type(keys[key]) == "function" then keys[key]() end
  end

  -- every frame
  function mandulia.render()
    if mandulia.record then
      if frames == frameLimit then
        mandulia.quit()
      else
        frames = frames + 1
      end
    end
    t = t + speed * (phi1 ^ dz)
    if t > s then -- we reached the target, new target needed
      t = t - s
      source.x = target.x
      source.y = target.y
      source.z = target.z
      target.x = math.random() * (maximum.x - minimum.x) + minimum.x
      target.y = math.random() * (maximum.y - minimum.y) + minimum.y
      target.z = math.random() * (maximum.z - minimum.z) + minimum.z
      s, f = transition(source, target, weight, phi)
    end
    mandulia.view = f(t)
    mandulia.view.z = math.max(mandulia.view.z, minimum.z)
    dz = mandulia.view.z
  end

end