class CGRect

Public Class Methods

empty()
Alias for: zero
infinite() click to toggle source
# File lib/geomotion/cg_rect.rb, line 40
def 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) click to toggle source

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

# File lib/geomotion/cg_rect.rb, line 53
def 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 = {}) click to toggle source

CGRect.make # default rect: {origin: {x: 0, y: 0}, size: {width:0, 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)

# File lib/geomotion/cg_rect.rb, line 12
def make(options = {})
  if options[:origin]
    x = options[:origin][0]
    y = options[:origin][1]
  else
    x = options[:x] || 0
    y = options[:y] || 0
  end
  if options[:size]
    w = options[:size][0]
    h = options[:size][1]
  else
    w = options[:width] || 0
    h = options[:height] || 0
  end
  self.new([x, y], [w, h])
end
null() click to toggle source
# File lib/geomotion/cg_rect.rb, line 35
def null
  # Don't just return CGRectNull; can be mutated
  CGRect.new([Float::INFINITY, Float::INFINITY], [0, 0])
end
zero() click to toggle source
# File lib/geomotion/cg_rect.rb, line 30
def zero
  CGRect.new([0, 0], [0, 0])
end
Also aliased as: empty

Public Instance Methods

*(scale) click to toggle source
Calls superclass method
# File lib/geomotion/cg_rect.rb, line 497
def *(scale)
  case scale
  when Numeric
    return CGRect.new(self.origin, self.size * scale)
  else
    super
  end
end
+(other) click to toggle source
# File lib/geomotion/cg_rect.rb, line 482
def +(other)
  case other
  when CGRect
    return self.union_with(other)
  when CGSize
    return CGRect.new([self.origin.x, self.origin.y], [self.size.width + other.width, self.size.height + other.height])
  when CGPoint
    return self.offset(other.x, other.y)
  when UIOffset
    return self.offset(other.horizontal, other.vertical)
  when UIEdgeInsets
    return self.inset(other)
  end
end
-(other) click to toggle source
# File lib/geomotion/cg_rect.rb, line 659
def -(other)
  self.+(-other)
end
-@() click to toggle source
# File lib/geomotion/cg_rect.rb, line 655
def -@
  CGRect.new(-self.origin, -self.size)
end
/(scale) click to toggle source

it is tempting to define this as self * (1.0/scale) but floating point errors result in too many errors

Calls superclass method
# File lib/geomotion/cg_rect.rb, line 508
def /(scale)
  case scale
  when Numeric
    return CGRect.new(self.origin, self.size / scale)
  else
    super
  end
end
==(rect) click to toggle source
# File lib/geomotion/cg_rect.rb, line 651
def ==(rect)
  rect.is_a?(CGRect) && CGRectEqualToRect(self, rect)
end
above(margin = 0, options={}) click to toggle source

adjacent rects

# File lib/geomotion/cg_rect.rb, line 321
def above(margin = 0, options={})
  margin, options = 0, margin if margin.is_a?(NSDictionary)
  margin = options.delete(:margin) if options.key?(:margin)

  height = options[:height] || self.size.height
  self.apply({
    up: height + margin
    }).apply(options)
end
after(margin = 0, options={})
Alias for: beside
apply(options) click to toggle source

most rect modifiers call this method in one way or another

# File lib/geomotion/cg_rect.rb, line 166
def apply(options)
  rect = CGRectStandardize(CGRect.new(self.origin, self.size))
  options.each do |method, value|
    case method
    when :left
      rect.origin.x -= value
    when :right
      rect.origin.x += value
    when :up
      rect.origin.y -= value
    when :down
      rect.origin.y += value
    when :wider, :grow_right
      rect.size.width += value
    when :thinner, :shrink_left
      rect.size.width -= value
    when :taller, :grow_down
      rect.size.height += value
    when :shorter, :shrink_up
      rect.size.height -= value
    when :x
      rect.origin.x = value
    when :y
      rect.origin.y = value
    when :origin
      rect.origin = value
    when :width
      rect.size.width = value
    when :height
      rect.size.height = value
    when :size
      rect.size = value
    when :grow
      rect = rect.grow(value)
    when :grow_up
      rect.size.height += value
      rect.origin.y -= value
    when :shrink_down
      rect.size.height -= value
      rect.origin.y += value
    when :grow_left
      rect.size.width += value
      rect.origin.x -= value
    when :shrink_right
      rect.size.width -= value
      rect.origin.x += value
    when :grow_width
      rect = rect.grow_width(value)
    when :grow_height
      rect = rect.grow_height(value)
    when :shrink
      rect = rect.shrink(value)
    when :shrink_width
      rect = rect.shrink_width(value)
    when :shrink_height
      rect = rect.shrink_height(value)
    when :offset
      rect = rect.offset(value)
    else
      raise "Unknow option #{method}"
    end
  end
  return rect
