Class: Cartesius::Line

Inherits:
Object
  • Object
show all
Includes:
Numerificator
Defined in:
lib/cartesius/line.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x:, y:, k:) ⇒ Line

equation type: dx + ey + f = 0



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

def initialize(x:, y:, k:)
  @x_coeff, @y_coeff, @k_coeff = x.to_r, y.to_r, k.to_r
  validation
end

Class Method Details

.ascending_bisectorObject



56
57
58
# File 'lib/cartesius/line.rb', line 56

def self.ascending_bisector
  create(slope: 1, known_term: 0)
end

.by_points(point1:, point2:) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/cartesius/line.rb', line 31

def self.by_points(point1:, point2:)
  if point1 == point2
    raise ArgumentError.new('Points must be different!')
  end

  if point1.x == point2.x
    return vertical(known_term: point1.x)
  else
    m, q = Cramer.solution2(
        [point1.x, 1],
        [point2.x, 1],
        [point1.y, point2.y]
    )
    create(slope: m, known_term: q)
  end
end

.create(slope:, known_term:) ⇒ Object



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

def self.create(slope:, known_term:)
  new(x: -slope.to_r, y: 1, k: -known_term.to_r)
end

.descending_bisectorObject



60
61
62
# File 'lib/cartesius/line.rb', line 60

def self.descending_bisector
  create(slope: -1, known_term: 0)
end

.horizontal(known_term:) ⇒ Object



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

def self.horizontal(known_term:)
  create(slope: HORIZONTAL_SLOPE, known_term: known_term)
end

.vertical(known_term:) ⇒ Object



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

def self.vertical(known_term:)
  new(x: 1, y: 0, k: -known_term.to_r)
end

.x_axisObject



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

def self.x_axis
  horizontal(known_term: 0)
end

.y_axisObject



52
53
54
# File 'lib/cartesius/line.rb', line 52

def self.y_axis
  vertical(known_term: 0)
end

Instance Method Details

#==(line) ⇒ Object



158
159
160
161
# File 'lib/cartesius/line.rb', line 158

def == (line)
  line.instance_of?(self.class) and
      line.slope == slope and line.known_term == known_term
end

#ascending?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/cartesius/line.rb', line 108

def ascending?
  slope != VERTICAL_SLOPE and slope > HORIZONTAL_SLOPE
end

#ascending_bisector?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/cartesius/line.rb', line 88

def ascending_bisector?
  self == Line.ascending_bisector
end

#congruent?(line) ⇒ Boolean

Returns:

  • (Boolean)


154
155
156
# File 'lib/cartesius/line.rb', line 154

def congruent?(line)
  line.instance_of?(self.class)
end

#descending?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/cartesius/line.rb', line 112

def descending?
  slope < HORIZONTAL_SLOPE
end

#descending_bisector?Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/cartesius/line.rb', line 92

def descending_bisector?
  self == Line.descending_bisector
end

#horizontal?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/cartesius/line.rb', line 96

def horizontal?
  slope == HORIZONTAL_SLOPE
end

#inclined?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/cartesius/line.rb', line 104

def inclined?
  ascending? or descending?
end

#include?(point) ⇒ Boolean

Returns:

  • (Boolean)


130
131
132
133
134
135
136
# File 'lib/cartesius/line.rb', line 130

def include?(point)
  if vertical?
    point.x == known_term
  else
    point.y == slope * point.x + known_term
  end
end

#known_termObject



72
73
74
75
76
77
78
# File 'lib/cartesius/line.rb', line 72

def known_term
  if @y_coeff.zero?
    numberfy(-@k_coeff, @x_coeff)
  else
    numberfy(-@k_coeff, @y_coeff)
  end
end

#parallel?(line) ⇒ Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/cartesius/line.rb', line 116

def parallel?(line)
  line.slope == slope
end

#perpendicular?(line) ⇒ Boolean

Returns:

  • (Boolean)


120
121
122
123
124
125
126
127
128
# File 'lib/cartesius/line.rb', line 120

def perpendicular?(line)
  if line.slope == HORIZONTAL_SLOPE
    slope == VERTICAL_SLOPE
  elsif line.slope == VERTICAL_SLOPE
    slope == HORIZONTAL_SLOPE
  else
    line.slope * slope == -1
  end
end

#slopeObject



64
65
66
67
68
69
70
# File 'lib/cartesius/line.rb', line 64

def slope
  if @y_coeff.zero?
    VERTICAL_SLOPE
  else
    numberfy(-@x_coeff, @y_coeff)
  end
end

#to_equationObject



150
151
152
# File 'lib/cartesius/line.rb', line 150

def to_equation
  equationfy('x' => @x_coeff, 'y' => @y_coeff, '1' => @k_coeff)
end

#vertical?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/cartesius/line.rb', line 100

def vertical?
  slope == VERTICAL_SLOPE
end

#x_axis?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/cartesius/line.rb', line 80

def x_axis?
  self == Line.x_axis
end

#x_interceptObject



138
139
140
141
142
# File 'lib/cartesius/line.rb', line 138

def x_intercept
  unless @x_coeff.zero?
    numberfy(-@k_coeff, @x_coeff)
  end
end

#y_axis?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/cartesius/line.rb', line 84

def y_axis?
  self == Line.y_axis
end

#y_interceptObject



144
145
146
147
148
# File 'lib/cartesius/line.rb', line 144

def y_intercept
  unless @y_coeff.zero?
    numberfy(-@k_coeff, @y_coeff)
  end
end