Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions Sources/OpenCoreGraphics/CGAffineTransform.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,15 @@ extension CGAffineTransform {
}

public func translatedBy(x tx: CGFloat, y ty: CGFloat) -> CGAffineTransform {
let t = CGAffineTransform(a: 1, b: 0, c: 0, d: 1, tx: Double(tx), ty: Double(ty))
return concatenating(t)
CGAffineTransform(a: 1, b: 0, c: 0, d: 1, tx: Double(tx), ty: Double(ty)).concatenating(self)
}

public func scaledBy(x sx: CGFloat, y sy: CGFloat) -> CGAffineTransform {
let s = CGAffineTransform(a: Double(sx), b: 0, c: 0, d: Double(sy), tx: 0, ty: 0)
return concatenating(s)
CGAffineTransform(a: Double(sx), b: 0, c: 0, d: Double(sy), tx: 0, ty: 0).concatenating(self)
}

public func rotated(by angle: CGFloat) -> CGAffineTransform {
return concatenating(CGAffineTransform(rotationAngle: angle))
CGAffineTransform(rotationAngle: angle).concatenating(self)
}

public func inverted() -> CGAffineTransform {
Expand Down
47 changes: 47 additions & 0 deletions Tests/OpenCoreGraphicsShimsTests/CGAffineTransformTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
import Testing
import OpenCoreGraphicsShims
import Numerics
#if OPENCOREGRAPHICS_COREGRAPHICS
import struct CoreGraphics.CGAffineTransform
#else
import struct OpenCoreGraphics.CGAffineTransform
#endif

@Suite
struct CGAffineTransformTests {
Expand Down Expand Up @@ -92,4 +97,46 @@ struct CGAffineTransformTests {
#expect(inv.tx.isApproximatelyEqual(to: singular.tx))
#expect(inv.ty.isApproximatelyEqual(to: singular.ty))
}

// MARK: - TranslateBy

@Test
func translatedBy() {
let t1 = CGAffineTransform(a: 1, b: 2, c: 3, d: 4, tx: 5, ty: 6)
let t2 = t1.translatedBy(x: 7, y: 8)
#expect(t2.a.isApproximatelyEqual(to: 1.0))
#expect(t2.b.isApproximatelyEqual(to: 2.0))
#expect(t2.c.isApproximatelyEqual(to: 3.0))
#expect(t2.d.isApproximatelyEqual(to: 4.0))
#expect(t2.tx.isApproximatelyEqual(to: 36.0))
#expect(t2.ty.isApproximatelyEqual(to: 52.0))
}

// MARK: - ScaledBy

@Test
func scaledBy() {
let t1 = CGAffineTransform(a: 1, b: 2, c: 3, d: 4, tx: 5, ty: 6)
let t2 = t1.scaledBy(x: 7, y: 8)
#expect(t2.a.isApproximatelyEqual(to: 7.0))
#expect(t2.b.isApproximatelyEqual(to: 14.0))
#expect(t2.c.isApproximatelyEqual(to: 24.0))
#expect(t2.d.isApproximatelyEqual(to: 32.0))
#expect(t2.tx.isApproximatelyEqual(to: 5.0))
#expect(t2.ty.isApproximatelyEqual(to: 6.0))
}

// MARK: - Rotated

@Test
func rotated() {
let t1 = CGAffineTransform(a: 1, b: 2, c: 3, d: 4, tx: 5, ty: 6)
let t2 = t1.rotated(by: .pi / 2)
#expect(t2.a.isApproximatelyEqual(to: 3.0))
#expect(t2.b.isApproximatelyEqual(to: 4.0))
#expect(t2.c.isApproximatelyEqual(to: -1.0))
#expect(t2.d.isApproximatelyEqual(to: -2.0))
#expect(t2.tx.isApproximatelyEqual(to: 5.0))
#expect(t2.ty.isApproximatelyEqual(to: 6.0))
}
}