Class: Geometry::Size
Overview
Constant Summary
Constants inherited from Vector
Vector::X, Vector::Y, Vector::Z
Instance Attribute Summary collapse
-
#x ⇒ Number
readonly
X-component (width).
-
#y ⇒ Number
readonly
Y-component (height).
-
#z ⇒ Number
readonly
Z-component (depth).
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.
Methods inherited from Vector
Instance Attribute Details
#x ⇒ Number (readonly)
Returns X-component (width).
94 95 96 |
# File 'lib/geometry/size.rb', line 94 def x @x end |
#y ⇒ Number (readonly)
Returns Y-component (height).
99 100 101 |
# File 'lib/geometry/size.rb', line 99 def y @y end |
#z ⇒ Number (readonly)
Returns Z-component (depth).
104 105 106 |
# File 'lib/geometry/size.rb', line 104 def z @z end |
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
30 31 32 33 34 |
# File 'lib/geometry/size.rb', line 30 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.
39 40 41 |
# File 'lib/geometry/size.rb', line 39 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.
46 47 48 |
# File 'lib/geometry/size.rb', line 46 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
51 52 53 54 |
# File 'lib/geometry/size.rb', line 51 def ==(other) return @elements == other if other.is_a?(Array) super other end |
#[](*args) ⇒ Object
Override Vector#[] to allow for regular array slicing
57 58 59 |
# File 'lib/geometry/size.rb', line 57 def [](*args) @elements[*args] end |
#coerce(other) ⇒ Object
61 62 63 64 65 66 67 68 69 |
# File 'lib/geometry/size.rb', line 61 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.
79 80 81 |
# File 'lib/geometry/size.rb', line 79 def depth z end |
#height ⇒ Number
Returns The size along the Y axis.
84 85 86 |
# File 'lib/geometry/size.rb', line 84 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
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/geometry/size.rb', line 117 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
71 72 73 |
# File 'lib/geometry/size.rb', line 71 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
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/geometry/size.rb', line 150 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
74 75 76 |
# File 'lib/geometry/size.rb', line 74 def to_s 'Size' + @elements.to_s end |
#width ⇒ Number
Returns The size along the X axis.
89 90 91 |
# File 'lib/geometry/size.rb', line 89 def width x end |