class CGAffineTransform

Public Class Methods

identity() click to toggle source

Returns the CGAffineTransform identity matrix @return CGAffineTransform

# File lib/geomotion/cg_affine_transform.rb, line 79
def self.identity
  CGAffineTransformIdentity
end
make(options = {}) click to toggle source

CGAffineTransform.make # default transform: identity matrix # make a transform from scratch CGAffineTransform.make(a: 1, b: 0, c: 0, d: 1, tx: 0, ty: 0)

# make a transform using primitives CGAffineTransform.make(scale: 2, translate: [10, 10], rotate: Math::PI)

# File lib/geomotion/cg_affine_transform.rb, line 8
def self.make(options = {})
  if options.key?(:a)
    args = [
      :a, :b,
      :c, :d,
      :tx, :ty,
    ].map do |key|
      raise "#{key.inspect} is required" unless options.key? key
      options[key]
    end
    return self.new(*args)
  else
    retval = self.identity
    if options[:translate]
      retval = retval.translate(options[:translate])
    end
    if options[:scale]
      retval = retval.scale(options[:scale])
    end
    if options[:rotate]
      retval = retval.rotate(options[:rotate])
    end
    return retval
  end
end
rotate(angle) click to toggle source

Returns a transform that is rotated by ‘angle` (+ => counterclockwise, - => clockwise) @return CGAffineTransform

# File lib/geomotion/cg_affine_transform.rb, line 65
def self.rotate(angle)
  CGAffineTransformMakeRotation(angle)
end
scale(scale, sy=nil) click to toggle source

Returns a transform that is scaled. Accepts one or two arguments. One argument can be a Point or Array with two items or a number that will be used to scale both directions. Two arguments should be the x and y values.

# File lib/geomotion/cg_affine_transform.rb, line 51
def self.scale(scale, sy=nil)
  if sy
    sx = scale
  elsif scale.is_a?(Numeric)
    sx = sy = scale
  else
    sx = scale[0]
    sy = scale[1]
  end
  CGAffineTransformMakeScale(sx, sy)
end
shear(px, py) click to toggle source

A “shear” translation turns a rectangle into a parallelogram @param px [Numeric] how much to shear in x direction @param py [Numeric] how much to shear in y direction @return CGAffineTransform

# File lib/geomotion/cg_affine_transform.rb, line 73
def self.shear(px, py)
  CGAffineTransformMake(1, py, px, 1, 0, 0)
end
translate(point, ty=nil) click to toggle source

Returns a transform that is translated. Accepts one or two arguments. One argument can be a Point or Array with two items, two arguments should be the x and y values. @return CGAffineTransform

# File lib/geomotion/cg_affine_transform.rb, line 38
def self.translate(point, ty=nil)
  if ty
    tx = point
  else
    tx = point[0]
    ty = point[1]
  end
  CGAffineTransformMakeTranslation(tx, ty)
end

Public Instance Methods

+(transform)
Alias for: concat
+@() click to toggle source

Returns self @return CGAffineTransform

# File lib/geomotion/cg_affine_transform.rb, line 101
def +@
  self
end
-(transform) click to toggle source

Inverts the second transform and adds the result to ‘self` @return CGAffineTransform

# File lib/geomotion/cg_affine_transform.rb, line 122
def -(transform)
  self.concat transform.invert
end
-@()
Alias for: invert
<<(transform)
Alias for: concat
==(transform) click to toggle source

@return [Boolean] true if the two matrices are equal

# File lib/geomotion/cg_affine_transform.rb, line 95
def ==(transform)
  CGAffineTransformEqualToTransform(self, transform)
end
apply_to(thing) click to toggle source
# File lib/geomotion/cg_affine_transform.rb, line 164
def apply_to(thing)
  case thing
  when CGPoint
    CGPointApplyAffineTransform(thing, self)
  when CGSize
    CGSizeApplyAffineTransform(thing, self)
  when CGRect
    CGRectApplyAffineTransform(thing, self)
  else
    raise "Cannot apply transform to #{thing.inspect}"
  end
end
concat(transform) click to toggle source

Concatenates the two transforms @return CGAffineTransform

# File lib/geomotion/cg_affine_transform.rb, line 107
def concat(transform)
  CGAffineTransformConcat(self, transform)
end
Also aliased as: +, <<
identity?() click to toggle source

Return true if the receiver is the identity matrix, false otherwise @return CGAffineTransform

# File lib/geomotion/cg_affine_transform.rb, line 85
def identity?
  CGAffineTransformIsIdentity(self)
end
invert() click to toggle source

Inverts the transform @return CGAffineTransform

# File lib/geomotion/cg_affine_transform.rb, line 115
def invert
  CGAffineTransformInvert(self)
end
Also aliased as: -@
rotate(angle) click to toggle source

Applies a rotation transform to the receiver @return CGAffineTransform

# File lib/geomotion/cg_affine_transform.rb, line 154
def rotate(angle)
  CGAffineTransformRotate(self, angle)
end
scale(scale, sy=nil) click to toggle source

Applies a scale transform to the receiver @return CGAffineTransform

# File lib/geomotion/cg_affine_transform.rb, line 140
def scale(scale, sy=nil)
  if sy
    sx = scale
  elsif scale.is_a?(Numeric)
    sx = sy = scale
  else
    sx = scale[0]
    sy = scale[1]
  end
  CGAffineTransformScale(self, sx, sy)
end
shear(px, py) click to toggle source

Applies a shear transform to the receiver @return CGAffineTransform

# File lib/geomotion/cg_affine_transform.rb, line 160
def shear(px, py)
  self.concat CGAffineTransform.shear(px, py)
end
to_a() click to toggle source
# File lib/geomotion/cg_affine_transform.rb, line 177
def to_a
  [self.a, self.b, self.c, self.d, self.tx, self.ty]
end
to_ns_value() click to toggle source
# File lib/geomotion/cg_affine_transform.rb, line 181
def to_ns_value
  NSValue.valueWithCGAffineTransform(self)
end
to_transform3d() click to toggle source

@return [CATransform3D]

# File lib/geomotion/cg_affine_transform.rb, line 90
def to_transform3d
  CATransform3DMakeAffineTransform(self)
end
translate(point, ty=nil) click to toggle source

Applies a translation transform to the receiver @return CGAffineTransform

# File lib/geomotion/cg_affine_transform.rb, line 128
def translate(point, ty=nil)
  if ty
    tx = point
  else
    tx = point[0]
    ty = point[1]
  end
  CGAffineTransformTranslate(self, tx, ty)
end

Private Instance Methods

to_ary() click to toggle source
# File lib/geomotion/cg_affine_transform.rb, line 186
def to_ary
  to_a
end