# Class: Linear1::Graph

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

## Constant Summary collapse

ORIGIN =

The center of the graph

`{x: @@axis[:x] / 2, y: @@axis[:y] / 2}`
@@axis =
`{x: 30, y: 20}`

## Instance Method Summary collapse

• The values of the xy hash.

• A hash used to build the graph.

• constructor

A new instance of Graph.

• The keys of the xy hash.

• The array used to create the graph.

• The graph.

## Constructor Details

### #initialize(equation) ⇒ Graph

Returns a new instance of Graph

 ``` 4 5 6``` ```# File 'lib/linear1/graph.rb', line 4 def initialize equation @equation = equation end```

## Class Method Details

### .axis(key) ⇒ Object

 ``` 9 10 11``` ```# File 'lib/linear1/graph.rb', line 9 def self.axis key @@axis.fetch key end```

## Instance Method Details

### #domain ⇒ Array<Integer>

Returns the values of the xy hash

Returns:

• (Array<Integer>)

the values of the xy hash

 ``` 43 44 45``` ```# File 'lib/linear1/graph.rb', line 43 def domain # @return [Array] the values of the xy hash hash.values end```

### #hash ⇒ Hash<Integer, Integer>

Note:

Do not use as an xy table; values are modified

Returns a hash used to build the graph

Returns:

• (Hash<Integer, Integer>)

a hash used to build the graph

 ``` 33 34 35 36 37 38 39 40 41 42``` ```# File 'lib/linear1/graph.rb', line 33 def hash table = Hash.new (0..@@axis[:y]).to_a.each do |y| (0..@@axis[:x]).to_a.each do |x| x_exec = @equation.execute(x - ORIGIN[:x]) table[y] = x_exec if x_exec == y - ORIGIN[:y] end end table end```

### #range ⇒ Array<Integer>

Returns the keys of the xy hash

Returns:

• (Array<Integer>)

the keys of the xy hash

 ``` 47 48 49``` ```# File 'lib/linear1/graph.rb', line 47 def range # @return [Array] the keys of the xy hash hash.keys end```

### #to_a ⇒ Array<Array<Integer>>

Note:

Do not use as an XY table; the values are modified and not accurate

Returns the array used to create the graph

Returns:

• (Array<Array<Integer>>)

the array used to create the graph

 ``` 17 18 19 20 21 22 23 24 25 26 27 28``` ```# File 'lib/linear1/graph.rb', line 17 def to_a final, y = Array.new, 0 @@axis[:y].times do final[y], x = Array.new, 0 @@axis[:x].times do final[y][x] = init_coord(x, y) x += 1 end y += 1 end final.reverse end```

### #to_s ⇒ String

Returns the graph

Returns:

• (String)

the graph

 ``` 52 53 54 55 56 57 58 59 60 61``` ```# File 'lib/linear1/graph.rb', line 52 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 result.center 100 end```