Class: Geometry::Size

Inherits:
Vector
  • Object
show all
Defined in:
lib/geometry/size.rb

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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Vector

#+@, #-@, #cross

Instance Attribute Details

#xNumber (readonly)

Returns X-component (width).

Returns:

  • (Number)

    X-component (width)



94
95
96
# File 'lib/geometry/size.rb', line 94

def x
  @x
end

#yNumber (readonly)

Returns Y-component (height).

Returns:

  • (Number)

    Y-component (height)



99
100
101
# File 'lib/geometry/size.rb', line 99

def y
  @y
end

#zNumber (readonly)

Returns Z-component (depth).

Returns:

  • (Number)

    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

Returns:



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.

Parameters:

  • size (Number) (defaults to: nil)

    the size of the new Geometry::Size full of ones

Returns:



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.

Parameters:

  • size (Number) (defaults to: nil)

    the size of the new Geometry::Size full of zeros

Returns:



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

#depthNumber

Returns The size along the Z axis.

Returns:

  • (Number)

    The size along the Z axis



79
80
81
# File 'lib/geometry/size.rb', line 79

def depth
    z
end

#heightNumber

Returns The size along the Y axis.

Returns:

  • (Number)

    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

Parameters:

  • x (Number)

    the horizontal inset

  • y (Number)

    the vertical inset

  • options (Hash)

    a customizable set of options



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)
    options, args = args.partition {|a| a.is_a? Hash}
    options = options.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 = -options[:x] if options[:x]
    top = bottom = -options[:y] if options[:y]

    top = -options[:top] if options[:top]
    left = -options[:left] if options[:left]
    bottom = -options[:bottom] if options[:bottom]
    right = -options[:right] if options[:right]

    self.class[left + width + right, top + height + bottom]
end

#inspectObject



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

Parameters:

  • x (Number)

    the horizontal inset

  • y (Number)

    the vertical inset

  • options (Hash)

    a customizable set of options



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)
    options, args = args.partition {|a| a.is_a? Hash}
    options = options.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 = options[:x] if options[:x]
    top = bottom = options[:y] if options[:y]

    top = options[:top] if options[:top]
    left = options[:left] if options[:left]
    bottom = options[:bottom] if options[:bottom]
    right = options[:right] if options[:right]

    self.class[left + width + right, top + height + bottom]
end

#to_sObject



74
75
76
# File 'lib/geometry/size.rb', line 74

def to_s
    'Size' + @elements.to_s
end

#widthNumber

Returns The size along the X axis.

Returns:

  • (Number)

    The size along the X axis



89
90
91
# File 'lib/geometry/size.rb', line 89

def width
    x
end