end
before(margin = 0, options={}) click to toggle source
# File lib/geomotion/cg_rect.rb, line 340
def before(margin = 0, options={})
  margin, options = 0, margin if margin.is_a?(NSDictionary)
  margin = options.delete(:margin) if options.key?(:margin)

  width = options[:width] || self.size.width
  self.apply({
    left: width + margin
    }).apply(options)
end
below(margin = 0, options={}) click to toggle source
# File lib/geomotion/cg_rect.rb, line 331
def below(margin = 0, options={})
  margin, options = 0, margin if margin.is_a?(NSDictionary)
  margin = options.delete(:margin) if options.key?(:margin)

  self.apply({
    down: self.size.height + margin
    }).apply(options)
end
beside(margin = 0, options={}) click to toggle source
# File lib/geomotion/cg_rect.rb, line 350
def beside(margin = 0, options={})
  margin, options = 0, margin if margin.is_a?(NSDictionary)
  margin = options.delete(:margin) if options.key?(:margin)

  self.apply({
    right: self.size.width + margin
    }).apply(options)
end
Also aliased as: after
bottom_center(absolute = false) click to toggle source
# File lib/geomotion/cg_rect.rb, line 457
def bottom_center(absolute = false)
  cgrect_offset(absolute) + CGPoint.new(self.size.width / 2, self.size.height)
end
bottom_left(absolute = false) click to toggle source
# File lib/geomotion/cg_rect.rb, line 461
def bottom_left(absolute = false)
  cgrect_offset(absolute) + CGPoint.new(0, self.size.height)
end
bottom_right(absolute = false) click to toggle source
# File lib/geomotion/cg_rect.rb, line 453
def bottom_right(absolute = false)
  cgrect_offset(absolute) + CGPoint.new(self.size.width, self.size.height)
end
center(absolute = false) click to toggle source
# File lib/geomotion/cg_rect.rb, line 433
def center(absolute = false)
  cgrect_offset(absolute) + CGPoint.new(self.size.width / 2, self.size.height / 2)
end
center_left(absolute = false) click to toggle source
# File lib/geomotion/cg_rect.rb, line 465
def center_left(absolute = false)
  cgrect_offset(absolute) + CGPoint.new(0, self.size.height / 2)
end
center_right(absolute = false) click to toggle source
# File lib/geomotion/cg_rect.rb, line 449
def center_right(absolute = false)
  cgrect_offset(absolute) + CGPoint.new(self.size.width, self.size.height / 2)
end
centered_in(rect, absolute = false) click to toggle source
# File lib/geomotion/cg_rect.rb, line 478
def centered_in(rect, absolute = false)
  self.size.centered_in(rect, absolute)
end
contains?(rect_or_point) click to toggle source
Calls superclass method
# File lib/geomotion/cg_rect.rb, line 640
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=nil, options={}) click to toggle source
# File lib/geomotion/cg_rect.rb, line 271
def down(dist=nil, options={})
  if dist.nil?
    NSLog("Using the default value of `0` in `CGRect#down` is deprecated.")
    dist = 0
  end
  raise "You cannot specify `:down` in `CGRect#down`" if options.key?(:down)
  raise "You must specify an amount in `CGRect#down`" unless dist.is_a?(Numeric)

  self.apply({
    down: dist
    }).apply(options)
end
empty?() click to toggle source
# File lib/geomotion/cg_rect.rb, line 619
def empty?
  CGRectIsEmpty(self)
end
from_bottom(options={}) click to toggle source

Create a rect inside the receiver, on the bottom side. If ‘margin` is supplied, the rect will be moved that number of points up.

# File lib/geomotion/cg_rect.rb, line 409
def from_bottom(options={})
  height = options[:height]
  margin = options.delete(:margin) || 0
  raise "You must specify a height in `CGRect#from_bottom`" unless height
  offset = cgrect_offset(options.delete(:absolute))
  self.apply({
    x: offset.x,
    y: offset.y + self.size.height - height - margin,
    width: self.size.width,
    height: height,
    }).apply(options)
end
from_left(options={}) click to toggle source

Create a rect inside the receiver, on the left side. If ‘margin` is supplied, the rect will be moved that number of points to the right.

# File lib/geomotion/cg_rect.rb, line 364
def from_left(options={})
  width = options[:width]
  margin = options.delete(:margin) || 0
  raise "You must specify a width in `CGRect#from_left`" unless width
  offset = cgrect_offset(options.delete(:absolute))
  self.apply({
    x: offset.x + margin,
    y: offset.y,
    width: width,
    height: self.size.height,
    }).apply(options)
