Class: CGRect

Inherits:
Object
  • Object
show all
Defined in:
lib/geomotion/cg_rect.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.emptyObject



28
29
30
31
# File 'lib/geomotion/cg_rect.rb', line 28

def self.empty
  # Don't just return CGRectZero; can be mutated
  CGRectZero.dup
end

.infiniteObject



38
39
40
41
42
43
44
45
46
# File 'lib/geomotion/cg_rect.rb', line 38

def self.infinite
  # This actually returns the not-very-infinite value of:
  # [[-1.7014114289565e+38, -1.7014114289565e+38], [3.402822857913e+38, 3.402822857913e+38]]
  # originally this method returned [[-Infinity, -Infinity], [Infinity, Infinity]],
  # but that rect ended up returning `false` for any point in the method
  # CGRect.infinite.contains?(point).  CGRectInfinite returns `true` for any
  # (sensible) point, so we'll go with that instead
  CGRectInfinite.dup
end

.layout(rect1, options) ⇒ Object

OPTIONS: [:above, :below, :left_of, :right_of, :margins]

:margins is array of [top, right, bottom, left]

EX CGRect.layout(rect1, above: rect2, left_of: rect3, margins: [0, 10, 20, 0])



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/geomotion/cg_rect.rb', line 51

def self.layout(rect1, options)
  if options.empty?
    p "No options provided in #{self.class}.layout"
    return rect1
  end

  rect = self.new
  rect.size = rect1.size

  options[:margins] ||= []
  margins = {}
  [:top, :right, :bottom, :left].each_with_index do |margin, index|
    margins[margin] = options[:margins][index] || 0
  end

  rect.y = options[:above].up(rect.height + margins[:bottom]).y if options[:above]
  rect.y = options[:below].below(margins[:top]).y if options[:below]

  rect.x = options[:left_of].left(rect.width + margins[:right]).x if options[:left_of]
  rect.x = options[:right_of].beside(margins[:left]).x if options[:right_of]

  rect
end

.make(options = {}) ⇒ Object

CGRect.make # default rect: {x: 0, y: 0, size: height:0}

# aka CGRectZero

CGRect.make(x: 10, y: 30) # default size: [0, 0] CGRect.make(x: 10, y: 30, width:100, height: 20)

point = CGPoint.make(x: 10, y: 30) size = CGSize.make(width: 100, height: 20) CGRect.make(origin: point, size: size)



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/geomotion/cg_rect.rb', line 10

def self.make(options = {})
  if options[:origin]
    x = options[:origin].x
    y = options[:origin].y
  else
    x = options[:x] || 0
    y = options[:y] || 0
  end
  if options[:size]
    w = options[:size].width
    h = options[:size].height
  else
    w = options[:width] || 0
    h = options[:height] || 0
  end
  self.new([x, y], [w, h])
end

.nullObject



33
34
35
36
# File 'lib/geomotion/cg_rect.rb', line 33

def self.null
  # Don't just return CGRectNull; can be mutated
  CGRectNull.dup
end

Instance Method Details

#+(other) ⇒ Object



231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/geomotion/cg_rect.rb', line 231

def +(other)
  case other
  when CGRect
    return self.union_with(other)
  when CGSize
    return CGRect.new([self.x, self.y], [self.width + other.width, self.height + other.height])
  when CGPoint
    return CGRectOffset(self, other.x, other.y)
  when UIOffset
    CGRectOffset(self, other.horizontal, other.vertical)
  when UIEdgeInsets
    UIEdgeInsetsInsetRect(self, other)
  end
end

#-(other) ⇒ Object



308
309
310
# File 'lib/geomotion/cg_rect.rb', line 308

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

#-@Object



304
305
306
# File 'lib/geomotion/cg_rect.rb', line 304

def -@
  CGRect.new(-self.origin, -self.size)
end

#==(rect) ⇒ Object



300
301
302
# File 'lib/geomotion/cg_rect.rb', line 300

def ==(rect)
  rect.is_a?(CGRect) && CGRectEqualToRect(self, rect)
end

#above(margin, height: height) ⇒ Object

adjacent rects



171
172
173
# File 'lib/geomotion/cg_rect.rb', line 171

def above(margin = 0)
  self.above(margin, height:self.height)
end

#before(margin, width: width) ⇒ Object



183
184
185
# File 'lib/geomotion/cg_rect.rb', line 183

