Class: Termular::Graph::Cartesian

Inherits:
Termular::Graph show all
Defined in:
lib/termular/graph.rb

Instance Attribute Summary

Attributes inherited from Termular::Graph

#expression, #max_x, #max_y, #min_x, #min_y, #needs_redraw, #options, #start_time

Instance Method Summary collapse

Methods inherited from Termular::Graph

#center, #initialize, #invalidate, #pan, #point_to_screen, #render_axes, #screen_to_point, #zoom

Constructor Details

This class inherits a constructor from Termular::Graph

Instance Method Details

#renderObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/termular/graph.rb', line 101

def render
  @needs_redraw = false
  Console.buffered_print do |buff|
    buff << render_axes
    # render actual graph
    ctx = Context.new Context.global
    buff << Console.color(:green)
    ctx["time"] = Time.now.to_f - @start_time
    old_y = nil
    0.upto(Console.cols).map { |x| screen_to_point(x, 0)[0] }.each do |x|
      ctx["x"] = x
      py = expression.eval ctx
      next if py.is_a? Complex or (py.respond_to? :nan and py.nan?)
      scr_pt = point_to_screen(x, py)
      new_y = scr_pt[1]
      (old_y ? (old_y > new_y ? (old_y - 1).downto(new_y) : (old_y < new_y ? (old_y + 1).upto(new_y) : [new_y])) : [new_y]).each do |y|
        if scr_pt[0] >= 0 and scr_pt[0] <= Console.cols and y >= 0 and y <= Console.rows
          buff << Console.move(scr_pt[0], y)
          buff << "+"
        end
      end
      old_y = new_y
    end
    buff << Console.reset
  end
end