end
from_right(options={}) click to toggle source

Create a rect inside the receiver, on the right side. If ‘margin` is supplied, the rect will be moved that number of points to the left.

# File lib/geomotion/cg_rect.rb, line 379
def from_right(options={})
  width = options[:width]
  margin = options.delete(:margin) || 0
  raise "You must specify a width in `CGRect#from_right`" unless width
  offset = cgrect_offset(options.delete(:absolute))
  self.apply({
    x: offset.x + self.size.width - width - margin,
    y: offset.y,
    width: width,
    height: self.size.height,
    }).apply(options)
end
from_top(options={}) click to toggle source

Create a rect inside the receiver, on the top side. If ‘margin` is supplied, the rect will be moved that number of points down.

# File lib/geomotion/cg_rect.rb, line 394
def from_top(options={})
  height = options[:height]
  margin = options.delete(:margin) || 0
  raise "You must specify a height in `CGRect#from_top`" unless height
  offset = cgrect_offset(options.delete(:absolute))
  self.apply({
    x: offset.x,
    y: offset.y + margin,
    width: self.size.width,
    height: height,
    }).apply(options)
end
grow(size, options=nil) click to toggle source
# File lib/geomotion/cg_rect.rb, line 537
def grow(size, options=nil)
  if size.is_a? Numeric
    size = CGSize.new(size, size)
  end
  rect = CGRectInset(self, -size[0], -size[1])
  if options
    return rect.apply(options)
  end
  return rect
end
grow_down(dist, options={})
Alias for: taller
grow_height(amount, options={}) click to toggle source
# File lib/geomotion/cg_rect.rb, line 574
def grow_height(amount, options={})
  return self.grow([0, amount], options)
end
grow_left(amount, options={}) click to toggle source
# File lib/geomotion/cg_rect.rb, line 550
def grow_left(amount, options={})
  raise "You cannot specify `:grow_left` in `CGRect#grow_left`" if options.key?(:grow_left)
  raise "You must specify an amount in `CGRect#grow_left`" unless amount.is_a?(Numeric)

  self.apply({
    grow_left: amount
    }).apply(options)
end
grow_right(dist, options={})
Alias for: wider
grow_up(amount, options={}) click to toggle source
# File lib/geomotion/cg_rect.rb, line 561
def grow_up(amount, options={})
  raise "You cannot specify `:grow_up` in `CGRect#grow_up`" if options.key?(:grow_up)
  raise "You must specify an amount in `CGRect#grow_up`" unless amount.is_a?(Numeric)

  self.apply({
    grow_up: amount
    }).apply(options)
end
grow_width(amount, options={}) click to toggle source
# File lib/geomotion/cg_rect.rb, line 570
def grow_width(amount, options={})
  return self.grow([amount, 0], options)
end
height(setter=nil, options=nil) click to toggle source
# File lib/geomotion/cg_rect.rb, line 150
def height(setter=nil, options=nil)
  if setter
    rect = CGRect.new(self.origin, [self.size.width, setter])
    if options
      return rect.apply(options)
    end
    return rect
  end
  return CGRectGetHeight(self)
end
height=(_height) click to toggle source
# File lib/geomotion/cg_rect.rb, line 161
def height=(_height)
  self.size.height = _height
end
infinite?() click to toggle source
# File lib/geomotion/cg_rect.rb, line 623
def infinite?
  self.size.infinite? || CGRectEqualToRect(self, CGRectInfinite)
end
inset(insets) click to toggle source
# File lib/geomotion/cg_rect.rb, line 525
def inset(insets)
  UIEdgeInsetsInsetRect(self, insets)
end
inspect() click to toggle source
# File lib/geomotion/cg_rect.rb, line 663
def inspect
  "#{self.class.name}([#{self.origin.x}, #{self.origin.y}], [#{self.size.width}, #{self.size.height}])"
end
integral() click to toggle source
# File lib/geomotion/cg_rect.rb, line 474
def integral
  CGRectIntegral(self)
end
intersection_with(rect) click to toggle source
# File lib/geomotion/cg_rect.rb, line 517
def intersection_with(rect)
  CGRectIntersection(self, rect)
end
intersects?(rect) click to toggle source
Calls superclass method
# File lib/geomotion/cg_rect.rb, line 631
def intersects?(rect)
  case rect
  when CGRect
    CGRectIntersectsRect(self, rect)
  else
    super  # raises an error
  end
