Skip to content

Commit b57e653

Browse files
committed
Initial Code Import
1 parent 3c58684 commit b57e653

File tree

3 files changed

+204
-4
lines changed

3 files changed

+204
-4
lines changed

Package.swift

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,17 @@ import PackageDescription
55

66
let package = Package(
77
name: "Geometry",
8+
platforms: [
9+
.iOS(.v16),
10+
.tvOS(.v16),
11+
.macOS(.v13),
12+
],
813
products: [
914
// Products define the executables and libraries a package produces, making them visible to other packages.
1015
.library(
1116
name: "Geometry",
12-
targets: ["Geometry"]),
17+
targets: ["Geometry"]
18+
),
1319
],
1420
targets: [
1521
// Targets are the basic building blocks of a package, defining a module or a test suite.
@@ -18,6 +24,7 @@ let package = Package(
1824
name: "Geometry"),
1925
.testTarget(
2026
name: "GeometryTests",
21-
dependencies: ["Geometry"]),
27+
dependencies: ["Geometry"]
28+
),
2229
]
2330
)
Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
//
2+
// CoreGraphics.swift
3+
// Flair
4+
//
5+
// Created by Mark Onyschuk on 09/02/23.
6+
// Copyright © 2023 Dimension North Inc. All rights reserved.
7+
//
8+
9+
import CoreGraphics
10+
11+
/// A 2D vector description of Core Graphics types
12+
public protocol CGVectorType {
13+
var dx: CGFloat { get set }
14+
var dy: CGFloat { get set }
15+
16+
init(_ dx: CGFloat, _ dy: CGFloat)
17+
}
18+
19+
extension CGVectorType {
20+
/// Vector type conversion
21+
public init(_ other: CGVectorType) {
22+
self.init(other.dx, other.dy)
23+
}
24+
25+
/// Vector length
26+
public var length: CGFloat {
27+
hypot(dx, dy)
28+
}
29+
30+
/// Vector length squared
31+
public var length2: CGFloat {
32+
dx * dx + dy * dy
33+
}
34+
35+
/// Unit representation of the type.
36+
public static var unit: Self {
37+
Self(1, 1)
38+
}
39+
40+
// scalar operators
41+
public static func +(lhs: Self, rhs: CGFloat) -> Self {
42+
Self(lhs.dx + rhs, lhs.dy + rhs)
43+
}
44+
public static func -(lhs: Self, rhs: CGFloat) -> Self {
45+
Self(lhs.dx - rhs, lhs.dy - rhs)
46+
}
47+
public static func *(lhs: Self, rhs: CGFloat) -> Self {
48+
Self(lhs.dx * rhs, lhs.dy * rhs)
49+
}
50+
public static func /(lhs: Self, rhs: CGFloat) -> Self {
51+
Self(lhs.dx / rhs, lhs.dy / rhs)
52+
}
53+
54+
// vector operators
55+
public static func +(lhs: Self, rhs: CGVectorType) -> Self {
56+
Self(lhs.dx + rhs.dx, lhs.dy + rhs.dy)
57+
}
58+
public static func -(lhs: Self, rhs: CGVectorType) -> Self {
59+
Self(lhs.dx - rhs.dx, lhs.dy - rhs.dy)
60+
}
61+
public static func *(lhs: Self, rhs: CGVectorType) -> Self {
62+
Self(lhs.dx * rhs.dx, lhs.dy * rhs.dy)
63+
}
64+
public static func /(lhs: Self, rhs: CGVectorType) -> Self {
65+
Self(lhs.dx / rhs.dx, lhs.dy / rhs.dy)
66+
}
67+
68+
// vector-like tuple operators
69+
public static func +(lhs: Self, rhs: (CGFloat, CGFloat)) -> Self {
70+
Self(lhs.dx + rhs.0, lhs.dy + rhs.1)
71+
}
72+
public static func -(lhs: Self, rhs: (CGFloat, CGFloat)) -> Self {
73+
Self(lhs.dx - rhs.0, lhs.dy - rhs.1)
74+
}
75+
public static func *(lhs: Self, rhs: (CGFloat, CGFloat)) -> Self {
76+
Self(lhs.dx * rhs.0, lhs.dy * rhs.1)
77+
}
78+
public static func /(lhs: Self, rhs: (CGFloat, CGFloat)) -> Self {
79+
Self(lhs.dx / rhs.0, lhs.dy / rhs.1)
80+
}
81+
82+
public static func +(lhs: (CGFloat, CGFloat), rhs: Self) -> Self {
83+
Self(lhs.0 + rhs.dx, lhs.1 + rhs.dy)
84+
}
85+
public static func -(lhs: (CGFloat, CGFloat), rhs: Self) -> Self {
86+
Self(lhs.0 - rhs.dx, lhs.1 - rhs.dy)
87+
}
88+
public static func *(lhs: (CGFloat, CGFloat), rhs: Self) -> Self {
89+
Self(lhs.0 * rhs.dx, lhs.1 * rhs.dy)
90+
}
91+
public static func /(lhs: (CGFloat, CGFloat), rhs: Self) -> Self {
92+
Self(lhs.0 / rhs.dx, lhs.1 / rhs.dy)
93+
}
94+
}
95+
96+
public func +(lhs: (CGFloat, CGFloat), rhs: (CGFloat, CGFloat)) -> (CGFloat, CGFloat) {
97+
(lhs.0 + rhs.0, lhs.1 + rhs.1)
98+
}
99+
public func -(lhs: (CGFloat, CGFloat), rhs: (CGFloat, CGFloat)) -> (CGFloat, CGFloat) {
100+
(lhs.0 - rhs.0, lhs.1 - rhs.1)
101+
}
102+
public func *(lhs: (CGFloat, CGFloat), rhs: (CGFloat, CGFloat)) -> (CGFloat, CGFloat) {
103+
(lhs.0 * rhs.0, lhs.1 * rhs.1)
104+
}
105+
public func /(lhs: (CGFloat, CGFloat), rhs: (CGFloat, CGFloat)) -> (CGFloat, CGFloat) {
106+
(lhs.0 / rhs.0, lhs.1 / rhs.1)
107+
}
108+
109+
110+
extension CGPoint: CGVectorType {
111+
public var dx: CGFloat {
112+
get { x }
113+
set { x = newValue }
114+
}
115+
public var dy: CGFloat {
116+
get { y }
117+
set { y = newValue }
118+
}
119+
120+
public init(_ dx: CGFloat, _ dy: CGFloat) {
121+
self.init(x: dx, y: dy)
122+
}
123+
}
124+
125+
extension CGSize: CGVectorType {
126+
public var dx: CGFloat {
127+
get { width }
128+
set { width = newValue }
129+
}
130+
public var dy: CGFloat {
131+
get { height }
132+
set { height = newValue }
133+
}
134+
135+
public init(_ dx: CGFloat, _ dy: CGFloat) {
136+
self.init(width: dx, height: dy)
137+
}
138+
}
139+
140+
extension CGVector: CGVectorType {
141+
public init(_ dx: CGFloat, _ dy: CGFloat) {
142+
self.init(dx: dx, dy: dy)
143+
}
144+
}
145+
146+
extension CGRect {
147+
public struct Position: Hashable, Codable {
148+
let value: CGFloat
149+
private init(_ value: CGFloat) {
150+
self.value = value
151+
}
152+
153+
public static var min: Self = .init(0)
154+
public static var mid: Self = .init(0.5)
155+
public static var max: Self = .init(1.0)
156+
157+
public static func pos(_ value: CGFloat) -> Self {
158+
.init(value)
159+
}
160+
}
161+
162+
public func at(_ x: Position, _ y: Position) -> CGPoint {
163+
return origin + size * (x.value, y.value)
164+
}
165+
166+
public struct Coord: Hashable, Codable {
167+
public var x, y: Position
168+
169+
public static func at(_ x: Position, _ y: Position) -> Self {
170+
Self(x: x, y: y)
171+
}
172+
}
173+
174+
public func at(_ coord: Coord) -> CGPoint {
175+
return origin + size * (coord.x.value, coord.y.value)
176+
}
177+
178+
public static var unit: Self {
179+
.init(origin: .zero, size: .unit)
180+
}
181+
182+
public func offset(by vector: some CGVectorType) -> Self {
183+
Self(origin: origin + vector, size: size)
184+
}
185+
public func offset(by vector: (CGFloat, CGFloat)) -> Self {
186+
Self(origin: origin + vector, size: size)
187+
}
188+
189+
public func stretchedWide(by amount: CGFloat = 1e12) -> Self {
190+
return self.insetBy(dx: -amount, dy: 0)
191+
}
192+
public func stretchedTall(by amount: CGFloat = 1e12) -> Self {
193+
return self.insetBy(dx: 0, dy: -amount)
194+
}
195+
}

Sources/Geometry/Geometry.swift

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)