Class: Silicium::Geometry::Line2dCanon
- Inherits:
-
Object
- Object
- Silicium::Geometry::Line2dCanon
- Defined in:
- lib/geometry.rb
Overview
Class represents a line as equation Ax + By + C = 0 in two-dimensional space
Instance Attribute Summary collapse
-
#free_coefficient ⇒ Object
readonly
Returns the value of attribute free_coefficient.
-
#x_coefficient ⇒ Object
readonly
Returns the value of attribute x_coefficient.
-
#y_coefficient ⇒ Object
readonly
Returns the value of attribute y_coefficient.
Instance Method Summary collapse
-
#array_of_points_is_on_line(array) ⇒ Object
Check if array of points is on the same line.
-
#check_point_on_segment(point) ⇒ Object
Checking if the point is on a segment.
-
#distance_between_parallel_lines(other_line) ⇒ Object
The distance between parallel lines.
-
#distance_point_to_line(point) ⇒ Object
The distance from a point to a line on a plane return 0 if the equation does not define a line.
-
#distance_to_line(other_line) ⇒ Object
Returns distance between lines.
-
#initialize(point1, point2) ⇒ Line2dCanon
constructor
Initializes with two objects of type Point.
-
#initialize_with_coefficients(a, b, c) ⇒ Object
Initializes with coefficients.
-
#intersecting?(other_line) ⇒ Boolean
Checks if two lines are intersecting.
-
#intersection_point(other_line) ⇒ Object
Returns a point of intersection of two lines If not intersecting returns nil.
-
#parallel?(other_line) ⇒ Boolean
Checks if two lines are parallel.
-
#perpendicular?(other_line) ⇒ Boolean
Checks if two lines are perpendicular.
-
#point_is_on_line?(point) ⇒ Boolean
Checks the point lies on the line or not.
Constructor Details
#initialize(point1, point2) ⇒ Line2dCanon
Initializes with two objects of type Point
29 30 31 32 33 34 35 36 37 38 39 40 41 |
# 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 |
Instance Attribute Details
#free_coefficient ⇒ Object (readonly)
Returns the value of attribute free_coefficient.
25 26 27 |
# File 'lib/geometry.rb', line 25 def free_coefficient @free_coefficient end |
#x_coefficient ⇒ Object (readonly)
Returns the value of attribute x_coefficient.
23 24 25 |
# File 'lib/geometry.rb', line 23 def x_coefficient @x_coefficient end |
#y_coefficient ⇒ Object (readonly)
Returns the value of attribute y_coefficient.
24 25 26 |
# File 'lib/geometry.rb', line 24 def y_coefficient @y_coefficient end |
Instance Method Details
#array_of_points_is_on_line(array) ⇒ Object
Check if array of points is on the same line
112 113 114 115 116 117 118 119 |
# 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) ⇒ Object
Checking if the point is on a segment
77 78 79 |
# 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) ⇒ Object
The distance between parallel lines
123 124 125 126 127 |
# 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) ⇒ Object
The distance from a point to a line on a plane return 0 if the equation does not define a line.
104 105 106 107 108 109 |
# 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) ⇒ Object
Returns distance between lines
95 96 97 98 99 |
# 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) ⇒ Object
Initializes with coefficients
44 45 46 47 48 49 50 |
# 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) ⇒ Boolean
Checks if two lines are intersecting
65 66 67 |
# 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) ⇒ Object
Returns a point of intersection of two lines If not intersecting returns nil
84 85 86 87 88 89 90 91 |
# 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) ⇒ Boolean
Checks if two lines are parallel
59 60 61 |
# 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) ⇒ Boolean
Checks if two lines are perpendicular
71 72 73 |
# 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) ⇒ Boolean
Checks the point lies on the line or not
54 55 56 |
# 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 |