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

Class Method Summary collapse

Methods included from ClusterFactory

included

Class Method Details

.[](Array, Array) ⇒ TwoPointLine .[](Point, Point) ⇒ TwoPointLine .[](Vector, Vector) ⇒ TwoPointLine .[](y-intercept, slope) ⇒ SlopeInterceptLine .[](point, slope) ⇒ PointSlopeLine

Overloads:



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/geometry/line.rb', line 44

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



80
81
82
# File 'lib/geometry/line.rb', line 80

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:



69
70
71
72
73
74
75
76
77
78
# File 'lib/geometry/line.rb', line 69

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



83
84
85
# File 'lib/geometry/line.rb', line 83

def self.vertical(x_intercept=0)
    SlopeInterceptLine.new(1/0.0, x_intercept)
end