Class: Geometry::Size
Overview
An object representing the size of something.
Supports all of the familiar Vector methods as well as a few convenience methods (width, height and depth).
Usage
Constructor
size = Geometry::Size[x,y,z]
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.
Instance Method Summary collapse
-
#==(other) ⇒ Object
Allow comparison with an Array, otherwise do the normal thing.
-
#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).
62 63 64 |
# File 'lib/geometry/size.rb', line 62 def x @x end |
#y ⇒ Number (readonly)
Returns Y-component (height).
67 68 69 |
# File 'lib/geometry/size.rb', line 67 def y @y end |
#z ⇒ Number (readonly)
Returns Z-component (depth).
72 73 74 |
# File 'lib/geometry/size.rb', line 72 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
27 28 29 30 31 |
# File 'lib/geometry/size.rb', line 27 def self.[](*array) array.map! {|a| a.respond_to?(:to_a) ? a.to_a : a } array.flatten! super *array end |
Instance Method Details
#==(other) ⇒ Object
Allow comparison with an Array, otherwise do the normal thing
34 35 36 37 |
# File 'lib/geometry/size.rb', line 34 def ==(other) return @elements == other if other.is_a?(Array) super other end |
#depth ⇒ Number
Returns The size along the Z axis.
47 48 49 |
# File 'lib/geometry/size.rb', line 47 def depth z end |
#height ⇒ Number
Returns The size along the Y axis.
52 53 54 |
# File 'lib/geometry/size.rb', line 52 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
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/geometry/size.rb', line 85 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 = -args.shift top = -args.shift right = bottom = 0 end left = -[:x] if [:x] top = -[: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
39 40 41 |
# File 'lib/geometry/size.rb', line 39 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
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/geometry/size.rb', line 119 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 = args.shift top = args.shift right = bottom = 0 end left = [:x] if [:x] top = [: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
42 43 44 |
# File 'lib/geometry/size.rb', line 42 def to_s 'Size' + @elements.to_s end |
#width ⇒ Number
Returns The size along the X axis.
57 58 59 |
# File 'lib/geometry/size.rb', line 57 def width x end |