Class: CGRect

Inherits:
Object
  • Object
show all
Defined in:
lib/motion-ui-geometry/cgrect.rb

Instance Method Summary collapse

Constructor Details

#initialize(origin = nil, size = nil) ⇒ CGRect

Returns a new instance of CGRect.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/motion-ui-geometry/cgrect.rb', line 3

def initialize(origin = nil, size = nil)
  if origin.nil? || size.nil?
    raise ArgumentError, "Can't initialize CGRect without given origin and size."
  end

  unless origin.is_a?(CGPoint)
    raise TypeError, "origin must be given as CGPoint."
  end

  unless size.is_a?(CGSize)
    raise TypeError, "size must be given as CGSize."
  end

  self.origin = origin
  self.size = size

  self
end

Instance Method Details

#&(other) ⇒ Object



107
108
109
# File 'lib/motion-ui-geometry/cgrect.rb', line 107

def &(other)
  intersection(other)
end

#*(other) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/motion-ui-geometry/cgrect.rb', line 67

def *(other)
  case other
  when Fixnum, Float
    CGRectMake origin.x   * other, origin.y    * other,
               size.width * other, size.height * other
  when CGSize
    CGRectMake origin.x   * other.width, origin.y    * other.height,
               size.width * other.width, size.height * other.height
  when CGPoint
    CGRectMake origin.x   * other.x, origin.y    * other.y,
               size.width * other.x, size.height * other.y
  when CGAffineTransform
    CGRectApplyAffineTransform self, other
  else
    raise TypeError, "Right operand for * must be Fixnum, "\
      "Float, Array, CGPoint, CGSize or CGAffineTransform "\
      "(got #{other.class})."
  end
end

#+(other) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/motion-ui-geometry/cgrect.rb', line 47

def +(other)
  case other
  when CGPoint
    CGRect.new origin + other, size
  when CGSize
    CGRect.new origin, size + other
  else
    raise TypeError, "Right operand for + and - must be a "\
      "CGPoint or a CGRect (got #{other.class})."
  end
end

#-(other) ⇒ Object



59
60
61
# File 'lib/motion-ui-geometry/cgrect.rb', line 59

def -(other)
  self + (-other)
end

#-@Object



63
64
65
# File 'lib/motion-ui-geometry/cgrect.rb', line 63

def -@
  CGRectMake -origin.x, -origin.y, -size.width, -size.height
end

#/(other) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/motion-ui-geometry/cgrect.rb', line 87

def /(other)
  case other
  when Float
    CGRectMake origin.x   / other, origin.y    / other,
               size.width / other, size.height / other
  when CGSize
    CGRectMake origin.x   / other.width, origin.y    / other.height,
               size.width / other.width, size.height / other.height
  when CGPoint
    CGRectMake origin.x   / other.x, origin.y    / other.y,
               size.width / other.x, size.height / other.y
  when CGAffineTransform
    CGRectApplyAffineTransform self, other.inverse
  else
    raise TypeError, "Right operand for / must be Float, "\
      "CGPoint, CGSize or CGAffineTransform (got #{other.class})."
  end

end

#==(other) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/motion-ui-geometry/cgrect.rb', line 23

def ==(other)
  other.is_a?(CGRect) &&
    origin.x == other.origin.x &&
    origin.y == other.origin.y &&
    size.width == other.size.width &&
    size.height == other.size.height
end

#=~(other) ⇒ Object



42
43
44
# File 'lib/motion-ui-geometry/cgrect.rb', line 42

def =~(other)
  roughly_equal?(other)
end

#apply(transform) ⇒ Object



207
208
209
# File 'lib/motion-ui-geometry/cgrect.rb', line 207

def apply(transform)
  CGRectApplyAffineTransform(self, transform)
end

#bottom_leftObject



220
221
222
# File 'lib/motion-ui-geometry/cgrect.rb', line 220

def bottom_left
  CGPointMake(origin.x, origin.y + size.height)
end

#bottom_rightObject



224
225
226
# File 'lib/motion-ui-geometry/cgrect.rb', line 224

def bottom_right
  origin + size
end

#centerObject



212
213
214
# File 'lib/motion-ui-geometry/cgrect.rb', line 212

def center
  origin + size * 0.5
end

#contain?(point_or_rect) ⇒ Boolean

Returns:

  • (Boolean)


185
186
187
188
189
190
191
# File 'lib/motion-ui-geometry/cgrect.rb', line 185

def contain?(point_or_rect)
  case point_or_rect
  when CGPoint then CGRectContainsPoint self, point_or_rect
  when CGRect then CGRectContainsRect self, point_or_rect
  else raise TypeError, "Parameter must be a CGPoint or CGRect, got #{point_or_rect}."
  end
