Class: Linear::Graph

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

Constant Summary collapse

@@axis =
{x: 50, y: 25}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(equation) ⇒ Graph


4
5
6
# File 'lib/linear/graph.rb', line 4

def initialize equation
      @equation = equation
end

Class Method Details

.axis(key) ⇒ Object


10
11
12
# File 'lib/linear/graph.rb', line 10

def self.axis(key)
  @@axis[key]
end

.originHash<Symbol => Integer>


27
28
29
# File 'lib/linear/graph.rb', line 27

def self.origin # @return [Hash<Symbol => Integer>] the center of the graph
  {x: @@axis[:x] / 2, y: @@axis[:y] / 2} 
end

.x_axis=(num) ⇒ Object


13
14
15
16
# File 'lib/linear/graph.rb', line 13

def self.x_axis=(num)
        caa(num)
        @@axis[:x] = num
end

.y_axisObject


18
19
20
# File 'lib/linear/graph.rb', line 18

def self.y_axis
  (0..(@@axis[:y])).to_a
end

.y_axis=(num) ⇒ Object


22
23
24
25
# File 'lib/linear/graph.rb', line 22

def self.y_axis=(num)
  caa num
  @@axis[:y] = num
end

Instance Method Details

#domainArray<Integer>


58
59
60
# File 'lib/linear/graph.rb', line 58

def domain # @return [Array<Integer>] the values of the xy hash
  xy.values
end

#rangeArray<Integer>


62
63
64
# File 'lib/linear/graph.rb', line 62

def range # @return [Array<Integer>] the keys of the xy hash
  xy.keys
end

#to_aObject


30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/linear/graph.rb', line 30

def to_a
      final = Array.new
      y = 0
      @@axis[:y].times do
        final[y] = Array.new
        x = 0
        @@axis[:x].times do
          final[y][x] = (to_hash[y] - 50 / 2) ? to_hash[y] : format_grid(x + Graph.origin[:x], y + Graph.origin[:y])
          x += 1
        end
        y += 1
  end
  return final.reverse
end

#to_hashObject Also known as: to_h


46
47
48
49
50
51
52
53
54
55
56
# File 'lib/linear/graph.rb', line 46

def to_hash
  table = Hash.new
  for y in (0..@@axis[:y]).to_a
    for x in (0..@@axis[:x]).to_a
      x_exec = @equation.execute x - Graph.origin[:x]
      y_exec = y - Graph.origin[:y]
      table[y] = x_exec if x_exec == y_exec
    end
  end
  return table
end

#to_sString


67
68
69
70
71
72
73
74
75
76
# File 'lib/linear/graph.rb', line 67

def to_s
  result = String.new
  for y_index in to_a
    for x_index in y_index
      result << x_index
    end
    result << ?\n
  end
  return result.center 80
end