Skip to content

Commit cfb2a87

Browse files
authored
Merge pull request #11347 from phausler/affine_transform_minimal_whitespace
2 parents 0b173de + d2926cf commit cfb2a87

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

Diff for: stdlib/public/SDK/Foundation/AffineTransform.swift

+13-6
Original file line numberDiff line numberDiff line change
@@ -166,12 +166,19 @@ public struct AffineTransform : ReferenceConvertible, Hashable, CustomStringConv
166166
[ 0 0 1 ]
167167
*/
168168
public mutating func rotate(byRadians angle: CGFloat) {
169-
let α = Double(angle)
170-
171-
let sine = CGFloat(sin(α))
172-
let cosine = CGFloat(cos(α))
173-
174-
append(AffineTransform(m11: cosine, m12: sine, m21: -sine, m22: cosine, tX: 0, tY: 0))
169+
let t2 = self
170+
let t1 = AffineTransform(rotationByRadians: angle)
171+
172+
var t = AffineTransform.identity
173+
174+
t.m11 = t1.m11 * t2.m11 + t1.m12 * t2.m21
175+
t.m12 = t1.m11 * t2.m12 + t1.m12 * t2.m22
176+
t.m21 = t1.m21 * t2.m11 + t1.m22 * t2.m21
177+
t.m22 = t1.m21 * t2.m12 + t1.m22 * t2.m22
178+
t.tX = t1.tX * t2.m11 + t1.tY * t2.m21 + t2.tX
179+
t.tY = t1.tX * t2.m12 + t1.tY * t2.m22 + t2.tY
180+
181+
self = t
175182
}
176183

177184
/**

Diff for: test/stdlib/TestAffineTransform.swift

+11
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,16 @@ class TestAffineTransform : TestAffineTransformSuper {
372372
func test_unconditionallyBridgeFromObjectiveC() {
373373
expectEqual(AffineTransform(), AffineTransform._unconditionallyBridgeFromObjectiveC(nil))
374374
}
375+
376+
func test_rotation_compose() {
377+
var t = AffineTransform.identity
378+
t.translate(x: 1.0, y: 1.0)
379+
t.rotate(byDegrees: 90)
380+
t.translate(x: -1.0, y: -1.0)
381+
let result = t.transform(NSPoint(x: 1.0, y: 2.0))
382+
expectEqualWithAccuracy(0.0, Double(result.x), accuracy: accuracyThreshold)
383+
expectEqualWithAccuracy(1.0, Double(result.y), accuracy: accuracyThreshold)
384+
}
375385
}
376386

377387
#if !FOUNDATION_XCTEST
@@ -395,6 +405,7 @@ AffineTransformTests.test("test_hashing_values") { TestAffineTransform().test_ha
395405
AffineTransformTests.test("test_AnyHashableContainingAffineTransform") { TestAffineTransform().test_AnyHashableContainingAffineTransform() }
396406
AffineTransformTests.test("test_AnyHashableCreatedFromNSAffineTransform") { TestAffineTransform().test_AnyHashableCreatedFromNSAffineTransform() }
397407
AffineTransformTests.test("test_unconditionallyBridgeFromObjectiveC") { TestAffineTransform().test_unconditionallyBridgeFromObjectiveC() }
408+
AffineTransformTests.test("test_rotation_compose") { TestAffineTransform().test_rotation_compose() }
398409
runAllTests()
399410
#endif
400411

0 commit comments

Comments
 (0)