end
left(dist=nil, options={}) click to toggle source

modified rects

# File lib/geomotion/cg_rect.rb, line 232
def left(dist=nil, options={})
  if dist.nil?
    NSLog("Using the default value of `0` in `CGRect#left` is deprecated.")
    dist = 0
  end
  raise "You cannot specify `:left` in `CGRect#left`" if options.key?(:left)
  raise "You must specify an amount in `CGRect#left`" unless dist.is_a?(Numeric)

  self.apply({
    left: dist
    }).apply(options)
end
max_x() click to toggle source
# File lib/geomotion/cg_rect.rb, line 88
def max_x
  CGRectGetMaxX(self)
end
max_y() click to toggle source
# File lib/geomotion/cg_rect.rb, line 100
def max_y
  CGRectGetMaxY(self)
end
mid_x() click to toggle source
# File lib/geomotion/cg_rect.rb, line 84
def mid_x
  CGRectGetMidX(self)
end
mid_y() click to toggle source
# File lib/geomotion/cg_rect.rb, line 96
def mid_y
  CGRectGetMidY(self)
end
min_x() click to toggle source

bounds

# File lib/geomotion/cg_rect.rb, line 80
def min_x
  CGRectGetMinX(self)
end
min_y() click to toggle source
# File lib/geomotion/cg_rect.rb, line 92
def min_y
  CGRectGetMinY(self)
end
null?() click to toggle source
# File lib/geomotion/cg_rect.rb, line 627
def null?
  CGRectIsNull(self)
end
offset(point_or_x, y=nil) click to toggle source
# File lib/geomotion/cg_rect.rb, line 529
def offset(point_or_x, y=nil)
  if y
    CGRectOffset(self, point_or_x, y)
  else
    CGRectOffset(self, point_or_x[0], point_or_x[1])
  end
end
right(dist=nil, options={}) click to toggle source
# File lib/geomotion/cg_rect.rb, line 245
def right(dist=nil, options={})
  if dist.nil?
    NSLog("Using the default value of `0` in `CGRect#right` is deprecated.")
    dist = 0
  end
  raise "You cannot specify `:right` in `CGRect#right`" if options.key?(:right)
  raise "You must specify an amount in `CGRect#right`" unless dist.is_a?(Numeric)

  self.apply({
    right: dist
    }).apply(options)
end
round() click to toggle source

others

# File lib/geomotion/cg_rect.rb, line 470
def round
  CGRect.new([self.origin.x.round, self.origin.y.round], [self.size.width.round, self.size.height.round])
end
shorter(dist, options={}) click to toggle source
# File lib/geomotion/cg_rect.rb, line 311
def shorter(dist, options={})
  raise "You cannot specify `:shorter` in `CGRect#shorter`" if options.key?(:shorter)
  raise "You must specify an amount in `CGRect#shorter`" unless dist.is_a?(Numeric)

  self.apply({
    shorter: dist
    }).apply(options)
end
Also aliased as: shrink_up
shrink(size, options=nil) click to toggle source
# File lib/geomotion/cg_rect.rb, line 578
def shrink(size, options=nil)
  if size.is_a? Numeric
    size = CGSize.new(size, size)
  end
  rect = CGRectInset(self, size[0], size[1])
  if options
    return rect.apply(options)
  end
  return rect
end
shrink_down(amount, options={}) click to toggle source
# File lib/geomotion/cg_rect.rb, line 602
def shrink_down(amount, options={})
  raise "You cannot specify `:shrink_down` in `CGRect#shrink_down`" if options.key?(:shrink_down)
  raise "You must specify an amount in `CGRect#shrink_down`" unless amount.is_a?(Numeric)

  self.apply({
    shrink_down: amount
    }).apply(options)
end
shrink_height(amount, options={}) click to toggle source
# File lib/geomotion/cg_rect.rb, line 615
def shrink_height(amount, options={})
  return self.shrink([0, amount], options)
end
shrink_left(dist, options={})
Alias for: thinner
shrink_right(amount, options={}) click to toggle source
# File lib/geomotion/cg_rect.rb, line 591
def shrink_right(amount, options={})
  raise "You cannot specify `:shrink_right` in `CGRect#shrink_right`" if options.key?(:shrink_right)
  raise "You must specify an amount in `CGRect#shrink_right`" unless amount.is_a?(Numeric)

  self.apply({
    shrink_right: amount
    }).apply(options)
end
shrink_up(dist, options={})
Alias for: shorter
shrink_width(amount, options={}) click to toggle source
# File lib/geomotion/cg_rect.rb, line 611
def shrink_width(amount, options={})
  return self.shrink([amount, 0], options)
