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

Constant Summary

Constants inherited from Vector

Vector::X, Vector::Y, Vector::Z

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Vector

#+@, #-@, #cross

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



77
78
79
# File 'lib/geometry/size.rb', line 77

def depth
    z
end

#height ⇒ Number



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)
    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

#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)
    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_s ⇒ Object



72
73
74
# File 'lib/geometry/size.rb', line 72

def to_s
    'Size' + @elements.to_s
end

#width ⇒ Number



87
88
89
# File 'lib/geometry/size.rb', line 87

def width
    x
end

#x ⇒ Number



92
93
94
# File 'lib/geometry/size.rb', line 92

def x
    @elements[0]
end

#y ⇒ Number



97
98
99
# File 'lib/geometry/size.rb', line 97

def y
    @elements[1]
end

#z ⇒ Number



102
103
104
# File 'lib/geometry/size.rb', line 102

def z
    @elements[2]
end