Class: Geometry::Line
- Inherits:
-
Object
- Object
- Geometry::Line
- Includes:
- ClusterFactory
- Defined in:
- lib/geometry/line.rb
Overview
A cluster of objects representing a Line of infinite length
Supports two-point, slope-intercept, and point-slope initializer forms
Usage
Two-point constructors
line = Geometry::Line[[0,0], [10,10]]
line = Geometry::Line[Geometry::Point[0,0], Geometry::Point[10,10]]
line = Geometry::Line[Vector[0,0], Vector[10,10]]
Slope-intercept constructors
Geometry::Line[Rational(3,4), 5] # Slope = 3/4, Intercept = 5
Geometry::Line[0.75, 5]
Point-slope constructors
Geometry::Line(Geometry::Point[0,0], 0.75)
Geometry::Line(Vector[0,0], Rational(3,4))
Special constructors (2D only)
Geometry::Line.horizontal(y=0)
Geometry::Line.vertical(x=0)
Direct Known Subclasses
Instance Attribute Summary collapse
-
#horizontal? ⇒ Boolean
readonly
True if the slope is zero.
-
#slope ⇒ Number
readonly
The slope of the Line.
-
#vertical? ⇒ Boolean
readonly
True if the slope is infinite.
Class Method Summary collapse
- .[](*args) ⇒ Object
- .horizontal(y_intercept = 0) ⇒ Object
- .new(options = {}) ⇒ TwoPointLine
- .vertical(x_intercept = 0) ⇒ Object
Methods included from ClusterFactory
Instance Attribute Details
#horizontal? ⇒ Boolean (readonly)
Returns true if the slope is zero.
|
# File 'lib/geometry/line.rb', line 34
|
#slope ⇒ Number (readonly)
Returns the slope of the Geometry::Line.
|
# File 'lib/geometry/line.rb', line 37
|
#vertical? ⇒ Boolean (readonly)
Returns true if the slope is infinite.
|
# File 'lib/geometry/line.rb', line 40
|
Class Method Details
.[](Array, Array) ⇒ TwoPointLine .[](Point, Point) ⇒ TwoPointLine .[](Vector, Vector) ⇒ TwoPointLine .[](y-intercept, slope) ⇒ SlopeInterceptLine .[](point, slope) ⇒ PointSlopeLine
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/geometry/line.rb', line 53 def self.[](*args) if( 2 == args.size ) args.map! {|x| x.is_a?(Array) ? Point[*x] : x} # If both args are Points, create a TwoPointLine return TwoPointLine.new(*args) if args.all? {|x| x.is_a?(Vector)} # If only the first arg is a Point, create a PointSlopeLine return PointSlopeLine.new(*args) if args.first.is_a?(Vector) # Otherise, create a SlopeInterceptLine return SlopeInterceptLine.new(*args) else nil end end |
.horizontal(y_intercept = 0) ⇒ Object
89 90 91 |
# File 'lib/geometry/line.rb', line 89 def self.horizontal(y_intercept=0) SlopeInterceptLine.new(0, y_intercept) end |
.new(from, to) ⇒ TwoPointLine .new(start, end) ⇒ TwoPointLine
78 79 80 81 82 83 84 85 86 87 |
# File 'lib/geometry/line.rb', line 78 def self.new(={}) from = [:from] || [:start] to = [:end] || [:to] if from and to TwoPointLine.new(from, to) else raise ArgumentError, "Start and end Points must be provided" end end |
.vertical(x_intercept = 0) ⇒ Object
92 93 94 |
# File 'lib/geometry/line.rb', line 92 def self.vertical(x_intercept=0) SlopeInterceptLine.new(1/0.0, x_intercept) end |