plot_2d.lua


NAME
    plot_2d

NOTES
    2D plot package.

SOURCE

require("register")

local P = {}

if _REQUIREDNAME == nil then
    plot_2d = P
else
    _G[_REQUIREDNAME] = P
end

function P.new(width, height)
    P.width = width
    P.height = height
    
    local render, scene, root, font, plot = zeGrf.new("render", "scene", "node", "font", "plot")
    
    render:add(scene)
    render:set{size = {width, height}}
    scene:set{node = root, viewport = {0, 0, width, height}}
    root:add(plot)
    plot:font(font)

    P.render = render
    P.root = root
    P.plot = plot
    P.font = font
    
    P.xscale = .7
    P.yscale = .7
    plot:scale(P.xscale, P.yscale, 1)
    plot:set{axis = "x", offset = {0, -P.height * P.yscale / 2, 0}}
    plot:set{axis = "y", offset = {-P.width * P.xscale / 2, 0, 0}}
   
    return P
end

function P.show(self)
    require("towindow")
    towindow(self.render, self.width, self.height)
end

function P.save(self, fname)
    self.render:tofile(fname)
end

function P.scale(self, xscale, yscale)
    self.xscale = xscale
    self.yscale = yscale
    self.plot:scale(self.xscale, self.yscale, 1)
    self.plot:set{axis = "x", offset = {0, -self.height * self.yscale / 2, 0}}
    self.plot:set{axis = "y", offset = {-self.width * self.xscale / 2, 0, 0}}
end

function P.translate(self, dx, dy)
    self.root:translate(dx, dy, 0)
end

function P.add(self, object, position)
    if (position) then
        self.plot:add(object, position)
    else
        self.plot:add(object)
    end
end

function P.xaxis(self, options)
    options.axis = "x"
    self.plot:set(options)
end

function P.yaxis(self, options)
    options.axis = "y"
    self.plot:set(options)
end

function P.fontsize(self, size)
    self.plot:fontsize(size)
end

function P.right_yaxis(self)
    self.plot:set{axis = "y", offset = {self.width * self.xscale / 2, 0, 0}}
end

function P.top_xaxis(self)
    self.plot:set{axis = "x", offset = {0, self.height * self.yscale / 2, 0}}
end