Class: CoordinatePlane

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x_int, y_int) ⇒ CoordinatePlane


40
41
42
# File 'lib/coordinate_plane.rb', line 40

def initialize(x_int, y_int)
      @x_intercept, @y_intercept = x_int, y_int
end

Instance Attribute Details

#x_interceptInteger (readonly) Also known as: zero, solution


33
34
35
# File 'lib/coordinate_plane.rb', line 33

def x_intercept
  @x_intercept
end

#y_interceptObject (readonly) Also known as: b

Returns the value of attribute y_intercept


37
38
39
# File 'lib/coordinate_plane.rb', line 37

def y_intercept
  @y_intercept
end

Class Method Details

.borders=(bool) ⇒ Object

Raises:

  • (ArgumentError)

27
28
29
30
# File 'lib/coordinate_plane.rb', line 27

def self.borders=(bool)
  raise ArgumentError, "Argument must be true or false" unless bool == true | false
  @@borders = bool
end

.has_borders?Boolean


23
24
25
# File 'lib/coordinate_plane.rb', line 23

def self.has_borders?
  @@borders
end

.x_axisObject


5
6
7
# File 'lib/coordinate_plane.rb', line 5

def self.x_axis
    @@x_axis
end

.x_axis=(num) ⇒ Object


9
10
11
12
# File 'lib/coordinate_plane.rb', line 9

def self.x_axis=(num)
    caa(num)
    @@x_axis = num
end

.y_axisObject


14
15
16
# File 'lib/coordinate_plane.rb', line 14

def self.y_axis
  @@y_axis
end

.y_axis=(num) ⇒ Object


18
19
20
21
# File 'lib/coordinate_plane.rb', line 18

def self.y_axis=(num)
  caa num
  @@y_axis = num
end

Instance Method Details

#domainArray<Integer>


79
80
81
# File 'lib/coordinate_plane.rb', line 79

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

#eval_equation(x) ⇒ Object


106
107
108
# File 'lib/coordinate_plane.rb', line 106

def eval_equation(x)
  slope * x + @y_intercept
end

#functionString


102
103
104
# File 'lib/coordinate_plane.rb', line 102

def function # @return [String] the function used to plot the line
  "f(x) = #{slope}x + #{@y_intercept}"
end

#graphObject


52
53
54
55
56
57
58
59
60
61
62
# File 'lib/coordinate_plane.rb', line 52

def graph
  cp, x = raw_graph, 0
  for x_index in cp
    y = 0
    for y_index in x_index
      cp[x][y] = format_graph(x, y)
      y += 1
    end
    x += 1
  end
end

#rangeArray<Integer>


83
84
85
# File 'lib/coordinate_plane.rb', line 83

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

#raw_graphObject


48
49
50
# File 'lib/coordinate_plane.rb', line 48

def raw_graph
  Array.new(@@x_axis) {Array.new @@y_axis}
end

#slopeObject


44
45
46
# File 'lib/coordinate_plane.rb', line 44

def slope
  @y_intercept / @x_intercept
end

#to_sString

Displays graph


91
92
93
94
95
96
97
98
99
100
# File 'lib/coordinate_plane.rb', line 91

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

#xyHash

Note:

There are no floats

The XY table


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

def xy
  x, table = 0, Hash.new
  until x > @@x_axis
    y = eval_equation(x)
    table[x] = y unless y.instance_of?(Float)
    x += 1
  end
  return table
end