Skip to content

Commit aee746d

Browse files
committed
NSAffineTransform: implemented support for scaling with tests
Now that we have a generic matrix multiplication function, this becomes trivial. I have also verified these tests against the NSAffineTransform in the 10.11 SDK to ensure consistent results (e.g. order of oeprations).
1 parent 2247301 commit aee746d

File tree

2 files changed

+48
-3
lines changed

2 files changed

+48
-3
lines changed

Foundation/NSAffineTransform.swift

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,18 @@ public class NSAffineTransform : NSObject, NSCopying, NSSecureCoding {
5858
public func rotateByRadians(angle: CGFloat) { NSUnimplemented() }
5959

6060
// Scaling
61-
public func scaleBy(scale: CGFloat) { NSUnimplemented() }
62-
public func scaleXBy(scaleX: CGFloat, yBy scaleY: CGFloat) { NSUnimplemented() }
61+
public func scaleBy(scale: CGFloat) {
62+
scaleXBy(scale, yBy: scale)
63+
}
64+
65+
public func scaleXBy(scaleX: CGFloat, yBy scaleY: CGFloat) {
66+
let matrix = transformStruct.matrix3x3
67+
let scaleMatrix = Matrix3x3(scaleX, CGFloat(), CGFloat(),
68+
CGFloat(), scaleY, CGFloat(),
69+
CGFloat(), CGFloat(), CGFloat(1.0))
70+
let product = multiplyMatrix3x3(matrix, byMatrix3x3: scaleMatrix)
71+
transformStruct = NSAffineTransformStruct(matrix: product)
72+
}
6373

6474
// Inverting
6575
public func invert() { NSUnimplemented() }

TestFoundation/TestNSAffineTransform.swift

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ class TestNSAffineTransform : XCTestCase {
3131
("test_BasicConstruction", test_BasicConstruction),
3232
("test_IdentityTransformation", test_IdentityTransformation),
3333
("test_Translation", test_Translation),
34-
("test_TranslationComposed", test_TranslationComposed)
34+
("test_TranslationComposed", test_TranslationComposed),
35+
("test_Scaling", test_Scaling),
36+
("test_TranslationScaling", test_TranslationScaling),
37+
("test_ScalingTranslation", test_ScalingTranslation)
3538
]
3639
}
3740

@@ -94,4 +97,36 @@ class TestNSAffineTransform : XCTestCase {
9497
checkPointTransformation(xyPlus5, point: NSMakePoint(CGFloat(-2.0), CGFloat(-3.0)),
9598
expectedPoint: NSMakePoint(CGFloat(3.0), CGFloat(2.0)))
9699
}
100+
101+
func test_Scaling() {
102+
let xyTimes5 = NSAffineTransform()
103+
xyTimes5.scaleBy(CGFloat(5.0))
104+
105+
checkPointTransformation(xyTimes5, point: NSMakePoint(CGFloat(-2.0), CGFloat(3.0)),
106+
expectedPoint: NSMakePoint(CGFloat(-10.0), CGFloat(15.0)))
107+
108+
let xTimes2YTimes3 = NSAffineTransform()
109+
xTimes2YTimes3.scaleXBy(CGFloat(2.0), yBy: CGFloat(-3.0))
110+
111+
checkPointTransformation(xTimes2YTimes3, point: NSMakePoint(CGFloat(-1.0), CGFloat(3.5)),
112+
expectedPoint: NSMakePoint(CGFloat(-2.0), CGFloat(-10.5)))
113+
}
114+
115+
func test_TranslationScaling() {
116+
let xPlus2XYTimes5 = NSAffineTransform()
117+
xPlus2XYTimes5.translateXBy(CGFloat(2.0), yBy: CGFloat())
118+
xPlus2XYTimes5.scaleXBy(CGFloat(5.0), yBy: CGFloat(-5.0))
119+
120+
checkPointTransformation(xPlus2XYTimes5, point: NSMakePoint(CGFloat(1.0), CGFloat(2.0)),
121+
expectedPoint: NSMakePoint(CGFloat(7.0), CGFloat(-10.0)))
122+
}
123+
124+
func test_ScalingTranslation() {
125+
let xyTimes5XPlus3 = NSAffineTransform()
126+
xyTimes5XPlus3.scaleBy(CGFloat(5.0))
127+
xyTimes5XPlus3.translateXBy(CGFloat(3.0), yBy: CGFloat())
128+
129+
checkPointTransformation(xyTimes5XPlus3, point: NSMakePoint(CGFloat(1.0), CGFloat(2.0)),
130+
expectedPoint: NSMakePoint(CGFloat(20.0), CGFloat(10.0)))
131+
}
97132
}

0 commit comments

Comments
 (0)