end

#division(amount, edge = :left) ⇒ Object Also known as: divide



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/motion-ui-geometry/cgrect.rb', line 152

def division(amount, edge = :left)
  unless amount.is_a?(Float) || amount.is_a?(Fixnum)
    raise ArgumentError, "amount must be given as Float or Fixnum, got #{amount}."
  end

  unless [:left, :right, :top, :bottom].include? edge
    raise ArgumentError, "edge must be :left, :right, :top, or :bottom (got #{edge})."
  end

  slice = Pointer.new(CGRect.type)
  remainder = Pointer.new(CGRect.type)

  edge = case edge
  when :left   then CGRectMinXEdge
  when :right  then CGRectMaxXEdge
  when :top    then CGRectMinYEdge
  when :bottom then CGRectMaxYEdge
  end

  CGRectDivide self, slice, remainder, amount, edge

  [slice[0], remainder[0]]
end

#empty?Boolean

Returns:

  • (Boolean)


193
194
195
# File 'lib/motion-ui-geometry/cgrect.rb', line 193

def empty?
  CGRectIsEmpty self
end

#floorObject



120
121
122
# File 'lib/motion-ui-geometry/cgrect.rb', line 120

def floor
  CGRect.new origin.floor, size.floor
end

#inset(dx, dy) ⇒ Object



126
127
128
# File 'lib/motion-ui-geometry/cgrect.rb', line 126

def inset(dx, dy)
  CGRectInset self, dx, dy
end

#intersect?(other) ⇒ Boolean

Returns:

  • (Boolean)


177
178
179
180
181
182
183
# File 'lib/motion-ui-geometry/cgrect.rb', line 177

def intersect?(other)
  unless other.is_a? CGRect
    raise TypeError, "Parameter must be a CGRect, got #{other}."
  end

  CGRectIntersectsRect self, other
end

#intersection(rect) ⇒ Object Also known as: intersect



143
144
145
146
147
148
149
# File 'lib/motion-ui-geometry/cgrect.rb', line 143

def intersection(rect)
  unless rect.is_a? CGRect
    raise TypeError, "Parameter must be a CGRect, got #{rect}."
  end

  CGRectIntersection self, rect
end

#null?Boolean

Returns:

  • (Boolean)


197
198
199
# File 'lib/motion-ui-geometry/cgrect.rb', line 197

def null?
  CGRectIsNull self
end

#outset(dx, dy) ⇒ Object



130
131
132
# File 'lib/motion-ui-geometry/cgrect.rb', line 130

def outset(dx, dy)
  inset -dx, -dy
end

#roughly_equal?(other, epsilon = Float::EPSILON) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
34
35
36
37
38
39
40
# File 'lib/motion-ui-geometry/cgrect.rb', line 31

def roughly_equal?(other, epsilon = Float::EPSILON)
  unless other.is_a? CGRect
    raise TypeError, "Right operand for =~ must be a CGRect (got #{other})."
  end

  origin.x.roughly_equal?(other.origin.x, epsilon) &&
    origin.y.roughly_equal?(other.origin.y, epsilon) &&
    size.width.roughly_equal?(other.size.width, epsilon) &&
    size.height.roughly_equal?(other.size.height, epsilon)
end

#roundObject



116
117
118
# File 'lib/motion-ui-geometry/cgrect.rb', line 116

def round
  CGRect.new origin.round, size.round
end

#to_dictionaryObject



233
234
235
# File 'lib/motion-ui-geometry/cgrect.rb', line 233

def to_dictionary
  CGRectCreateDictionaryRepresentation self
end

#to_sObject



241
242
243
# File 'lib/motion-ui-geometry/cgrect.rb', line 241

def to_s
  NSStringFromCGRect self
end

#to_valueObject



237
238
239
# File 'lib/motion-ui-geometry/cgrect.rb', line 237

def to_value
  NSValue.valueWithCGRect self
end

#top_leftObject



216
217
218
# File 'lib/motion-ui-geometry/cgrect.rb', line 216

def top_left
  origin
end

#top_rightObject



228
229
230
# File 'lib/motion-ui-geometry/cgrect.rb', line 228

def top_right
  CGPointMake(origin.x + size.width, origin.y)
end

#union(rect) ⇒ Object Also known as: unionize



134
135
136
137
138
139
140
# File 'lib/motion-ui-geometry/cgrect.rb', line 134

def union(rect)
  unless rect.is_a? CGRect
    raise TypeError, "Parameter must be a CGRect, got #{rect}."
  end

  CGRectUnion self, rect
end

#|(other) ⇒ Object



111
112
113
# File 'lib/motion-ui-geometry/cgrect.rb', line 111

def |(other)
  union(other)
end