Skip to content

Commit 5177b15

Browse files
committed
Initial commit
0 parents  commit 5177b15

File tree

13 files changed

+287
-0
lines changed

13 files changed

+287
-0
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Auto detect text files and perform LF normalization
2+
* text=auto

.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
## Xcode User Data
2+
xcuserdata/
3+
4+
## Playgrounds
5+
timeline.xctimeline
6+
playground.xcworkspace
7+
8+
# Swift Package Manager
9+
/.build
10+
/Packages

.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>IDEDidComputeMac32BitWarning</key>
6+
<true/>
7+
</dict>
8+
</plist>

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2019 Julian Dunskus
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

Package.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// swift-tools-version:5.1
2+
3+
import PackageDescription
4+
5+
let package = Package(
6+
name: "CGeometry",
7+
products: [
8+
.library(
9+
name: "CGeometry",
10+
targets: ["CGeometry"]
11+
),
12+
],
13+
dependencies: [],
14+
targets: [
15+
.target(
16+
name: "CGeometry",
17+
dependencies: []
18+
),
19+
.testTarget(
20+
name: "CGeometryTests",
21+
dependencies: ["CGeometry"]
22+
),
23+
]
24+
)

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# CGeometry
2+
3+
A description of this package.

Sources/CGeometry/CGPoint.swift

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import CoreGraphics
2+
3+
public extension CGPoint {
4+
static var zero: Self { Self(x: 0, y: 0) }
5+
6+
// MARK: Point × Vector
7+
8+
static func + (point: Self, offset: CGVector) -> Self {
9+
Self(
10+
x: point.x + offset.dx,
11+
y: point.y + offset.dy
12+
)
13+
}
14+
15+
static func += (point: inout Self, offset: CGVector) {
16+
point.x += offset.dx
17+
point.y += offset.dy
18+
}
19+
20+
static func - (point: Self, offset: CGVector) -> Self {
21+
Self(
22+
x: point.x - offset.dx,
23+
y: point.y - offset.dy
24+
)
25+
}
26+
27+
static func -= (point: inout Self, offset: CGVector) {
28+
point.x -= offset.dx
29+
point.y -= offset.dy
30+
}
31+
}

Sources/CGeometry/CGSize.swift

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import CoreGraphics
2+
3+
public extension CGSize {
4+
static var zero: CGSize { .init(width: 0, height: 0) }
5+
6+
static var one: CGSize { .init(width: 1, height: 1) }
7+
8+
// MARK: Size × Float
9+
10+
static func * (size: Self, scale: CGFloat) -> Self {
11+
Self(
12+
width: size.width * scale,
13+
height: size.height * scale
14+
)
15+
}
16+
17+
static func * (scale: CGFloat, size: Self) -> Self {
18+
Self(
19+
width: size.width * scale,
20+
height: size.height * scale
21+
)
22+
}
23+
24+
static func *= (size: inout Self, scale: CGFloat) {
25+
size.width *= scale
26+
size.height *= scale
27+
}
28+
29+
static func / (size: Self, scale: CGFloat) -> Self {
30+
Self(
31+
width: size.width / scale,
32+
height: size.height / scale
33+
)
34+
}
35+
36+
static func /= (size: inout Self, scale: CGFloat) {
37+
size.width /= scale
38+
size.height /= scale
39+
}
40+
}

Sources/CGeometry/CGVector.swift

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import CoreGraphics
2+
3+
public extension CGVector {
4+
static var zero: Self { Self(dx: 0, dy: 0) }
5+
6+
var length: CGFloat { hypot(dx, dy) }
7+
8+
var angle: CGFloat { atan2(dy, dx) }
9+
10+
init(angle: CGFloat, length: CGFloat) {
11+
self.init(dx: cos(angle) * length, dy: sin(angle) * length)
12+
}
13+
14+
func clamped(to length: CGFloat) -> Self {
15+
let len = self.length
16+
17+
if len > length {
18+
return self * (length / len)
19+
} else {
20+
return self
21+
}
22+
}
23+
24+
static prefix func - (vector: Self) -> Self {
25+
Self(
26+
dx: -vector.dx,
27+
dy: -vector.dy
28+
)
29+
}
30+
31+
// MARK: Vector × Vector
32+
33+
static func + (lhs: Self, rhs: Self) -> Self {
34+
Self(
35+
dx: lhs.dx + rhs.dx,
36+
dy: lhs.dy + rhs.dy
37+
)
38+
}
39+
40+
static func += (lhs: inout Self, rhs: Self) {
41+
lhs.dx += rhs.dx
42+
lhs.dy += rhs.dy
43+
}
44+
45+
static func - (lhs: Self, rhs: Self) -> Self {
46+
Self(
47+
dx: lhs.dx - rhs.dx,
48+
dy: lhs.dy - rhs.dy
49+
)
50+
}
51+
52+
static func -= (lhs: inout Self, rhs: Self) {
53+
lhs.dx -= rhs.dx
54+
lhs.dy -= rhs.dy
55+
}
56+
57+
static func * (lhs: Self, rhs: Self) -> Self {
58+
Self(
59+
dx: lhs.dx * rhs.dx,
60+
dy: lhs.dy * rhs.dy
61+
)
62+
}
63+
64+
static func *= (lhs: inout Self, rhs: Self) {
65+
lhs.dx *= rhs.dx
66+
lhs.dy *= rhs.dy
67+
}
68+
69+
static func / (lhs: Self, rhs: Self) -> Self {
70+
Self(
71+
dx: lhs.dx / rhs.dx,
72+
dy: lhs.dy / rhs.dy
73+
)
74+
}
75+
76+
static func /= (lhs: inout Self, rhs: Self) {
77+
lhs.dx /= rhs.dx
78+
lhs.dy /= rhs.dy
79+
}
80+
81+
// MARK: Vector × Float
82+
83+
static func * (vector: Self, scale: CGFloat) -> Self {
84+
Self(
85+
dx: vector.dx * scale,
86+
dy: vector.dy * scale
87+
)
88+
}
89+
90+
static func * (scale: CGFloat, vector: Self) -> Self {
91+
Self(
92+
dx: vector.dx * scale,
93+
dy: vector.dy * scale
94+
)
95+
}
96+
97+
static func *= (vector: inout Self, scale: CGFloat) {
98+
vector.dx *= scale
99+
vector.dy *= scale
100+
}
101+
102+
static func / (vector: Self, scale: CGFloat) -> Self {
103+
Self(
104+
dx: vector.dx / scale,
105+
dy: vector.dy / scale
106+
)
107+
}
108+
109+
static func /= (vector: inout Self, scale: CGFloat) {
110+
vector.dx /= scale
111+
vector.dy /= scale
112+
}
113+
}

0 commit comments

Comments
 (0)