@@ -33,13 +33,14 @@ public struct AffineTransform : ReferenceConvertible, Hashable, CustomStringConv
33
33
public var tX : CGFloat
34
34
public var tY : CGFloat
35
35
36
- // init() returns affine transformation matrix with identity values.
36
+ /// Creates an affine transformation matrix with identity values.
37
37
public init ( ) {
38
38
self . init ( m11: 1.0 , m12: 0.0 ,
39
39
m21: 0.0 , m22: 1.0 ,
40
40
tX: 0.0 , tY: 0.0 )
41
41
}
42
42
43
+ /// Creates an affine transformation.
43
44
public init ( m11: CGFloat , m12: CGFloat , m21: CGFloat , m22: CGFloat , tX: CGFloat , tY: CGFloat ) {
44
45
( self . m11, self . m12, self . m21, self . m22) = ( m11, m12, m21, m22)
45
46
( self . tX, self . tY) = ( tX, tY)
@@ -185,11 +186,12 @@ public struct AffineTransform : ReferenceConvertible, Hashable, CustomStringConv
185
186
)
186
187
}
187
188
188
- // Scaling
189
+ /// Mutates an affine transformation matrix to perform the given scaling in both x and y dimensions.
189
190
public mutating func scale( _ scale: CGFloat ) {
190
191
self . scale ( x: scale, y: scale)
191
192
}
192
193
194
+ /// Mutates an affine transformation matrix to perform a scaling in each of the x and y dimensions.
193
195
public mutating func scale( x: CGFloat , y: CGFloat ) {
194
196
m11 = CGFloat ( m11. native * x. native)
195
197
m12 = CGFloat ( m12. native * x. native)
@@ -215,6 +217,7 @@ public struct AffineTransform : ReferenceConvertible, Hashable, CustomStringConv
215
217
self = inverse
216
218
}
217
219
220
+ /// Returns an inverted version of the matrix if possible, or nil if not.
218
221
public func inverted( ) -> AffineTransform ? {
219
222
let determinant = ( m11 * m22) - ( m12 * m21)
220
223
if fabs ( determinant. native) <= ε. native {
@@ -230,38 +233,43 @@ public struct AffineTransform : ReferenceConvertible, Hashable, CustomStringConv
230
233
return inverse
231
234
}
232
235
233
- // Transforming with transform
236
+ /// Mutates an affine transformation by appending the specified matrix.
234
237
public mutating func append( _ transform: AffineTransform ) {
235
238
self = concatenated ( transform)
236
239
}
237
240
241
+ /// Mutates an affine transformation by prepending the specified matrix.
238
242
public mutating func prepend( _ transform: AffineTransform ) {
239
243
self = transform. concatenated ( self )
240
244
}
241
245
242
- // Transforming points and sizes
246
+ /// Applies the transform to the specified point and returns the result.
243
247
public func transform( _ point: NSPoint ) -> NSPoint {
244
248
var newPoint = NSPoint ( )
245
249
newPoint. x = ( m11 * point. x) + ( m21 * point. y) + tX
246
250
newPoint. y = ( m12 * point. x) + ( m22 * point. y) + tY
247
251
return newPoint
248
252
}
249
253
254
+ /// Applies the transform to the specified size and returns the result.
250
255
public func transform( _ size: NSSize ) -> NSSize {
251
256
var newSize = NSSize ( )
252
257
newSize. width = ( m11 * size. width) + ( m21 * size. height)
253
258
newSize. height = ( m12 * size. width) + ( m22 * size. height)
254
259
return newSize
255
260
}
256
261
262
+ /// The computed hash value for the transform.
257
263
public var hashValue : Int {
258
264
return Int ( ( m11 + m12 + m21 + m22 + tX + tY) . native)
259
265
}
260
266
267
+ /// A textual description of the transform.
261
268
public var description : String {
262
269
return " {m11: \( m11) , m12: \( m12) , m21: \( m21) , m22: \( m22) , tX: \( tX) , tY: \( tY) } "
263
270
}
264
271
272
+ /// A textual description of the transform suitable for debugging.
265
273
public var debugDescription : String {
266
274
return description
267
275
}
@@ -273,6 +281,8 @@ public struct AffineTransform : ReferenceConvertible, Hashable, CustomStringConv
273
281
}
274
282
}
275
283
284
+
285
+ /// A structure that defines the three-by-three matrix that performs an affine transform between two coordinate systems.
276
286
public struct NSAffineTransformStruct {
277
287
public var m11 : CGFloat
278
288
public var m12 : CGFloat
@@ -281,7 +291,7 @@ public struct NSAffineTransformStruct {
281
291
public var tX : CGFloat
282
292
public var tY : CGFloat
283
293
284
- // This is NOT an identity matrix
294
+ /// Initializes a zero-filled transformation matrix.
285
295
public init ( ) {
286
296
m11 = 0.0
287
297
m12 = 0.0
@@ -291,6 +301,7 @@ public struct NSAffineTransformStruct {
291
301
tY = 0.0
292
302
}
293
303
304
+ /// Initializes a transformation matrix with the given values.
294
305
public init ( m11: CGFloat , m12: CGFloat , m21: CGFloat , m22: CGFloat , tX: CGFloat , tY: CGFloat ) {
295
306
self . m11 = m11
296
307
self . m12 = m12
@@ -305,6 +316,7 @@ open class NSAffineTransform : NSObject, NSCopying, NSSecureCoding {
305
316
306
317
private var affineTransform : AffineTransform
307
318
319
+ /// The matrix coefficients stored as the transformation matrix.
308
320
public var transformStruct : NSAffineTransformStruct {
309
321
get {
310
322
return NSAffineTransformStruct ( m11: affineTransform. m11,
@@ -389,13 +401,13 @@ open class NSAffineTransform : NSObject, NSCopying, NSSecureCoding {
389
401
return true
390
402
}
391
403
392
- // Initialization
404
+ /// Initializes an affine transform matrix using another transform object.
393
405
public convenience init ( transform: AffineTransform ) {
394
406
self . init ( )
395
407
affineTransform = transform
396
408
}
397
409
398
- // init() returns affine transformation matrix with identity values .
410
+ /// Initializes an affine transform matrix to the identity matrix .
399
411
public override init ( ) {
400
412
affineTransform = AffineTransform (
401
413
m11: CGFloat ( 1.0 ) , m12: CGFloat ( ) ,
@@ -404,34 +416,36 @@ open class NSAffineTransform : NSObject, NSCopying, NSSecureCoding {
404
416
)
405
417
}
406
418
407
- // Translating
419
+ /// Applies the specified translation factors to the transformation matrix.
408
420
open func translateX( by deltaX: CGFloat , yBy deltaY: CGFloat ) {
409
421
let translation = AffineTransform ( translationByX: deltaX, byY: deltaY)
410
422
affineTransform = translation. concatenated ( affineTransform)
411
423
}
412
424
413
- // Rotating
425
+ /// Applies a rotation factor (measured in degrees) to the transformation matrix.
414
426
open func rotate( byDegrees angle: CGFloat ) {
415
427
let rotation = AffineTransform ( rotationByDegrees: angle)
416
428
affineTransform = rotation. concatenated ( affineTransform)
417
429
}
418
430
431
+ /// Applies a rotation factor (measured in radians) to the transformation matrix.
419
432
open func rotate( byRadians angle: CGFloat ) {
420
433
let rotation = AffineTransform ( rotationByRadians: angle)
421
434
affineTransform = rotation. concatenated ( affineTransform)
422
435
}
423
436
424
- // Scaling
437
+ /// Applies the specified scaling factor along both x and y axes to the transformation matrix.
425
438
open func scale( by scale: CGFloat ) {
426
439
scaleX ( by: scale, yBy: scale)
427
440
}
428
441
442
+ /// Applies scaling factors to each axis of the transformation matrix.
429
443
open func scaleX( by scaleX: CGFloat , yBy scaleY: CGFloat ) {
430
444
let scale = AffineTransform ( scaleByX: scaleX, byY: scaleY)
431
445
affineTransform = scale. concatenated ( affineTransform)
432
446
}
433
447
434
- // Inverting
448
+ /// Replaces the matrix with its inverse matrix.
435
449
open func invert( ) {
436
450
if let inverse = affineTransform. inverted ( ) {
437
451
affineTransform = inverse
@@ -441,20 +455,22 @@ open class NSAffineTransform : NSObject, NSCopying, NSSecureCoding {
441
455
}
442
456
}
443
457
444
- // Transforming with transform
458
+ /// Appends the specified matrix.
445
459
open func append( _ transform: AffineTransform ) {
446
460
affineTransform = affineTransform. concatenated ( transform)
447
461
}
448
462
463
+ /// Prepends the specified matrix.
449
464
open func prepend( _ transform: AffineTransform ) {
450
465
affineTransform = transform. concatenated ( affineTransform)
451
466
}
452
467
453
- // Transforming points and sizes
468
+ /// Applies the transform to the specified point and returns the result.
454
469
open func transform( _ aPoint: NSPoint ) -> NSPoint {
455
470
return affineTransform. transform ( aPoint)
456
471
}
457
472
473
+ /// Applies the transform to the specified size and returns the result.
458
474
open func transform( _ aSize: NSSize ) -> NSSize {
459
475
return affineTransform. transform ( aSize)
460
476
}
0 commit comments