def before(margin = 0)
  self.before(margin, width:self.width)
end

#below(margin = 0) ⇒ Object



179
180
181
# File 'lib/geomotion/cg_rect.rb', line 179

def below(margin = 0)
  CGRect.new([self.x, self.y + self.height + margin], self.size)
end

#beside(margin, width: width) ⇒ Object



191
192
193
# File 'lib/geomotion/cg_rect.rb', line 191

def beside(margin = 0)
  self.beside(margin, width: self.width)
end

#bottom_leftObject



214
215
216
# File 'lib/geomotion/cg_rect.rb', line 214

def bottom_left
  CGPoint.new(CGRectGetMinX(self), CGRectGetMaxY(self))
end

#bottom_rightObject



218
219
220
# File 'lib/geomotion/cg_rect.rb', line 218

def bottom_right
  CGPoint.new(CGRectGetMaxX(self), CGRectGetMaxY(self))
end

#center(absolute = false) ⇒ Object

locations



200
201
202
203
204
# File 'lib/geomotion/cg_rect.rb', line 200

def center(absolute = false)
  offset_x = absolute ? self.x : 0
  offset_y = absolute ? self.y : 0
  CGPoint.new(offset_x + self.width / 2, offset_y + self.height / 2)
end

#centered_in(rect, absolute = false) ⇒ Object



227
228
229
# File 'lib/geomotion/cg_rect.rb', line 227

def centered_in(rect, absolute = false)
  self.size.centered_in(rect, absolute)
end

#contains?(rect_or_point) ⇒ Boolean

Returns:

  • (Boolean)


289
290
291
292
293
294
295
296
297
298
# File 'lib/geomotion/cg_rect.rb', line 289

def contains?(rect_or_point)
  case rect_or_point
  when CGPoint
    CGRectContainsPoint(self, rect_or_point)
  when CGRect
    CGRectContainsRect(self, rect_or_point)
  else
    super  # raises an error
  end
end

#down(dist = 0) ⇒ Object



150
151
152
# File 'lib/geomotion/cg_rect.rb', line 150

def down(dist = 0)
  CGRect.new([self.x, self.y + dist], self.size)
end

#empty?Boolean

Returns:

  • (Boolean)


268
269
270
# File 'lib/geomotion/cg_rect.rb', line 268

def empty?
  CGRectIsEmpty(self)
end

#grow(size) ⇒ Object



254
255
256
257
258
259
# File 'lib/geomotion/cg_rect.rb', line 254

def grow(size)
  if size.is_a? Numeric
    size = CGSize.new(size, size)
  end
  CGRectInset(self, -size.width, -size.height)
end

#height(setter = nil) ⇒ Object



126
127
128
129
130
131
# File 'lib/geomotion/cg_rect.rb', line 126

def height(setter = nil)
  if setter
    return CGRect.new(self.origin, [self.width, setter])
  end
  CGRectGetHeight(self)
end

#height=(_height) ⇒ Object



133
134
135
# File 'lib/geomotion/cg_rect.rb', line 133

def height=(_height)
  self.size.height = _height
end

#infinite?Boolean

Returns:

  • (Boolean)


272
273
274
# File 'lib/geomotion/cg_rect.rb', line 272

def infinite?
  self.size.infinite? || CGRectEqualToRect(self, CGRectInfinite)
end

#inspectObject



312
313
314
# File 'lib/geomotion/cg_rect.rb', line 312

def inspect
  "#{self.class.name}([#{self.origin.x}, #{self.origin.y}], [#{self.size.width}, #{self.size.height}])"
end

#intersection_with(rect) ⇒ Object



246
247
248
# File 'lib/geomotion/cg_rect.rb', line 246

def intersection_with(rect)
  CGRectIntersection(self, rect)
end

#intersects?(rect) ⇒ Boolean

Returns:

  • (Boolean)


280
281
282
283
284
285
286
287
# File 'lib/geomotion/cg_rect.rb', line 280

def intersects?(rect)
  case rect
  when CGRect
    CGRectIntersectsRect(self, rect)
  else
    super  # raises an error
  end
end

#left(dist = 0) ⇒ Object

modified rects



138
139
140
# File 'lib/geomotion/cg_rect.rb', line 138

def left(dist = 0)
  CGRect.new([self.x - dist, self.y], self.size)
end

#max_xObject



