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}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(equation) ⇒ 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

#domainArray<Integer>



43
44
45
# File 'lib/linear1/graph.rb', line 43

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

#hashHash<Integer, Integer>

Note:

Do not use as an xy table; values are modified

Returns a hash used to build the graph

See Also:



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

#rangeArray<Integer>



47
48
49
# File 'lib/linear1/graph.rb', line 47

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

#to_aArray<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



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_sString



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