Class: Geometry::PointIso
- Inherits:
-
Object
- Object
- Geometry::PointIso
- Defined in:
- lib/geometry/point_iso.rb
Overview
An object repesenting a N-dimensional Point with identical elements.
Accessors collapse
- #x ⇒ Object readonly
- #y ⇒ Object readonly
- #z ⇒ Object readonly
Instance Attribute Summary collapse
-
#value ⇒ Number
The value for every element.
Accessors collapse
-
#[](i) ⇒ Numeric
Element i (starting at 0).
Unary operators collapse
Instance Method Summary collapse
- #*(other) ⇒ Object
- #+(other) ⇒ Object
- #-(other) ⇒ Object
- #/(other) ⇒ Object
- #coerce(other) ⇒ Object
- #eql?(other) ⇒ Boolean (also: #==)
-
#initialize(value) ⇒ PointIso
constructor
Initialize to the given value.
- #inspect ⇒ Object
- #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.
- #to_s ⇒ Object
Constructor Details
#initialize(value) ⇒ PointIso
Initialize to the given value
12 13 14 |
# File 'lib/geometry/point_iso.rb', line 12 def initialize(value) @value = value end |
Instance Attribute Details
#value ⇒ Number
Returns the value for every element.
8 9 10 |
# File 'lib/geometry/point_iso.rb', line 8 def value @value end |
#x ⇒ Object (readonly)
113 114 115 |
# File 'lib/geometry/point_iso.rb', line 113 def x @value end |
#y ⇒ Object (readonly)
119 120 121 |
# File 'lib/geometry/point_iso.rb', line 119 def y @value end |
#z ⇒ Object (readonly)
125 126 127 |
# File 'lib/geometry/point_iso.rb', line 125 def z @value end |
Instance Method Details
#*(other) ⇒ Object
167 168 169 170 |
# File 'lib/geometry/point_iso.rb', line 167 def *(other) raise OperationNotDefined unless other.is_a? Numeric self.class.new(other * @value) end |
#+(other) ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/geometry/point_iso.rb', line 142 def +(other) case other when Numeric other + @value when Size Point[other.map {|a| a + @value }] else if other.respond_to?(:map) other.map {|a| a + @value } else Point[other + @value] end end end |
#+@ ⇒ Object
133 134 135 |
# File 'lib/geometry/point_iso.rb', line 133 def +@ self end |
#-(other) ⇒ Object
157 158 159 160 161 162 163 164 165 |
# File 'lib/geometry/point_iso.rb', line 157 def -(other) if other.is_a? Size Point[other.map {|a| @value - a }] elsif other.respond_to? :map other.map {|a| @value - a } else @value - other end end |
#-@ ⇒ Object
137 138 139 |
# File 'lib/geometry/point_iso.rb', line 137 def -@ self.class.new(-@value) end |
#/(other) ⇒ Object
172 173 174 175 176 |
# File 'lib/geometry/point_iso.rb', line 172 def /(other) raise OperationNotDefined unless other.is_a? Numeric raise ZeroDivisionError if 0 == other self.class.new(@value / other) end |
#[](i) ⇒ Numeric
Returns Element i (starting at 0).
107 108 109 |
# File 'lib/geometry/point_iso.rb', line 107 def [](i) @value end |
#coerce(other) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/geometry/point_iso.rb', line 25 def coerce(other) if other.is_a? Numeric [other, @value] elsif other.is_a? Array [other, Array.new(other.size, @value)] elsif other.is_a? Vector [other, Vector[*Array.new(other.size, @value)]] else [Point[other], Point[Array.new(other.size, @value)]] end end |
#eql?(other) ⇒ Boolean Also known as: ==
16 17 18 19 20 21 22 |
# File 'lib/geometry/point_iso.rb', line 16 def eql?(other) if other.respond_to? :all? other.all? {|e| e.eql? @value} else other == @value end end |
#inspect ⇒ Object
37 38 39 |
# File 'lib/geometry/point_iso.rb', line 37 def inspect 'PointIso<' + @value.inspect + '>' end |
#is_a?(klass) ⇒ Boolean Also known as: kind_of?
44 45 46 |
# File 'lib/geometry/point_iso.rb', line 44 def is_a?(klass) (klass == Point) || super end |
#max(*args) ⇒ Number, Point
58 59 60 61 62 63 64 65 |
# File 'lib/geometry/point_iso.rb', line 58 def max(*args) if args.empty? @value else args = args.first if 1 == args.size Point[Array.new(args.size, @value).zip(args).map(&:max)] end end |
#min(*args) ⇒ Number, Point
71 72 73 74 75 76 77 78 |
# File 'lib/geometry/point_iso.rb', line 71 def min(*args) if args.empty? @value else args = args.first if 1 == args.size Point[Array.new(args.size, @value).zip(args).map(&:min)] end end |
#minmax(*args) ⇒ Array<Number>, Array<Point>
84 85 86 87 88 89 90 |
# File 'lib/geometry/point_iso.rb', line 84 def minmax(*args) if args.empty? [@value, @value] 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
94 95 96 |
# File 'lib/geometry/point_iso.rb', line 94 def pop(count=1) Point[Array.new(count, @value)] end |
#shift(count = 1) ⇒ Point
Removes the first element and returns it
100 101 102 |
# File 'lib/geometry/point_iso.rb', line 100 def shift(count=1) Point[Array.new(count, @value)] end |
#to_ary ⇒ Object
This is a hack to get Array#== to work properly. It works on ruby 2.0 and 1.9.3.
50 51 52 |
# File 'lib/geometry/point_iso.rb', line 50 def to_ary [] end |
#to_s ⇒ Object
40 41 42 |
# File 'lib/geometry/point_iso.rb', line 40 def to_s 'PointIso<' + @value.to_s + '>' end |