class Cartesius::Line

Constants

HORIZONTAL_SLOPE
VERTICAL_SLOPE

Public Class Methods

ascending_bisector() click to toggle source
# File lib/cartesius/line.rb, line 56
def self.ascending_bisector
  create(slope: 1, known_term: 0)
end
by_points(point1:, point2:) click to toggle source
# 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:) click to toggle source
# 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_bisector() click to toggle source
# File lib/cartesius/line.rb, line 60
def self.descending_bisector
  create(slope: -1, known_term: 0)
end
horizontal(known_term:) click to toggle source
# File lib/cartesius/line.rb, line 23
def self.horizontal(known_term:)
  create(slope: HORIZONTAL_SLOPE, known_term: known_term)
end
new(x:, y:, k:) click to toggle source

equation type: dx + ey + f = 0

# 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
vertical(known_term:) click to toggle source
# File lib/cartesius/line.rb, line 27
def self.vertical(known_term:)
  new(x: 1, y: 0, k: -known_term.to_r)
end
x_axis() click to toggle source
# File lib/cartesius/line.rb, line 48
def self.x_axis
  horizontal(known_term: 0)
end
y_axis() click to toggle source
# File lib/cartesius/line.rb, line 52
def self.y_axis
  vertical(known_term: 0)
end

Public Instance Methods

==(line) click to toggle source
# File lib/cartesius/line.rb, line 158
def == (line)
  line.instance_of?(self.class) &&
      line.slope == slope && line.known_term == known_term
end
ascending?() click to toggle source
# File lib/cartesius/line.rb, line 108
def ascending?
  slope != VERTICAL_SLOPE && slope > HORIZONTAL_SLOPE
end
ascending_bisector?() click to toggle source
# File lib/cartesius/line.rb, line 88
def ascending_bisector?
  self == Line.ascending_bisector
end
congruent?(line) click to toggle source
# File lib/cartesius/line.rb, line 154
def congruent?(line)
  line.instance_of?(self.class)
end
descending?() click to toggle source
# File lib/cartesius/line.rb, line 112
def descending?
  slope < HORIZONTAL_SLOPE
end
descending_bisector?() click to toggle source
# File lib/cartesius/line.rb, line 92
def descending_bisector?
  self == Line.descending_bisector
end
horizontal?() click to toggle source
# File lib/cartesius/line.rb, line 96
def horizontal?
  slope == HORIZONTAL_SLOPE
end
inclined?() click to toggle source
# File lib/cartesius/line.rb, line 104
def inclined?
  ascending? || descending?
end
include?(point) click to toggle source
# 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_term() click to toggle source
# 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) click to toggle source
# File lib/cartesius/line.rb, line 116
def parallel?(line)
  line.slope == slope
end
perpendicular?(line) click to toggle source
# 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
slope() click to toggle source
# File lib/cartesius/line.rb, line 64
def slope
  if @y_coeff.zero?
    VERTICAL_SLOPE
  else
    numberfy(-@x_coeff, @y_coeff)
  end
end
to_equation() click to toggle source
# File lib/cartesius/line.rb, line 150
def to_equation
  equationfy('x' => @x_coeff, 'y' => @y_coeff, '1' => @k_coeff)
end
vertical?() click to toggle source
# File lib/cartesius/line.rb, line 100
def vertical?
  slope == VERTICAL_SLOPE
end
x_axis?() click to toggle source
# File lib/cartesius/line.rb, line 80
def x_axis?
  self == Line.x_axis
end
x_intercept() click to toggle source
# File lib/cartesius/line.rb, line 138
def x_intercept
  unless @x_coeff.zero?
    numberfy(-@k_coeff, @x_coeff)
  end
end
y_axis?() click to toggle source
# File lib/cartesius/line.rb, line 84
def y_axis?
  self == Line.y_axis
end
y_intercept() click to toggle source
# File lib/cartesius/line.rb, line 144
def y_intercept
  unless @y_coeff.zero?
    numberfy(-@k_coeff, @y_coeff)
  end
end

Private Instance Methods

validation() click to toggle source
# File lib/cartesius/line.rb, line 165
def validation
  if @x_coeff.zero? && @y_coeff.zero?
    raise ArgumentError.new('Invalid coefficients!')
  end
end