Class: Geometry::Size
Overview
Constant Summary
Constants inherited from Vector
Vector::X, Vector::Y, Vector::Z
Class Method Summary collapse
-
.[](*array) ⇒ Size
Allow vector-style initialization, but override to support copy-init from Vector, Point or another Size.
-
.one(size = nil) ⇒ SizeOne
Creates and returns a new SizeOne instance.
-
.zero(size = nil) ⇒ SizeOne
Creates and returns a new SizeOne instance.
Instance Method Summary collapse
-
#==(other) ⇒ Object
Allow comparison with an Array, otherwise do the normal thing.
-
#[](*args) ⇒ Object
Override Vector#[] to allow for regular array slicing.
- #coerce(other) ⇒ Object
-
#depth ⇒ Number
The size along the Z axis.
-
#height ⇒ Number
The size along the Y axis.
-
#inset(*args) ⇒ Object
Create a new Size that is smaller than the receiver by the specified amounts.
- #inspect ⇒ Object
-
#outset(*args) ⇒ Object
Create a new Size that is larger than the receiver by the specified amounts.
- #to_s ⇒ Object
-
#width ⇒ Number
The size along the X axis.
-
#x ⇒ Number
X-component (width).
-
#y ⇒ Number
Y-component (height).
-
#z ⇒ Number
Z-component (depth).
Methods inherited from Vector
Class Method Details
.[](x, y, z, ...) ⇒ Size .[](Point) ⇒ Size .[](Size) ⇒ Size .[](Vector) ⇒ Size
Allow vector-style initialization, but override to support copy-init from Vector, Point or another Size
28 29 30 31 32 |
# File 'lib/geometry/size.rb', line 28 def self.[](*array) array.map! {|a| a.respond_to?(:to_a) ? a.to_a : a } array.flatten! super(*array) end |
.one(size = nil) ⇒ SizeOne
Creates and returns a new Geometry::SizeOne instance. Or, a Geometry::Size full of ones if the size argument is given.
37 38 39 |
# File 'lib/geometry/size.rb', line 37 def self.one(size=nil) size ? Size[Array.new(size, 1)] : SizeOne.new end |
.zero(size = nil) ⇒ SizeOne
Creates and returns a new Geometry::SizeOne instance. Or, a Geometry::Size full of zeros if the size argument is given.
44 45 46 |
# File 'lib/geometry/size.rb', line 44 def self.zero(size=nil) size ? Size[Array.new(size, 0)] : SizeOne.new end |
Instance Method Details
#==(other) ⇒ Object
Allow comparison with an Array, otherwise do the normal thing
49 50 51 52 |
# File 'lib/geometry/size.rb', line 49 def ==(other) return @elements == other if other.is_a?(Array) super other end |
#[](*args) ⇒ Object
Override Vector#[] to allow for regular array slicing
55 56 57 |
# File 'lib/geometry/size.rb', line 55 def [](*args) @elements[*args] end |
#coerce(other) ⇒ Object
59 60 61 62 63 64 65 66 67 |
# File 'lib/geometry/size.rb', line 59 def coerce(other) case other when Array then [Size[*other], self] when Numeric then [Size[Array.new(self.size, other)], self] when Vector then [Size[*other], self] else raise TypeError, "#{self.class} can't be coerced into #{other.class}" end end |
#depth ⇒ Number
Returns The size along the Z axis.
77 78 79 |
# File 'lib/geometry/size.rb', line 77 def depth z end |
#height ⇒ Number
Returns The size along the Y axis.
82 83 84 |
# File 'lib/geometry/size.rb', line 82 def height y end |
#inset(x, y) ⇒ Object #inset(options) ⇒ Object
Create a new Geometry::Size that is smaller than the receiver by the specified amounts
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/geometry/size.rb', line 115 def inset(*args) , args = args.partition {|a| a.is_a? Hash} = .reduce({}, :merge) left = right = top = bottom = 0 if 1 == args.size left = top = -args.shift right = bottom = 0 elsif 2 == args.size left = right = -args.shift top = bottom = -args.shift end left = right = -[:x] if [:x] top = bottom = -[:y] if [:y] top = -[:top] if [:top] left = -[:left] if [:left] bottom = -[:bottom] if [:bottom] right = -[:right] if [:right] self.class[left + width + right, top + height + bottom] end |
#inspect ⇒ Object
69 70 71 |
# File 'lib/geometry/size.rb', line 69 def inspect 'Size' + @elements.inspect end |
#outset(x, y) ⇒ Object #outset(options) ⇒ Object
Create a new Geometry::Size that is larger than the receiver by the specified amounts
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/geometry/size.rb', line 148 def outset(*args) , args = args.partition {|a| a.is_a? Hash} = .reduce({}, :merge) left = right = top = bottom = 0 if 1 == args.size left = top = args.shift right = bottom = 0 elsif 2 == args.size left = right = args.shift top = bottom = args.shift end left = right = [:x] if [:x] top = bottom = [:y] if [:y] top = [:top] if [:top] left = [:left] if [:left] bottom = [:bottom] if [:bottom] right = [:right] if [:right] self.class[left + width + right, top + height + bottom] end |
#to_s ⇒ Object
72 73 74 |
# File 'lib/geometry/size.rb', line 72 def to_s 'Size' + @elements.to_s end |
#width ⇒ Number
Returns The size along the X axis.
87 88 89 |
# File 'lib/geometry/size.rb', line 87 def width x end |
#x ⇒ Number
Returns X-component (width).
92 93 94 |
# File 'lib/geometry/size.rb', line 92 def x @elements[0] end |
#y ⇒ Number
Returns Y-component (height).
97 98 99 |
# File 'lib/geometry/size.rb', line 97 def y @elements[1] end |
#z ⇒ Number
Returns Z-component (depth).
102 103 104 |
# File 'lib/geometry/size.rb', line 102 def z @elements[2] end |