end
taller(dist, options={}) click to toggle source
# File lib/geomotion/cg_rect.rb, line 302
def taller(dist, options={})
  raise "You cannot specify `:taller` in `CGRect#taller`" if options.key?(:taller)
  raise "You must specify an amount in `CGRect#taller`" unless dist.is_a?(Numeric)

  self.apply({
    taller: dist
    }).apply(options)
end
Also aliased as: grow_down
thinner(dist, options={}) click to toggle source
# File lib/geomotion/cg_rect.rb, line 293
def thinner(dist, options={})
  raise "You cannot specify `:thinner` in `CGRect#thinner`" if options.key?(:thinner)
  raise "You must specify an amount in `CGRect#thinner`" unless dist.is_a?(Numeric)

  self.apply({
    thinner: dist
    }).apply(options)
end
Also aliased as: shrink_left
to_ns_value() click to toggle source
# File lib/geomotion/cg_rect.rb, line 667
def to_ns_value
  NSValue.valueWithCGRect(self)
end
top_center(absolute = false) click to toggle source
# File lib/geomotion/cg_rect.rb, line 441
def top_center(absolute = false)
  cgrect_offset(absolute) + CGPoint.new(self.size.width / 2, 0)
end
top_left(absolute = false) click to toggle source
# File lib/geomotion/cg_rect.rb, line 437
def top_left(absolute = false)
  cgrect_offset(absolute) + CGPoint.new(0, 0)
end
top_right(absolute = false) click to toggle source
# File lib/geomotion/cg_rect.rb, line 445
def top_right(absolute = false)
  cgrect_offset(absolute) + CGPoint.new(self.size.width, 0)
end
union_with(rect) click to toggle source
# File lib/geomotion/cg_rect.rb, line 521
def union_with(rect)
  CGRectUnion(self, rect)
end
up(dist=nil, options={}) click to toggle source
# File lib/geomotion/cg_rect.rb, line 258
def up(dist=nil, options={})
  if dist.nil?
    NSLog("Using the default value of `0` in `CGRect#up` is deprecated.")
    dist = 0
  end
  raise "You cannot specify `:up` in `CGRect#up`" if options.key?(:up)
  raise "You must specify an amount in `CGRect#up`" unless dist.is_a?(Numeric)

  self.apply({
    up: dist
    }).apply(options)
end
wider(dist, options={}) click to toggle source
# File lib/geomotion/cg_rect.rb, line 284
def wider(dist, options={})
  raise "You cannot specify `:width` in `CGRect#width`" if options.key?(:width)
  raise "You must specify an amount in `CGRect#wider`" unless dist.is_a?(Numeric)

  self.apply({
    wider: dist
    }).apply(options)
end
Also aliased as: grow_right
width(setter=nil, options=nil) click to toggle source
# File lib/geomotion/cg_rect.rb, line 135
def width(setter=nil, options=nil)
  if setter
    rect = CGRect.new(self.origin, [setter, self.size.height])
    if options
      return rect.apply(options)
    end
    return rect
  end
  return CGRectGetWidth(self)
end
width=(_width) click to toggle source
# File lib/geomotion/cg_rect.rb, line 146
def width=(_width)
  self.size.width = _width
end
x(setter=nil, options=nil) click to toggle source

getters/setters

# File lib/geomotion/cg_rect.rb, line 105
def x(setter=nil, options=nil)
  if setter
    rect = CGRect.new([setter, self.origin.y], self.size)
    if options
      return rect.apply(options)
    end
    return rect
  end
  return min_x
end
x=(_x) click to toggle source
# File lib/geomotion/cg_rect.rb, line 116
def x=(_x)
  self.origin.x = _x
end
y(setter=nil, options=nil) click to toggle source
# File lib/geomotion/cg_rect.rb, line 120
def y(setter=nil, options=nil)
  if setter
    rect = CGRect.new([self.origin.x, setter], self.size)
    if options
      return rect.apply(options)
    end
    return rect
  end
  return min_y
end
y=(_y) click to toggle source
# File lib/geomotion/cg_rect.rb, line 131
def y=(_y)
  self.origin.y = _y
end

Private Instance Methods

cgrect_offset(absolute) click to toggle source

positions

# File lib/geomotion/cg_rect.rb, line 424
def cgrect_offset(absolute)
  if absolute
    CGPoint.new(self.min_x, self.min_y)
  else
    CGPoint.new(0, 0)
  end
end
to_ary() click to toggle source
# File lib/geomotion/cg_rect.rb, line 673
def to_ary
  [self.origin, self.size]
end