class EasyGeometry::D2::Line

An infinite line in 2-dimensional Euclidean space.

Public Instance Methods

==(other) click to toggle source

Returns True if self and other are the same mathematical entities.

Parameters:

GeometryEntity
# File lib/easy_geometry/d2/line.rb, line 47
def ==(other)
  return false unless other.is_a?(Line)
  Point.is_collinear?(self.p1, other.p1, self.p2, other.p2)
end
a() click to toggle source

The coefficients 'a' for ax + by + c = 0.

# File lib/easy_geometry/d2/line.rb, line 64
def a
  @a ||= self.p1.y - self.p2.y
end
b() click to toggle source

The coefficients 'b' for ax + by + c = 0.

# File lib/easy_geometry/d2/line.rb, line 69
def b
  @b ||= self.p2.x - self.p1.x
end
c() click to toggle source

The coefficients 'c' for ax + by + c = 0.

# File lib/easy_geometry/d2/line.rb, line 74
def c
  @c ||= self.p1.x * self.p2.y - self.p1.y * self.p2.x
end
contains?(other) click to toggle source

Is other GeometryEntity contained in this Line?

Parameters:

GeometryEntity or Array of Numeric(coordinates)

Returns:

true if `other` is on this Line.
false otherwise.
# File lib/easy_geometry/d2/line.rb, line 15
def contains?(other)
  other = Point.new(other[0], other[1]) if other.is_a?(Array)
  
  if other.is_a?(Point)
    return Point.is_collinear?(other, self.p1, self.p2)    
  end

  if other.is_a?(LinearEntity)
    return Point.is_collinear?(other.p1, other.p2, self.p1, self.p2)
  end

  return false
end
distance(other) click to toggle source

Finds the shortest distance between a line and a point.

Parameters:

Point or Array of Numeric(coordinates)
# File lib/easy_geometry/d2/line.rb, line 34
def distance(other)
  other = Point.new(other[0], other[1]) if other.is_a?(Array)
  raise TypeError, "Distance between Line and #{ other.class } is not defined" unless other.is_a?(Point)

  return 0 if self.contains?(other)
  self.perpendicular_segment(other).length
end
equation() click to toggle source

The equation of the line: ax + by + c.

# File lib/easy_geometry/d2/line.rb, line 53
def equation
  if p1.x == p2.x
    return "x - #{p1.x}"
  elsif p1.y == p2.y
    return "#{p2.y} - p1.y"
  end

  "#{a}*x + #{b}*y + #{c} = 0"
end