Class: Termular::Graph

Inherits:
Object
  • Object
show all
Defined in:
lib/termular/graph.rb

Direct Known Subclasses

Cartesian, ImplicitCartesian, ImplicitPolar, Polar

Defined Under Namespace

Classes: Cartesian, ImplicitCartesian, ImplicitPolar, Polar

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(expression) ⇒ Graph

Returns a new instance of Graph.



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/termular/graph.rb', line 5

def initialize(expression)
  @expression = expression
  @max_x = 10
  @min_x = -10
  factor = (Console.rows * 2.4 / Console.cols.to_f)
  @max_y = factor * @max_x
  @min_y = factor * @min_x
  @start_time = Time.now.to_f
  @options = {}
  invalidate
end

Instance Attribute Details

#expressionObject

Returns the value of attribute expression.



3
4
5
# File 'lib/termular/graph.rb', line 3

def expression
  @expression
end

#max_xObject

Returns the value of attribute max_x.



3
4
5
# File 'lib/termular/graph.rb', line 3

def max_x
  @max_x
end

#max_yObject

Returns the value of attribute max_y.



3
4
5
# File 'lib/termular/graph.rb', line 3

def max_y
  @max_y
end

#min_xObject

Returns the value of attribute min_x.



3
4
5
# File 'lib/termular/graph.rb', line 3

def min_x
  @min_x
end

#min_yObject

Returns the value of attribute min_y.



3
4
5
# File 'lib/termular/graph.rb', line 3

def min_y
  @min_y
end

#needs_redrawObject

Returns the value of attribute needs_redraw.



3
4
5
# File 'lib/termular/graph.rb', line 3

def needs_redraw
  @needs_redraw
end

#optionsObject

Returns the value of attribute options.



3
4
5
# File 'lib/termular/graph.rb', line 3

def options
  @options
end

#start_timeObject

Returns the value of attribute start_time.



3
4
5
# File 'lib/termular/graph.rb', line 3

def start_time
  @start_time
end

Instance Method Details

#centerObject



31
32
33
34
35
36
37
38
39
40
# File 'lib/termular/graph.rb', line 31

def center
  w = max_x - min_x
  h = max_y - min_y
  
  @max_x = w/2.0
  @min_x = -@max_x
  @max_y = h/2.0
  @min_y = -@max_y
  invalidate
end

#invalidateObject



17
18
19
# File 'lib/termular/graph.rb', line 17

def invalidate
  @needs_redraw = true
end

#pan(fx, fy) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/termular/graph.rb', line 21

def pan(fx, fy)
  x = (max_x - min_x) * fx
  y = (max_y - min_y) * fy
  @min_x += x
  @max_x += x
  @min_y += y
  @max_y += y
  invalidate
end

#point_to_screen(x, y) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/termular/graph.rb', line 59

def point_to_screen(x, y)
  sw = Console.cols
  sh = Console.rows
  pw = max_x - min_x
  ph = max_y - min_y
  sx = (((x - min_x) / pw.to_f) * sw).round
  sy = sh - (((y - min_y) / ph.to_f) * sh).round
  [sx, sy]
end

#render_axesObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/termular/graph.rb', line 79

def render_axes
  buff = Console.clear
  scr_origin = point_to_screen 0, 0
  buff << Console.color(:white)
  if scr_origin[0] >= 0 and scr_origin[0] <= Console.cols
    0.upto(Console.rows).each do |y|
      buff << Console.move(scr_origin[0], y)
      buff << "|"
    end
  end
  if scr_origin[1] >= 0 and scr_origin[1] <= Console.rows
    buff << Console.move(0, scr_origin[1])
    buff << "-" * Console.cols
    if scr_origin[0] >= 0 and scr_origin[0] <= Console.cols
      buff << Console.move(scr_origin[0], scr_origin[1])
      buff << "+"
    end
  end
  buff
end

#screen_to_point(x, y) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'lib/termular/graph.rb', line 69

def screen_to_point(x, y)
  sw = Console.cols
  sh = Console.rows
  pw = max_x - min_x
  ph = max_y - min_y
  sx = ((x / sw.to_f) * pw) + min_x
  sy = ((y / sh.to_f) * ph) + min_y
  [sx, sy]
end

#zoom(factor) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/termular/graph.rb', line 42

def zoom(factor)
  w = max_x - min_x
  h = max_y - min_y
  
  cx = min_x + w/2.0
  cy = min_y + h/2.0
  
  w /= factor.to_f
  h /= factor.to_f
  
  @min_x = cx - w/2.0
  @max_x = cx + w/2.0
  @min_y = cy - h/2.0
  @max_y = cy + h/2.0
  invalidate
end