World Conquest Chronicles

World Conquest Chronicles

luaplot, v1.3

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

Selecting a value by a distance between two 2D plots/oscillograms in the specific index.

Change Log

  • global operations:
    • selecting a value by a distance between two 2D plots/oscillograms in the specific index:
      • selecting by the specified sequential distance ranges;
  • refactoring:
    • using the cpml library:
      • using the cpml.utils.clamp() function;
      • using the cpml.utils.lerp() function;
  • examples:
    • colorizing the plots in the examples;
    • specifying the versions of the example dependencies.

Features

  • models:
    • 2D plot:
      • storing:
        • values of a displayed function;
        • limits for these values;
        • default value;
      • operations:
        • initializing:
          • filling a specified count of values by a specified default value;
        • iterating over values:
          • via the __ipairs metamethod (for Lua 5.2);
          • via the __index metamethod (for Lua 5.3+):
            • support of fractional indexes;
        • adding a new value:
          • specific;
          • with a shift from the last value:
            • with a specific shift;
            • with a random shift;
            • using a specified default value as the last value if no values;
          • clamping the added value to specified limits;
        • removing the first value:
          • returning the removed value:
            • returning a specified default value if no values;
    • 2D oscillogram:
      • extending the 2D plot model;
      • kinds:
        • custom (adding a specific new value);
        • linear (adding a new value with a specific shift from the last value);
        • random (adding a new value with a random shift from the last value);
      • operations:
        • updating:
          • first step: adding a new value;
          • second step: removing the first value;
    • 2D plot/oscillogram iterator:
      • storing:
        • 2D plot/oscillogram;
        • transformer for iterated values;
      • iterating over values of 2D plot/oscillogram:
        • via the __ipairs metamethod (for Lua 5.2);
        • via the __index metamethod (for Lua 5.3+);
      • applying transformations to iterated values via a specified transformer;
    • factory of a 2D plot/oscillogram iterator:
      • storing:
        • transformer for iterated values;
      • generating a 2D plot/oscillogram iterator for a specific 2D plot/oscillogram;
  • global operations:
    • calculating a difference (a distance) between two 2D plots/oscillograms in the same index:
      • returning a difference (a distance) by modulo (optionally);
    • selecting a value by a distance between two 2D plots/oscillograms in the specific index:
      • selecting by the specified sequential distance ranges.

Installation

1. Install the cpml library

Clone its repository:

$ git clone https://github.com/excessive/cpml.git
$ cd cpml

Reset to the required version:

git reset --hard v1.0.0

Apply the patch:

git apply << EOF
diff --git a/cpml-scm-1.rockspec b/cpml-1.0.0-1.rockspec
similarity index 95%
rename from cpml-scm-1.rockspec
rename to cpml-1.0.0-1.rockspec
index 0ad4055..0b0f414 100644
--- a/cpml-scm-1.rockspec
+++ b/cpml-1.0.0-1.rockspec
@@ -1,5 +1,5 @@
 package = "cpml"
-version = "scm-1"
+version = "1.0.0-1"
 source = {
    url = "git://github.com/excessive/cpml.git"
 }
@@ -10,7 +10,7 @@ description = {
    license = "MIT"
 }
 dependencies = {
-   "lua ~> 5.1"
+   "lua >= 5.1, < 5.4",
 }
 build = {
    type = "builtin",
EOF

Install the library with the LuaRocks tool:

$ luarocks make

2. Install this library

Clone this repository:

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

Install the library with the LuaRocks tool:

$ luarocks make

Examples

Distance detection:

local colors = require("ansicolors")
local types = require("luaplot.types")
local iterators = require("luaplot.iterators")
local Plot = require("luaplot.plot")
local Oscillogram = require("luaplot.oscillogram")
local DistanceLimit = require("luaplot.distancelimit")

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 colors("%{cyan}*%{reset}")
        or "."
      text = text .. symbol
    end

    text = text .. "\n"
  end

  io.write(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 SPEED = 0.25
local WIDTH = 40
local HEIGHT = 5
local VERTICAL_STEP = 1 / HEIGHT
local PRINT_DELAY = 1 / (SPEED * WIDTH)
local FACTOR = 1.5 * PRINT_DELAY

math.randomseed(os.time())

local plot_one = Oscillogram:new("random", WIDTH, 0.5)
local plot_two = Oscillogram:new("random", WIDTH, 0.5)
while true do
  plot_one:update(FACTOR)
  plot_two:update(FACTOR)

  local distance_marks = {}
  for index = 1, WIDTH do
    local distance_color =
      iterators.select_by_distance(plot_one, plot_two, index, {
        DistanceLimit:new(0.33, "green"),
        DistanceLimit:new(0.66, "yellow"),
        DistanceLimit:new(math.huge, "red"),
      })
    local distance_mark =
      colors(string.format("%%{%s}#%%{reset}", distance_color))
    table.insert(distance_marks, distance_mark)
  end

  print_plot(plot_one, VERTICAL_STEP)
  print_plot(plot_two, VERTICAL_STEP)
  for _ = 1, HEIGHT do
    io.write(table.concat(distance_marks, "") .. "\n")
  end
  io.write("\n")

  sleep(PRINT_DELAY)
end

Repository

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

Content: code.

License: MIT.