80
81
82
# File 'lib/geomotion/cg_rect.rb', line 80

def max_x
  CGRectGetMaxX(self)
end

#max_yObject



88
89
90
# File 'lib/geomotion/cg_rect.rb', line 88

def max_y
  CGRectGetMaxY(self)
end

#min_xObject

bounds



76
77
78
# File 'lib/geomotion/cg_rect.rb', line 76

def min_x
  CGRectGetMinX(self)
end

#min_yObject



84
85
86
# File 'lib/geomotion/cg_rect.rb', line 84

def min_y
  CGRectGetMinY(self)
end

#null?Boolean

Returns:

  • (Boolean)


276
277
278
# File 'lib/geomotion/cg_rect.rb', line 276

def null?
  CGRectIsNull(self)
end

#right(dist = 0) ⇒ Object



142
143
144
# File 'lib/geomotion/cg_rect.rb', line 142

def right(dist = 0)
  CGRect.new([self.x + dist, self.y], self.size)
end

#roundObject

others



223
224
225
# File 'lib/geomotion/cg_rect.rb', line 223

def round
  CGRect.new([self.x.round, self.y.round], [self.width.round, self.height.round])
end

#shorter(dist) ⇒ Object



166
167
168
# File 'lib/geomotion/cg_rect.rb', line 166

def shorter(dist)
  CGRect.new(self.origin, [self.width, self.height - dist])
end

#shrink(size) ⇒ Object



261
262
263
264
265
266
# File 'lib/geomotion/cg_rect.rb', line 261

def shrink(size)
  if size.is_a? Numeric
    size = CGSize.new(size, size)
  end
  CGRectInset(self, size.width, size.height)
end

#taller(dist) ⇒ Object



162
163
164
# File 'lib/geomotion/cg_rect.rb', line 162

def taller(dist)
  CGRect.new(self.origin, [self.width, self.height + dist])
end

#thinner(dist) ⇒ Object



158
159
160
# File 'lib/geomotion/cg_rect.rb', line 158

def thinner(dist)
  CGRect.new(self.origin, [self.width - dist, self.height])
end

#top_leftObject



206
207
208
# File 'lib/geomotion/cg_rect.rb', line 206

def top_left
  CGPoint.new(CGRectGetMinX(self), CGRectGetMinY(self))
end

#top_rightObject



210
211
212
# File 'lib/geomotion/cg_rect.rb', line 210

def top_right
  CGPoint.new(CGRectGetMaxX(self), CGRectGetMinY(self))
end

#union_with(rect) ⇒ Object



250
251
252
# File 'lib/geomotion/cg_rect.rb', line 250

def union_with(rect)
  CGRectUnion(self, rect)
end

#up(dist = 0) ⇒ Object



146
147
148
# File 'lib/geomotion/cg_rect.rb', line 146

def up(dist = 0)
  CGRect.new([self.x, self.y - dist], self.size)
end

#wider(dist) ⇒ Object



154
155
156
# File 'lib/geomotion/cg_rect.rb', line 154

def wider(dist)
  CGRect.new(self.origin, [self.width + dist, self.height])
end

#width(setter = nil) ⇒ Object



115
116
117
118
119
120
# File 'lib/geomotion/cg_rect.rb', line 115

def width(setter = nil)
  if setter
    return CGRect.new(self.origin, [setter, self.height])
  end
  CGRectGetWidth(self)
end

#width=(_width) ⇒ Object



122
123
124
# File 'lib/geomotion/cg_rect.rb', line 122

def width=(_width)
  self.size.width = _width
end

#x(setter = nil) ⇒ Object

getters/setters



93
94
95
96
97
98
# File 'lib/geomotion/cg_rect.rb', line 93

def x(setter = nil)
  if setter
    return CGRect.new([setter, self.y], self.size)
  end
  min_x
end

#x=(_x) ⇒ Object



100
101
102
# File 'lib/geomotion/cg_rect.rb', line 100

def x=(_x)
  self.origin.x = _x
end

#y(setter = nil) ⇒ Object



104
105
106
107
108
109
# File 'lib/geomotion/cg_rect.rb', line 104

def y(setter = nil)
  if setter
    return CGRect.new([self.x, setter], self.size)
  end
  min_y
end

#y=(_y) ⇒ Object



111
112
113
# File 'lib/geomotion/cg_rect.rb', line 111

def y=(_y)
  self.origin.y = _y
end