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)



62
63
64
# File 'lib/geometry/size.rb', line 62

def x
  @x
end

#yNumber (readonly)

Returns Y-component (height).

Returns:

  • (Number)

    Y-component (height)



67
68
69
# File 'lib/geometry/size.rb', line 67

def y
  @y
end

#zNumber (readonly)

Returns Z-component (depth).

Returns:

  • (Number)

    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

Returns:



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

#depthNumber

Returns The size along the Z axis.

Returns:

  • (Number)

    The size along the Z axis



47
48
49
# File 'lib/geometry/size.rb', line 47

def depth
    z
end

#heightNumber

Returns The size along the Y axis.

Returns:

  • (Number)

    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

Parameters:

  • x (Number)

    the horizontal inset

  • y (Number)

    the vertical inset

  • options (Hash)

    a customizable set of options



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)
    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 = -args.shift
  top = -args.shift
  right = bottom = 0
    end

    left = -options[:x] if options[:x]
    top = -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



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

Parameters:

  • x (Number)

    the horizontal inset

  • y (Number)

    the vertical inset

  • options (Hash)

    a customizable set of options



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)
    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 = args.shift
  top = args.shift
  right = bottom = 0
    end

    left = options[:x] if options[:x]
    top = 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



42
43
44
# File 'lib/geometry/size.rb', line 42

def to_s
    'Size' + @elements.to_s
end

#widthNumber

Returns The size along the X axis.

Returns:

  • (Number)

    The size along the X axis



57
58
59
# File 'lib/geometry/size.rb', line 57

def width
    x
end