Class: Geometry::PointOne
- Inherits:
-
Object
- Object
- Geometry::PointOne
- Defined in:
- lib/geometry/point_one.rb
Overview
An object repesenting a Point that is one unit away from the origin, along each axis, in N-dimensional space
A PointOne object is a Point that will always compare equal to one and unequal to everything else, regardless of size. It’s similar to the Null Object Pattern, but for ones.
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)
51 52 53 |
# File 'lib/geometry/point_one.rb', line 51 def x 1 end |
#y ⇒ Object (readonly)
57 58 59 |
# File 'lib/geometry/point_one.rb', line 57 def y 1 end |
#z ⇒ Object (readonly)
63 64 65 |
# File 'lib/geometry/point_one.rb', line 63 def z 1 end |
Instance Method Details
#*(other) ⇒ Object
157 158 159 160 |
# File 'lib/geometry/point_one.rb', line 157 def *(other) raise OperationNotDefined unless other.is_a? Numeric other end |
#+(other) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/geometry/point_one.rb', line 130 def +(other) case other when Numeric Point.iso(other + 1) when Size Point[other.map {|a| a + 1 }] else if other.respond_to?(:map) other.map {|a| a + 1 } else Point[other + 1] end end end |
#+@ ⇒ Object
121 122 123 |
# File 'lib/geometry/point_one.rb', line 121 def +@ self end |
#-(other) ⇒ Object
145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/geometry/point_one.rb', line 145 def -(other) if other.is_a? Size Point[other.map {|a| 1 - a }] elsif other.respond_to? :map other.map {|a| 1 - a } elsif other == 1 Point.zero else Point.iso(1 - other) end end |
#-@ ⇒ Object
125 126 127 |
# File 'lib/geometry/point_one.rb', line 125 def -@ -1 end |
#/(other) ⇒ Object
162 163 164 165 166 |
# File 'lib/geometry/point_one.rb', line 162 def /(other) raise OperationNotDefined unless other.is_a? Numeric raise ZeroDivisionError if 0 == other 1 / other end |
#[](i) ⇒ Numeric
Returns Element i (starting at 0).
45 46 47 |
# File 'lib/geometry/point_one.rb', line 45 def [](i) 1 end |
#coerce(other) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/geometry/point_one.rb', line 20 def coerce(other) if other.is_a? Numeric [other, 1] elsif other.is_a? Array [other, Array.new(other.size, 1)] elsif other.is_a? Vector [other, Vector[*Array.new(other.size, 1)]] else [Point[other], Point[Array.new(other.size, 1)]] end end |
#eql?(other) ⇒ Boolean Also known as: ==
11 12 13 14 15 16 17 |
# File 'lib/geometry/point_one.rb', line 11 def eql?(other) if other.respond_to? :all? other.all? {|e| e.eql? 1} else other == 1 end end |
#first(n = nil) ⇒ Object
Return the first, or first n, elements (always 0)
173 174 175 |
# File 'lib/geometry/point_one.rb', line 173 def first(n=nil) Array.new(n, 1) rescue 1 end |
#is_a?(klass) ⇒ Boolean Also known as: kind_of?
32 33 34 |
# File 'lib/geometry/point_one.rb', line 32 def is_a?(klass) (klass == Point) || super end |
#max(*args) ⇒ Number, Point
72 73 74 75 76 77 78 79 |
# File 'lib/geometry/point_one.rb', line 72 def max(*args) if args.empty? 1 else args = args.first if 1 == args.size Point[Array.new(args.size, 1).zip(args).map(&:max)] end end |
#min(*args) ⇒ Number, Point
85 86 87 88 89 90 91 92 |
# File 'lib/geometry/point_one.rb', line 85 def min(*args) if args.empty? 1 else args = args.first if 1 == args.size Point[Array.new(args.size, 1).zip(args).map(&:min)] end end |
#minmax(*args) ⇒ Array<Number>, Array<Point>
98 99 100 101 102 103 104 |
# File 'lib/geometry/point_one.rb', line 98 def minmax(*args) if args.empty? [1, 1] 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
108 109 110 |
# File 'lib/geometry/point_one.rb', line 108 def pop(count=1) Point[Array.new(count, 1)] end |
#shift(count = 1) ⇒ Point
Removes the first element and returns it
114 115 116 |
# File 'lib/geometry/point_one.rb', line 114 def shift(count=1) Point[Array.new(count, 1)] end |
#to_ary ⇒ Object
This is a hack to get Array#== to work properly. It works on ruby 2.0 and 1.9.3.
38 39 40 |
# File 'lib/geometry/point_one.rb', line 38 def to_ary [] end |