Class: CGAffineTransform
- Inherits:
-
Object
- Object
- CGAffineTransform
- Defined in:
- lib/geomotion/cg_affine_transform.rb
Class Method Summary collapse
-
.identity ⇒ Object
Returns the CGAffineTransform identity matrix.
-
.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).
-
.rotate(angle) ⇒ Object
Returns a transform that is rotated by ‘angle` (+ => counterclockwise, - => clockwise).
-
.scale(scale, sy = nil) ⇒ Object
Returns a transform that is scaled.
-
.shear(px, py) ⇒ Object
A “shear” translation turns a rectangle into a parallelogram.
-
.translate(point, ty = nil) ⇒ Object
Returns a transform that is translated.
Instance Method Summary collapse
-
#+@ ⇒ Object
Returns self.
-
#-(transform) ⇒ Object
Inverts the second transform and adds the result to ‘self`.
-
#==(transform) ⇒ Boolean
True if the two matrices are equal.
- #apply_to(thing) ⇒ Object
-
#concat(transform) ⇒ Object
(also: #+, #<<)
Concatenates the two transforms.
-
#identity? ⇒ Boolean
Return true if the receiver is the identity matrix, false otherwise.
-
#invert ⇒ Object
(also: #-@)
Inverts the transform.
-
#rotate(angle) ⇒ Object
Applies a rotation transform to the receiver.
-
#scale(scale, sy = nil) ⇒ Object
Applies a scale transform to the receiver.
-
#shear(px, py) ⇒ Object
Applies a shear transform to the receiver.
- #to_a ⇒ Object
- #to_ns_value ⇒ Object
- #to_transform3d ⇒ CATransform3D
-
#translate(point, ty = nil) ⇒ Object
Applies a translation transform to the receiver.
Class Method Details
.identity ⇒ Object
Returns the CGAffineTransform identity matrix
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( = {}) if .key?(:a) args = [ :a, :b, :c, :d, :tx, :ty, ].map do |key| raise "#{key.inspect} is required" unless .key? key [key] end return self.new(*args) else retval = self.identity if [:translate] retval = retval.translate([:translate]) end if [:scale] retval = retval.scale([:scale]) end if [:rotate] retval = retval.rotate([:rotate]) end return retval end end |
.rotate(angle) ⇒ Object
Returns a transform that is rotated by ‘angle` (+ => counterclockwise, - => clockwise)
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
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.
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
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`
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.
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
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
85 86 87 |
# File 'lib/geomotion/cg_affine_transform.rb', line 85 def identity? CGAffineTransformIsIdentity(self) end |
#invert ⇒ Object Also known as: -@
Inverts the transform
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
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
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
160 161 162 |
# File 'lib/geomotion/cg_affine_transform.rb', line 160 def shear(px, py) self.concat CGAffineTransform.shear(px, py) end |
#to_a ⇒ Object
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_value ⇒ Object
181 182 183 |
# File 'lib/geomotion/cg_affine_transform.rb', line 181 def to_ns_value NSValue.valueWithCGAffineTransform(self) end |
#to_transform3d ⇒ CATransform3D
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
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 |