Skip to content

Commit e5cd76c

Browse files
committed
that took way too long
1 parent 941c5ff commit e5cd76c

File tree

3 files changed

+33
-16
lines changed

3 files changed

+33
-16
lines changed

Sources/SwiftCrossUI/Path.swift

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ public struct AffineTransform: Equatable, CustomDebugStringConvertible {
4949
/// [ x y ]
5050
/// [ z w ]
5151
/// ```
52+
/// - Remark: The matrices in some graphics frameworks, such as WinUI's `Matrix` and
53+
/// CoreGraphics' `CGAffineTransform`, take the transpose of this matrix. The reason for
54+
/// this difference is left- vs right-multiplication; the values are identical.
5255
public var linearTransform: SIMD4<Double>
5356
/// The translation applied after the linear transformation.
5457
public var translation: SIMD2<Double>
@@ -73,14 +76,13 @@ public struct AffineTransform: Equatable, CustomDebugStringConvertible {
7376
}
7477

7578
public static func rotation(radians: Double, center: SIMD2<Double>) -> AffineTransform {
76-
// Making the sine negative so that positive rotations are clockwise
77-
let sine = sin(-radians)
79+
let sine = sin(radians)
7880
let cosine = cos(radians)
7981
return AffineTransform(
8082
linearTransform: SIMD4(x: cosine, y: -sine, z: sine, w: cosine),
8183
translation: SIMD2(
82-
x: -center.x * cosine - center.y * sine + center.x,
83-
y: center.x * sine - center.y * cosine + center.y
84+
x: -center.x * cosine + center.y * sine + center.x,
85+
y: -center.x * sine - center.y * cosine + center.y
8486
)
8587
)
8688
}
@@ -95,7 +97,8 @@ public struct AffineTransform: Equatable, CustomDebugStringConvertible {
9597
)
9698

9799
public func inverted() -> AffineTransform? {
98-
let determinant = linearTransform.x * linearTransform.w - linearTransform.y * linearTransform.z
100+
let determinant =
101+
linearTransform.x * linearTransform.w - linearTransform.y * linearTransform.z
99102
if determinant == 0.0 {
100103
return nil
101104
}

Sources/SwiftCrossUI/Views/Shapes/Shape.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ extension Shape {
4040
let storage = children as! ShapeStorage
4141
let size = size(fitting: proposedSize)
4242

43-
let path = path(in: Path.Rect(x: 0.0, y: 0.0, width: Double(size.x), height: Double(size.y)))
43+
let path = path(
44+
in: Path.Rect(x: 0.0, y: 0.0, width: Double(size.x), height: Double(size.y)))
4445
let pointsChanged = storage.oldPath?.actions != path.actions
4546
storage.oldPath = path
4647

Sources/UIKitBackend/UIKitBackend+Path.swift

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,16 +78,7 @@ extension UIKitBackend {
7878
clockwise: clockwise
7979
)
8080
case .transform(let transform):
81-
path.apply(
82-
CGAffineTransform(
83-
a: transform.linearTransform.x,
84-
b: transform.linearTransform.y,
85-
c: transform.linearTransform.z,
86-
d: transform.linearTransform.w,
87-
tx: transform.translation.x,
88-
ty: transform.translation.y
89-
)
90-
)
81+
path.apply(CGAffineTransform(transform))
9182
}
9283
}
9384
}
@@ -102,3 +93,25 @@ extension UIKitBackend {
10293
container.view.backgroundColor = environment.suggestedForegroundColor.uiColor
10394
}
10495
}
96+
97+
extension CGAffineTransform {
98+
public init(_ transform: AffineTransform) {
99+
self.init(
100+
a: transform.linearTransform.x,
101+
b: transform.linearTransform.z,
102+
c: transform.linearTransform.y,
103+
d: transform.linearTransform.w,
104+
tx: transform.translation.x,
105+
ty: transform.translation.y
106+
)
107+
}
108+
}
109+
110+
extension AffineTransform {
111+
public init(cg transform: CGAffineTransform) {
112+
self.init(
113+
linearTransform: SIMD4(x: transform.a, y: transform.c, z: transform.b, w: transform.d),
114+
translation: SIMD2(x: transform.tx, y: transform.ty)
115+
)
116+
}
117+
}

0 commit comments

Comments
 (0)