Class: CGAffineTransform

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.identityObject

Returns the CGAffineTransform identity matrix

Returns:

  • CGAffineTransform



79
80
81
# File 'lib/geomotion/cg_affine_transform.rb', line 79

def self.identity
  CGAffineTransformIdentity
end

.make(options = {}) ⇒ Object

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)



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 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) ⇒ Object

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

Returns:

  • CGAffineTransform



65
66
67
# File 'lib/geomotion/cg_affine_transform.rb', line 65

def self.rotate(angle)
  CGAffineTransformMakeRotation(angle)
end

.scale(scale, sy = nil) ⇒ Object

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.



51
52
53
54
55
56
57
58
59
60
61
# 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) ⇒ Object

A “shear” translation turns a rectangle into a parallelogram

Parameters:

  • px (Numeric)

    how much to shear in x direction

  • py (Numeric)

    how much to shear in y direction

Returns:

  • CGAffineTransform



73
74
75
# 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) ⇒ Object

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.

Returns:

  • CGAffineTransform



38
39
40
41
42
43
44
45
46
# 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

Instance Method Details

#+@Object

Returns self

Returns:

  • CGAffineTransform



101
102
103
# File 'lib/geomotion/cg_affine_transform.rb', line 101

def +@
  self
end

#-(transform) ⇒ Object

Inverts the second transform and adds the result to ‘self`

Returns:

  • CGAffineTransform



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

def -(transform)
  self.concat transform.invert
end

#==(transform) ⇒ Boolean

Returns true if the two matrices are equal.

Returns:

  • (Boolean)

    true if the two matrices are equal



95
96
97
# File 'lib/geomotion/cg_affine_transform.rb', line 95

def ==(transform)
  CGAffineTransformEqualToTransform(self, transform)
end

#apply_to(thing) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
# 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) ⇒ Object Also known as: +, <<

Concatenates the two transforms

Returns:

  • CGAffineTransform



107
108
109
# File 'lib/geomotion/cg_affine_transform.rb', line 107

def concat(transform)
  CGAffineTransformConcat(self, transform)
end

#identity?Boolean

Return true if the receiver is the identity matrix, false otherwise

Returns:

  • (Boolean)

    CGAffineTransform



85
86
87
# File 'lib/geomotion/cg_affine_transform.rb', line 85

def identity?
  CGAffineTransformIsIdentity(self)
end

#invertObject Also known as: -@

Inverts the transform

Returns:

  • CGAffineTransform



115
116
117
# File 'lib/geomotion/cg_affine_transform.rb', line 115

def invert
  CGAffineTransformInvert(self)
end

#rotate(angle) ⇒ Object

Applies a rotation transform to the receiver

Returns:

  • CGAffineTransform



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

def rotate(angle)
  CGAffineTransformRotate(self, angle)
end

#scale(scale, sy = nil) ⇒ Object

Applies a scale transform to the receiver

Returns:

  • CGAffineTransform



140
141
142
143
144
145
146
147
148
149
150
# 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) ⇒ Object

Applies a shear transform to the receiver

Returns:

  • CGAffineTransform



160
161
162
# File 'lib/geomotion/cg_affine_transform.rb', line 160

def shear(px, py)
  self.concat CGAffineTransform.shear(px, py)
end

#to_aObject



177
178
179
# 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_valueObject



181
182
183
# File 'lib/geomotion/cg_affine_transform.rb', line 181

def to_ns_value
  NSValue.valueWithCGAffineTransform(self)
end

#to_transform3dCATransform3D

Returns:



90
91
92
# File 'lib/geomotion/cg_affine_transform.rb', line 90

def to_transform3d
  CATransform3DMakeAffineTransform(self)
end

#translate(point, ty = nil) ⇒ Object

Applies a translation transform to the receiver

Returns:

  • CGAffineTransform



128
129
130
131
132
133
134
135
136
# 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