Class: Geometry::Path

Inherits:
Object
  • Object
show all
Defined in:
lib/geometry/path.rb

Overview

An object representing a set of connected elements, each of which could be an Edge or an Arc. Unlike a Polygon, a Path is not guaranteed to be closed.

Instance Attribute Summary collapse

Attributes collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Path

Construct a new Path from Geometry::Points, Edges, and Arcs Successive Geometry::Points will be converted to Edges.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/geometry/path.rb', line 14

def initialize(*args)
    args.map! {|a| (a.is_a?(Array) or a.is_a?(Vector)) ? Point[a] : a}
    args.each {|a| raise ArgumentError, "Unknown argument type #{a.class}" unless a.is_a?(Point) or a.is_a?(Edge) or a.is_a?(Arc) }

    @elements = []

    first = args.shift
    push first if first.is_a?(Edge) or first.is_a?(Arc)

    args.reduce(first) do |previous, n|
	case n
	    when Point
		case previous
		    when Point	    then push Edge.new(previous, n)
		    when Arc, Edge  then push Edge.new(previous.last, n) unless previous.last == n
		end
		last
	    when Edge
		case previous
		    when Point	    then push Edge.new(previous, n.first)
		    when Arc, Edge  then push Edge.new(previous.last, n.first) unless previous.last == n.first
		end
		push(n).last
	    when Arc
		case previous
		    when Point
			if previous == n.first
			    raise ArgumentError, "Duplicated point before an Arc"
			else
			    push Edge.new(previous, n.first)
			end
		    when Arc, Edge
			push Edge.new(previous.last, n.first) unless previous.last == n.first
		end
		push(n).last
	    else
		raise ArgumentError, "Unsupported argument type: #{n}"
	end
    end
end

Instance Attribute Details

#elements ⇒ Object (readonly)

Returns the value of attribute elements.



10
11
12
# File 'lib/geometry/path.rb', line 10

def elements
  @elements
end

Instance Method Details

#==(other) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/geometry/path.rb', line 55

def ==(other)
    if other.is_a?(Path)
	@elements == other.elements
    else
	super other
    end
end

#last ⇒ Geometry

Returns The last element in the Geometry::Path.

Returns:



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

def last
    @elements.last
end

#max ⇒ Point

Returns The upper-right corner of the bounding rectangle that encloses the Geometry::Path.

Returns:

  • (Point) —

    The upper-right corner of the bounding rectangle that encloses the Geometry::Path



66
67
68
# File 'lib/geometry/path.rb', line 66

def max
    elements.reduce(elements.first.max) {|memo, e| memo.max(e.max) }
end

#min ⇒ Point

Returns The lower-left corner of the bounding rectangle that encloses the Geometry::Path.

Returns:



71
72
73
# File 'lib/geometry/path.rb', line 71

def min
    elements.reduce(elements.first.min) {|memo, e| memo.min(e.max) }
end

#minmax ⇒ Array<Point>

Returns The lower-left and upper-right corners of the enclosing bounding rectangle.

Returns:

  • (Array<Point>) —

    The lower-left and upper-right corners of the enclosing bounding rectangle



76
77
78
# File 'lib/geometry/path.rb', line 76

def minmax
    elements.reduce(elements.first.minmax) {|memo, e| [memo.first.min(e.min), memo.last.max(e.max)] }
end

#push(arg) ⇒ Path

Append a new geometry element to the Geometry::Path

Returns:



89
90
91
92
# File 'lib/geometry/path.rb', line 89

def push(arg)
    @elements.push arg
    self
end