Skip to content

Commit 13b0a89

Browse files
committed
AffineTransform: Add documentation
1 parent a047275 commit 13b0a89

File tree

1 file changed

+29
-13
lines changed

1 file changed

+29
-13
lines changed

Foundation/AffineTransform.swift

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,14 @@ public struct AffineTransform : ReferenceConvertible, Hashable, CustomStringConv
3333
public var tX: CGFloat
3434
public var tY: CGFloat
3535

36-
// init() returns affine transformation matrix with identity values.
36+
/// Creates an affine transformation matrix with identity values.
3737
public init() {
3838
self.init(m11: 1.0, m12: 0.0,
3939
m21: 0.0, m22: 1.0,
4040
tX: 0.0, tY: 0.0)
4141
}
4242

43+
/// Creates an affine transformation.
4344
public init(m11: CGFloat, m12: CGFloat, m21: CGFloat, m22: CGFloat, tX: CGFloat, tY: CGFloat) {
4445
(self.m11, self.m12, self.m21, self.m22) = (m11, m12, m21, m22)
4546
(self.tX, self.tY) = (tX, tY)
@@ -185,11 +186,12 @@ public struct AffineTransform : ReferenceConvertible, Hashable, CustomStringConv
185186
)
186187
}
187188

188-
// Scaling
189+
/// Mutates an affine transformation matrix to perform the given scaling in both x and y dimensions.
189190
public mutating func scale(_ scale: CGFloat) {
190191
self.scale(x: scale, y: scale)
191192
}
192193

194+
/// Mutates an affine transformation matrix to perform a scaling in each of the x and y dimensions.
193195
public mutating func scale(x: CGFloat, y: CGFloat) {
194196
m11 = CGFloat(m11.native * x.native)
195197
m12 = CGFloat(m12.native * x.native)
@@ -215,6 +217,7 @@ public struct AffineTransform : ReferenceConvertible, Hashable, CustomStringConv
215217
self = inverse
216218
}
217219

220+
/// Returns an inverted version of the matrix if possible, or nil if not.
218221
public func inverted() -> AffineTransform? {
219222
let determinant = (m11 * m22) - (m12 * m21)
220223
if fabs(determinant.native) <= ε.native {
@@ -230,38 +233,43 @@ public struct AffineTransform : ReferenceConvertible, Hashable, CustomStringConv
230233
return inverse
231234
}
232235

233-
// Transforming with transform
236+
/// Mutates an affine transformation by appending the specified matrix.
234237
public mutating func append(_ transform: AffineTransform) {
235238
self = concatenated(transform)
236239
}
237240

241+
/// Mutates an affine transformation by prepending the specified matrix.
238242
public mutating func prepend(_ transform: AffineTransform) {
239243
self = transform.concatenated(self)
240244
}
241245

242-
// Transforming points and sizes
246+
/// Applies the transform to the specified point and returns the result.
243247
public func transform(_ point: NSPoint) -> NSPoint {
244248
var newPoint = NSPoint()
245249
newPoint.x = (m11 * point.x) + (m21 * point.y) + tX
246250
newPoint.y = (m12 * point.x) + (m22 * point.y) + tY
247251
return newPoint
248252
}
249253

254+
/// Applies the transform to the specified size and returns the result.
250255
public func transform(_ size: NSSize) -> NSSize {
251256
var newSize = NSSize()
252257
newSize.width = (m11 * size.width) + (m21 * size.height)
253258
newSize.height = (m12 * size.width) + (m22 * size.height)
254259
return newSize
255260
}
256261

262+
/// The computed hash value for the transform.
257263
public var hashValue : Int {
258264
return Int((m11 + m12 + m21 + m22 + tX + tY).native)
259265
}
260266

267+
/// A textual description of the transform.
261268
public var description: String {
262269
return "{m11:\(m11), m12:\(m12), m21:\(m21), m22:\(m22), tX:\(tX), tY:\(tY)}"
263270
}
264271

272+
/// A textual description of the transform suitable for debugging.
265273
public var debugDescription: String {
266274
return description
267275
}
@@ -273,6 +281,8 @@ public struct AffineTransform : ReferenceConvertible, Hashable, CustomStringConv
273281
}
274282
}
275283

