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: 75, y: 25}

Class Method Summary collapse

Instance Method Summary collapse

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[key]
end

Instance Method Details

#domainArray<Integer>

Returns the values of the xy hash.

Returns:

  • (Array<Integer>)

    the values of the xy hash



41
42
43
# File 'lib/linear1/graph.rb', line 41

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

#rangeArray<Integer>

Returns the keys of the xy hash.

Returns:

  • (Array<Integer>)

    the keys of the xy hash



45
46
47
# File 'lib/linear1/graph.rb', line 45

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

#to_aObject



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/linear1/graph.rb', line 15

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_hashObject Also known as: to_h, xy



29
30
31
32
33
34
35
36
37
38
# File 'lib/linear1/graph.rb', line 29

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

#to_sString

Returns the graph.

Returns:

  • (String)

    the graph



50
51
52
53
54
55
56
57
58
59
# File 'lib/linear1/graph.rb', line 50

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