Class: Termular::Graph::Polar

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



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/termular/graph.rb', line 130

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
    (options["tmin"] || 0).upto(((options["tmax"] || 2 * Math::PI) * 100).round).map { |t| t / 100.0 }.each do |theta|
      ctx["t"] = theta
      radius = expression.eval ctx
      next if radius.is_a? Complex or (radius.respond_to? :nan and radius.nan?)
      scr_pt = point_to_screen(radius*Math.cos(theta), radius*Math.sin(theta))
      if scr_pt[0] >= 0 and scr_pt[0] <= Console.cols and scr_pt[1] >= 0 and scr_pt[1] <= Console.rows
        buff << Console.move(scr_pt[0], scr_pt[1])
        buff << "+"
      end
    end
    buff << Console.reset
  end
end