class Silicium::Geometry::Line2dCanon
Class represents a line as equation Ax + By + C = 0 in two-dimensional space
Attributes
free_coefficient[R]
x_coefficient[R]
y_coefficient[R]
Public Class Methods
new(point1, point2)
click to toggle source
Initializes with two objects of type Point
# File lib/geometry.rb, line 29 def initialize(point1, point2) raise ArgumentError, 'You need 2 different points' if point1.x.equal?(point2.x) && point1.y.equal?(point2.y) if point1.x.equal?(point2.x) @x_coefficient = 1 @y_coefficient = 0 @free_coefficient = - point1.x else slope_point = (point2.y - point1.y) / (point2.x - point1.x) @x_coefficient = -slope_point @y_coefficient = 1 @free_coefficient = - point1.y + slope_point * point1.x end end
Public Instance Methods
array_of_points_is_on_line(array)
click to toggle source
Check if array of points is on the same line
# File lib/geometry.rb, line 112 def array_of_points_is_on_line(array) raise ArgumentError, 'Array is empty!' if array.length == 0 res = Array.new for i in 0..array.size-1 do res.push(point_is_on_line?(array[i])) end res end
check_point_on_segment(point)
click to toggle source
Checking if the point is on a segment
# File lib/geometry.rb, line 77 def check_point_on_segment(point) (@x_coefficient * point.x + @y_coefficient * point.y + @free_coefficient) - 0.0 <= 0.0001 end
distance_between_parallel_lines(other_line)
click to toggle source
The distance between parallel lines
# File lib/geometry.rb, line 123 def distance_between_parallel_lines(other_line) raise ArgumentError, 'Lines are not parallel' if !parallel?(other_line) (other_line.free_coefficient - @free_coefficient).abs / Math.sqrt(@x_coefficient**2 + @y_coefficient**2) end
distance_point_to_line(point)
click to toggle source
The distance from a point to a line on a plane return 0 if the equation does not define a line.
# File lib/geometry.rb, line 104 def distance_point_to_line(point) return 0 if @x_coefficient.eql?(0) && @y_coefficient.eql?(0) res = (@x_coefficient * point.x + @y_coefficient * point.y + @free_coefficient).abs res / Math.sqrt(@x_coefficient**2 + @y_coefficient**2).to_f end
distance_to_line(other_line)
click to toggle source
Returns distance between lines
# File lib/geometry.rb, line 95 def distance_to_line(other_line) return 0 if intersecting?(other_line) (@free_coefficient - other_line.free_coefficient).abs / Math.sqrt(@x_coefficient**2 + @y_coefficient**2) end
initialize_with_coefficients(a, b, c)
click to toggle source
Initializes with coefficients
# File lib/geometry.rb, line 44 def initialize_with_coefficients(a, b, c) raise ArgumentError, 'All coefficients cannot be 0 ' if a.equal?(0) && b.equal?(0) && (c.equal?(0) || !c.equal?(0)) @x_coefficient = a @y_coefficient = b @free_coefficient = c end
intersecting?(other_line)
click to toggle source
Checks if two lines are intersecting
# File lib/geometry.rb, line 65 def intersecting?(other_line) @x_coefficient != other_line.x_coefficient || @y_coefficient != other_line.y_coefficient end
intersection_point(other_line)
click to toggle source
Returns a point of intersection of two lines If not intersecting returns nil
# File lib/geometry.rb, line 84 def intersection_point(other_line) return nil unless intersecting?(other_line) divisor = @x_coefficient * other_line.y_coefficient - other_line.x_coefficient * @y_coefficient x = (@y_coefficient * other_line.free_coefficient - other_line.y_coefficient * @free_coefficient) / divisor y = (@free_coefficient * other_line.x_coefficient - other_line.free_coefficient * @x_coefficient) / divisor Point.new(x, y) end
parallel?(other_line)
click to toggle source
Checks if two lines are parallel
# File lib/geometry.rb, line 59 def parallel?(other_line) @x_coefficient.equal?(other_line.x_coefficient) && @y_coefficient.equal?(other_line.y_coefficient) end
perpendicular?(other_line)
click to toggle source
Checks if two lines are perpendicular
# File lib/geometry.rb, line 71 def perpendicular?(other_line) (@x_coefficient * other_line.x_coefficient).equal?(- @y_coefficient * other_line.y_coefficient) end
point_is_on_line?(point)
click to toggle source
Checks the point lies on the line or not
# File lib/geometry.rb, line 54 def point_is_on_line?(point) ((@x_coefficient * point.x + @y_coefficient * point.y + @free_coefficient) - 0.0).abs < 0.0001 end