Method: Geometry::Path#initialize

Defined in:
lib/geometry/path.rb

#initialize(*args) ⇒ Path

Construct a new Path from Geometry::Points, Edges, and Arcs

Successive {Point}s will be converted to {Edge}s.


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