Class: Geometry::PointZero
- Inherits:
-
Object
- Object
- Geometry::PointZero
- 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
- #x ⇒ Object readonly
- #y ⇒ Object readonly
- #z ⇒ Object readonly
Accessors collapse
-
#[](i) ⇒ Numeric
Element i (starting at 0).
Unary operators collapse
Enumerable collapse
-
#first(n = nil) ⇒ Object
Return the first, or first n, elements (always 0).
Instance Method Summary collapse
- #*(other) ⇒ Object
- #+(other) ⇒ Object
- #-(other) ⇒ Object
- #/(other) ⇒ Object
- #coerce(other) ⇒ Object
- #eql?(other) ⇒ Boolean (also: #==)
- #is_a?(klass) ⇒ Boolean (also: #kind_of?)
- #max(*args) ⇒ Number, Point
- #min(*args) ⇒ Number, Point
- #minmax(*args) ⇒ Array<Number>, Array<Point>
-
#pop(count = 1) ⇒ Point
Returns a new Point with the given number of elements removed from the end.
-
#shift(count = 1) ⇒ Point
Removes the first element and returns it.
-
#to_ary ⇒ Object
This is a hack to get Array#== to work properly.
Instance Attribute Details
#x ⇒ Object (readonly)
53 54 55 |
# File 'lib/geometry/point_zero.rb', line 53 def x 0 end |
#y ⇒ Object (readonly)
59 60 61 |
# File 'lib/geometry/point_zero.rb', line 59 def y 0 end |
#z ⇒ Object (readonly)
65 66 67 |
# File 'lib/geometry/point_zero.rb', line 65 def z 0 end |
Instance Method Details
#*(other) ⇒ Object
154 155 156 |
# File 'lib/geometry/point_zero.rb', line 154 def *(other) self end |
#+(other) ⇒ Object
132 133 134 135 136 137 138 139 140 |
# File 'lib/geometry/point_zero.rb', line 132 def +(other) case other when Array then other when Numeric Point.iso(other) else Point[other] end end |
#+@ ⇒ Object
123 124 125 |
# File 'lib/geometry/point_zero.rb', line 123 def +@ self end |
#-(other) ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/geometry/point_zero.rb', line 142 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
127 128 129 |
# File 'lib/geometry/point_zero.rb', line 127 def -@ self end |
#/(other) ⇒ Object
158 159 160 161 162 |
# File 'lib/geometry/point_zero.rb', line 158 def /(other) raise OperationNotDefined unless other.is_a? Numeric raise ZeroDivisionError if 0 == other self end |
#[](i) ⇒ Numeric
Returns Element i (starting at 0).
47 48 49 |
# File 'lib/geometry/point_zero.rb', line 47 def [](i) 0 end |
#coerce(other) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/geometry/point_zero.rb', line 22 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: ==
13 14 15 16 17 18 19 |
# File 'lib/geometry/point_zero.rb', line 13 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)
169 170 171 |
# File 'lib/geometry/point_zero.rb', line 169 def first(n=nil) Array.new(n, 0) rescue 0 end |
#is_a?(klass) ⇒ Boolean Also known as: kind_of?
34 35 36 |
# File 'lib/geometry/point_zero.rb', line 34 def is_a?(klass) (klass == Point) || super end |
#max(*args) ⇒ Number, Point
74 75 76 77 78 79 80 81 |
# File 'lib/geometry/point_zero.rb', line 74 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
87 88 89 90 91 92 93 94 |
# File 'lib/geometry/point_zero.rb', line 87 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>
100 101 102 103 104 105 106 |
# File 'lib/geometry/point_zero.rb', line 100 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
110 111 112 |
# File 'lib/geometry/point_zero.rb', line 110 def pop(count=1) Point[Array.new(count, 0)] end |
#shift(count = 1) ⇒ Point
Removes the first element and returns it
116 117 118 |
# File 'lib/geometry/point_zero.rb', line 116 def shift(count=1) Point[Array.new(count, 0)] end |
#to_ary ⇒ Object
This is a hack to get Array#== to work properly. It works on ruby 2.0 and 1.9.3.
40 41 42 |
# File 'lib/geometry/point_zero.rb', line 40 def to_ary [] end |