Class: Geometry::PointZero

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

Overview

An object repesenting a Point at the origin in N-dimensional space

A PointZero object is a Point that will always compare equal to zero and unequal to everything else, regardless of size. You can think of it as an application of the Null Object Pattern.

Accessors collapse

Accessors collapse

Unary operators collapse

Enumerable collapse

Instance Method Summary collapse

Instance Attribute Details

#xObject (readonly)



52
53
54
# File 'lib/geometry/point_zero.rb', line 52

def x
    0
end

#yObject (readonly)



58
59
60
# File 'lib/geometry/point_zero.rb', line 58

def y
    0
end

#zObject (readonly)



64
65
66
# File 'lib/geometry/point_zero.rb', line 64

def z
    0
end

Instance Method Details

#*(other) ⇒ Object



153
154
155
# File 'lib/geometry/point_zero.rb', line 153

def *(other)
    self
end

#+(other) ⇒ Object



131
132
133
134
135
136
137
138
139
# File 'lib/geometry/point_zero.rb', line 131

def +(other)
    case other
	when Array then other
	when Numeric
	    Point.iso(other)
	else
	    Point[other]
    end
end

#+@Object



122
123
124
# File 'lib/geometry/point_zero.rb', line 122

def +@
    self
end

#-(other) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
# File 'lib/geometry/point_zero.rb', line 141

def -(other)
    if other.is_a? Size
	-Point[other]
    elsif other.is_a? Numeric
	Point.iso(-other)
    elsif other.respond_to? :-@
	-other
    elsif other.respond_to? :map
	other.map {|a| -a }
    end
end

#-@Object



126
127
128
# File 'lib/geometry/point_zero.rb', line 126

def -@
    self
end

#/(other) ⇒ Object



157
158
159
160
161
# File 'lib/geometry/point_zero.rb', line 157

def /(other)
    raise OperationNotDefined unless other.is_a? Numeric
    raise ZeroDivisionError if 0 == other
    self
end

#[](i) ⇒ Numeric

Returns Element i (starting at 0).

Parameters:

Returns:

  • (Numeric)

    Element i (starting at 0)



46
47
48
# File 'lib/geometry/point_zero.rb', line 46

def [](i)
    0
end

#coerce(other) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/geometry/point_zero.rb', line 21

def coerce(other)
    if other.is_a? Numeric
	[other, 0]
    elsif other.is_a? Array
	[other, Array.new(other.size,0)]
    elsif other.is_a? Vector
	[other, Vector[*Array.new(other.size,0)]]
    else
	[Point[other], Point[Array.new(other.size,0)]]
    end
end

#eql?(other) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)


12
13
14
15
16
17
18
# File 'lib/geometry/point_zero.rb', line 12

def eql?(other)
    if other.respond_to? :all?
	other.all? {|e| e.eql? 0}
    else
	other == 0
    end
end

#first(n = nil) ⇒ Object

Return the first, or first n, elements (always 0)

Parameters:

  • n (Number) (defaults to: nil)

    the number of elements to return



168
169
170
# File 'lib/geometry/point_zero.rb', line 168

def first(n=nil)
    Array.new(n, 0) rescue 0
end

#is_a?(klass) ⇒ Boolean Also known as: kind_of?

Returns:

  • (Boolean)


33
34
35
# File 'lib/geometry/point_zero.rb', line 33

def is_a?(klass)
    (klass == Point) || super
end

#max(*args) ⇒ Number, Point

Returns:



73
74
75
76
77
78
79
80
# File 'lib/geometry/point_zero.rb', line 73

def max(*args)
    if args.empty?
	0
    else
	args = args.first if 1 == args.size
	Array.new(args.size, 0).zip(args).map(&:max)
    end
end

#min(*args) ⇒ Number, Point

Returns:



86
87
88
89
90
91
92
93
# File 'lib/geometry/point_zero.rb', line 86

def min(*args)
    if args.empty?
	0
    else
	args = args.first if 1 == args.size
	Array.new(args.size, 0).zip(args).map(&:min)
    end
end

#minmax(*args) ⇒ Array<Number>, Array<Point>

Returns:



99
100
101
102
103
104
105
# File 'lib/geometry/point_zero.rb', line 99

def minmax(*args)
    if args.empty?
	[0, 0]
    else
	[min(*args), max(*args)]
    end
end

#pop(count = 1) ⇒ Point

Returns a new Geometry::Point with the given number of elements removed from the end

Returns:

  • (Point)

    the popped elements



109
110
111
# File 'lib/geometry/point_zero.rb', line 109

def pop(count=1)
    Point[Array.new(count, 0)]
end

#shift(count = 1) ⇒ Point

Removes the first element and returns it

Returns:

  • (Point)

    the shifted elements



115
116
117
# File 'lib/geometry/point_zero.rb', line 115

def shift(count=1)
    Point[Array.new(count, 0)]
end

#to_aryObject

This is a hack to get Array#== to work properly. It works on ruby 2.0 and 1.9.3.



39
40
41
# File 'lib/geometry/point_zero.rb', line 39

def to_ary
    []
end