Class: EasyGeometry::D2::Line
- Inherits:
-
LinearEntity
- Object
- LinearEntity
- EasyGeometry::D2::Line
- Defined in:
- lib/easy_geometry/d2/line.rb
Overview
An infinite line in 2-dimensional Euclidean space.
Instance Attribute Summary
Attributes inherited from LinearEntity
Instance Method Summary collapse
-
#==(other) ⇒ Object
Returns True if self and other are the same mathematical entities.
-
#a ⇒ Object
The coefficients ‘a’ for ax + by + c = 0.
-
#b ⇒ Object
The coefficients ‘b’ for ax + by + c = 0.
-
#c ⇒ Object
The coefficients ‘c’ for ax + by + c = 0.
-
#contains?(other) ⇒ Boolean
Is other GeometryEntity contained in this Line?.
-
#distance(other) ⇒ Object
Finds the shortest distance between a line and a point.
-
#equation ⇒ Object
The equation of the line: ax + by + c.
Methods inherited from LinearEntity
#angle_between, #direction, #initialize, #intersection, #parallel_line, #parallel_to?, #perpendicular_line, #perpendicular_segment, #perpendicular_to?, #projection_point, #similar_to?, #slope, #span_test
Constructor Details
This class inherits a constructor from EasyGeometry::D2::LinearEntity
Instance Method Details
#==(other) ⇒ Object
Returns True if self and other are the same mathematical entities.
Parameters:
GeometryEntity
47 48 49 50 |
# 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 ⇒ Object
The coefficients ‘a’ for ax + by + c = 0.
64 65 66 |
# File 'lib/easy_geometry/d2/line.rb', line 64 def a @a ||= self.p1.y - self.p2.y end |
#b ⇒ Object
The coefficients ‘b’ for ax + by + c = 0.
69 70 71 |
# File 'lib/easy_geometry/d2/line.rb', line 69 def b @b ||= self.p2.x - self.p1.x end |
#c ⇒ Object
The coefficients ‘c’ for ax + by + c = 0.
74 75 76 |
# 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) ⇒ Boolean
Is other GeometryEntity contained in this Line?
Parameters:
GeometryEntity or Array of Numeric(coordinates)
Returns:
true if `other` is on this Line.
false otherwise.
15 16 17 18 19 20 21 22 23 24 25 26 27 |
# 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) ⇒ Object
Finds the shortest distance between a line and a point.
Parameters:
Point or Array of Numeric(coordinates)
34 35 36 37 38 39 40 |
# 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 ⇒ Object
The equation of the line: ax + by + c.
53 54 55 56 57 58 59 60 61 |
# 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 |