class Silicium::Geometry::Line2dCanon

Class represents a line as equation y = k*x +b k - slope b - free_term in two-dimensional space

Attributes

free_term[R]
slope[R]

Public Class Methods

new(p1, p2) click to toggle source
# File lib/geometry.rb, line 31
def initialize(p1, p2)
  if (p1.x == p2.x) && (p1.y == p2.y)
    raise ArgumentError, "You need 2 diffrent points"
  end
  if (p1.x == p2.x)
    raise ArgumentError, "The straight line equation cannot be written in canonical form"
  end
  @slope = (p2.y - p1.y) / (p2.x - p1.x).to_f
  @free_term = (p2.x * p1.y - p2.y * p1.x) / (p2.x - p1.x).to_f
end

Public Instance Methods

point_is_on_line?(p1) click to toggle source

Checks the point lies on the line or not

# File lib/geometry.rb, line 44
def point_is_on_line?(p1)
  p1.y == @slope * p1.x + @free_term
end