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>
-
#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
14 15 16 |
# File 'lib/geometry/point_iso.rb', line 14 def initialize(value) @value = value end |
Instance Attribute Details
#value ⇒ Number
Returns the value for every element.
10 11 12 |
# File 'lib/geometry/point_iso.rb', line 10 def value @value end |
#x ⇒ Object (readonly)
103 104 105 |
# File 'lib/geometry/point_iso.rb', line 103 def x @value end |
#y ⇒ Object (readonly)
109 110 111 |
# File 'lib/geometry/point_iso.rb', line 109 def y @value end |
#z ⇒ Object (readonly)
115 116 117 |
# File 'lib/geometry/point_iso.rb', line 115 def z @value end |
Instance Method Details
#*(other) ⇒ Object
157 158 159 160 |
# File 'lib/geometry/point_iso.rb', line 157 def *(other) raise OperationNotDefined unless other.is_a? Numeric self.class.new(other * @value) end |
#+(other) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/geometry/point_iso.rb', line 132 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
123 124 125 |
# File 'lib/geometry/point_iso.rb', line 123 def +@ self end |
#-(other) ⇒ Object
147 148 149 150 151 152 153 154 155 |
# File 'lib/geometry/point_iso.rb', line 147 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
127 128 129 |
# File 'lib/geometry/point_iso.rb', line 127 def -@ self.class.new(-@value) end |
#/(other) ⇒ Object
162 163 164 165 166 |
# File 'lib/geometry/point_iso.rb', line 162 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).
97 98 99 |
# File 'lib/geometry/point_iso.rb', line 97 def [](i) @value end |
#coerce(other) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/geometry/point_iso.rb', line 27 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: ==
18 19 20 21 22 23 24 |
# File 'lib/geometry/point_iso.rb', line 18 def eql?(other) if other.respond_to? :all? other.all? {|e| e.eql? @value} else other == @value end end |
#inspect ⇒ Object
39 40 41 |
# File 'lib/geometry/point_iso.rb', line 39 def inspect 'PointIso<' + @value.inspect + '>' end |
#is_a?(klass) ⇒ Boolean Also known as: kind_of?
46 47 48 |
# File 'lib/geometry/point_iso.rb', line 46 def is_a?(klass) (klass == Point) || super end |
#max(*args) ⇒ Number, Point
60 61 62 63 64 65 66 67 |
# File 'lib/geometry/point_iso.rb', line 60 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
73 74 75 76 77 78 79 80 |
# File 'lib/geometry/point_iso.rb', line 73 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>
86 87 88 89 90 91 92 |
# File 'lib/geometry/point_iso.rb', line 86 def minmax(*args) if args.empty? [@value, @value] else [min(*args), max(*args)] end end |
#to_ary ⇒ Object
This is a hack to get Array#== to work properly. It works on ruby 2.0 and 1.9.3.
52 53 54 |
# File 'lib/geometry/point_iso.rb', line 52 def to_ary [] end |
#to_s ⇒ Object
42 43 44 |
# File 'lib/geometry/point_iso.rb', line 42 def to_s 'PointIso<' + @value.to_s + '>' end |