World Conquest Chronicles

World Conquest Chronicles

luaplot, v1.0

The library that implements a model of a 2D plot with support for displaying functions of time (as in an oscilloscope).

Major version.

Features

  • models:
    • 2D plot:
      • storing:
        • values of a displayed function;
        • limits for these values;
      • operations:
        • initializing:
          • filling a specified count of values by a bottom limit;
        • iterating over values:
          • via the __ipairs metamethod (for Lua 5.2);
          • via the __index metamethod (for Lua 5.3+);
        • adding a new value:
          • specific;
          • with a shift from the last value:
            • with a specific shift;
            • with a random shift;
            • using a bottom limit as the last value if no values;
          • clamping the added value to specified limits;
        • removing the first value:
          • returning the removed value:
            • returning a bottom limit if no values.

Installation

Clone this repository:

$ git clone https://github.com/thewizardplusplus/luaplot.git
$ cd luaplot

Install the library with the LuaRocks tool:

$ luarocks make

Examples

luaplot.Plot:

local types = require("luaplot.types")
local Plot = require("luaplot.plot")

local function print_plot(plot, vertical_step)
  assert(types.is_instance(plot, Plot))
  assert(types.is_number_with_limits(vertical_step, 0, 1))

  local text = ""
  for height = 1, 0, -vertical_step do
    for _, point in ipairs(plot) do
      local delta = math.abs(point - height)
      local symbol = delta < vertical_step / 2 and "*" or "."
      text = text .. symbol
    end

    text = text .. "\n"
  end

  print(text)
end

local function sleep(seconds)
  assert(types.is_number_with_limits(seconds, 0))

  local start = os.clock()
  while os.clock() - start < seconds do end
end

local WIDTH = 40
local HEIGHT = 10
local NOISE_FACTOR = 1.5
local PRINT_DELAY = 0.1

math.randomseed(os.time())

local plot = Plot:new(0)
for _ = 1, WIDTH do
  plot:push(0.5)
end

local vertical_step = 1 / HEIGHT
while true do
  plot:shift()
  plot:push_with_random_factor(NOISE_FACTOR * vertical_step)

  print_plot(plot, vertical_step)
  sleep(PRINT_DELAY)
end

Repository

Link: https://github.com/thewizardplusplus/luaplot/tree/v1.0.

Content: code.

License: MIT.

Screenshots

Random plot