Class: Geometry::Line

Inherits:
Object
  • Object
show all
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

PointSlopeLine, SlopeInterceptLine, TwoPointLine

Instance Attribute Summary collapse

Class Method Summary collapse

Methods included from ClusterFactory

included

Instance Attribute Details

#horizontal?Boolean (readonly)

Returns true if the slope is zero.

Returns:

  • (Boolean)

    true if the slope is zero



# File 'lib/geometry/line.rb', line 34

#slopeNumber (readonly)

Returns the slope of the Geometry::Line.

Returns:



# File 'lib/geometry/line.rb', line 37

#vertical?Boolean (readonly)

Returns true if the slope is infinite.

Returns:

  • (Boolean)

    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

Overloads:



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

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

Returns:



78
79
80
81
82
83
84
85
86
87
# File 'lib/geometry/line.rb', line 78

def self.new(options={})
    from = options[:from] || options[:start]
    to = options[:end] || options[: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