284+
285+
/// A structure that defines the three-by-three matrix that performs an affine transform between two coordinate systems.
276286
public struct NSAffineTransformStruct {
277287
public var m11: CGFloat
278288
public var m12: CGFloat
@@ -281,7 +291,7 @@ public struct NSAffineTransformStruct {
281291
public var tX: CGFloat
282292
public var tY: CGFloat
283293

284-
// This is NOT an identity matrix
294+
/// Initializes a zero-filled transformation matrix.
285295
public init() {
286296
m11 = 0.0
287297
m12 = 0.0
@@ -291,6 +301,7 @@ public struct NSAffineTransformStruct {
291301
tY = 0.0
292302
}
293303

304+
/// Initializes a transformation matrix with the given values.
294305
public init(m11: CGFloat, m12: CGFloat, m21: CGFloat, m22: CGFloat, tX: CGFloat, tY: CGFloat) {
295306
self.m11 = m11
296307
self.m12 = m12
@@ -305,6 +316,7 @@ open class NSAffineTransform : NSObject, NSCopying, NSSecureCoding {
305316

306317
private var affineTransform: AffineTransform
307318

319+
/// The matrix coefficients stored as the transformation matrix.
308320
public var transformStruct: NSAffineTransformStruct {
309321
get {
310322
return NSAffineTransformStruct(m11: affineTransform.m11,
@@ -389,13 +401,13 @@ open class NSAffineTransform : NSObject, NSCopying, NSSecureCoding {
389401
return true
390402
}
391403

392-
// Initialization
404+
/// Initializes an affine transform matrix using another transform object.
393405
public convenience init(transform: AffineTransform) {
394406
self.init()
395407
affineTransform = transform
396408
}
397409

398-
// init() returns affine transformation matrix with identity values.
410+
/// Initializes an affine transform matrix to the identity matrix.
399411
public override init() {
400412
affineTransform = AffineTransform(
401413
m11: CGFloat(1.0), m12: CGFloat(),
@@ -404,34 +416,36 @@ open class NSAffineTransform : NSObject, NSCopying, NSSecureCoding {
404416
)
405417
}
406418

407-
// Translating
419+
/// Applies the specified translation factors to the transformation matrix.
408420
open func translateX(by deltaX: CGFloat, yBy deltaY: CGFloat) {
409421
let translation = AffineTransform(translationByX: deltaX, byY: deltaY)
410422
affineTransform = translation.concatenated(affineTransform)
411423
}
412424

413-
// Rotating
425+
/// Applies a rotation factor (measured in degrees) to the transformation matrix.
414426
open func rotate(byDegrees angle: CGFloat) {
415427
let rotation = AffineTransform(rotationByDegrees: angle)
416428
affineTransform = rotation.concatenated(affineTransform)
417429
}
418430

431+
/// Applies a rotation factor (measured in radians) to the transformation matrix.
419432
open func rotate(byRadians angle: CGFloat) {
420433
let rotation = AffineTransform(rotationByRadians: angle)
421434
affineTransform = rotation.concatenated(affineTransform)
422435
}
423436

424-
// Scaling
437+
/// Applies the specified scaling factor along both x and y axes to the transformation matrix.
425438
open func scale(by scale: CGFloat) {
426439
scaleX(by: scale, yBy: scale)
427440
}
428441

442+
/// Applies scaling factors to each axis of the transformation matrix.
429443
open func scaleX(by scaleX: CGFloat, yBy scaleY: CGFloat) {
430444
let scale = AffineTransform(scaleByX: scaleX, byY: scaleY)
431445
affineTransform = scale.concatenated(affineTransform)
432446
}
433447

434-
// Inverting
448+
/// Replaces the matrix with its inverse matrix.
435449
open func invert() {
436450
if let inverse = affineTransform.inverted() {
437451
affineTransform = inverse
@@ -441,20 +455,22 @@ open class NSAffineTransform : NSObject, NSCopying, NSSecureCoding {
441455
}
442456
}
443457

444-
// Transforming with transform
458+
/// Appends the specified matrix.
445459
open func append(_ transform: AffineTransform) {
446460
affineTransform = affineTransform.concatenated(transform)
447461
}
448462

463+
/// Prepends the specified matrix.
449464
open func prepend(_ transform: AffineTransform) {
450465
affineTransform = transform.concatenated(affineTransform)
451466
}
452467

453-
// Transforming points and sizes
468+
/// Applies the transform to the specified point and returns the result.
454469
open func transform(_ aPoint: NSPoint) -> NSPoint {
455470
return affineTransform.transform(aPoint)
456471
}
457472

473+
/// Applies the transform to the specified size and returns the result.
458474
open func transform(_ aSize: NSSize) -> NSSize {
459475
return affineTransform.transform(aSize)
460476
}

0 commit comments

Comments
 (0)