-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPoint3.swift
194 lines (169 loc) · 4.97 KB
/
Point3.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
//
// Point3.swift
//
// Advent of Code Tools
//
/// A point in a 3d coordinate system.
public struct Point3: Hashable, Sendable {
public let x, y, z: Int
public static let zero = Point3(0, 0, 0)
@inlinable
public init(_ x: Int, _ y: Int, _ z: Int) {
self.x = x
self.y = y
self.z = z
}
@inlinable
public static func + (_ lhs: Point3, _ rhs: Point3) -> Point3 {
Point3(
lhs.x + rhs.x,
lhs.y + rhs.y,
lhs.z + rhs.z
)
}
@inlinable
public static func += (_ lhs: inout Point3, _ rhs: Point3) {
lhs = lhs + rhs
}
@inlinable
public static func - (_ lhs: Point3, _ rhs: Point3) -> Point3 {
Point3(
lhs.x - rhs.x,
lhs.y - rhs.y,
lhs.z - rhs.z
)
}
@inlinable
public static func -= (_ lhs: inout Point3, _ rhs: Point3) {
lhs = lhs - rhs
}
@inlinable
public static func * (_ lhs: Point3, _ rhs: Int) -> Point3 {
Point3(
lhs.x * rhs,
lhs.y * rhs,
lhs.z * rhs
)
}
// manhattan distance
@inlinable
public func distance(to point: Point3 = .zero) -> Int {
abs(x - point.x) + abs(y - point.y) + abs(z - point.z)
}
// aka chess distance
@inlinable
public func chebyshevDistance(to point: Point3 = .zero) -> Int {
max(abs(x - point.x), abs(y - point.y), abs(z - point.z))
}
}
// MARK: - rotation
extension Point3 {
public func rotateX(_ degrees: Int) -> Point3 {
/*
|1 0 0| |x| | x | |x'|
|0 cos θ −sin θ| |y| = |y cos θ − z sin θ| = |y'|
|0 sin θ cos θ| |z| |y sin θ + z cos θ| |z'|
*/
let xNew = x
let yNew = y * cos(degrees) - z * sin(degrees)
let zNew = y * sin(degrees) + z * cos(degrees)
return Point3(xNew, yNew, zNew)
}
public func rotateY(_ degrees: Int) -> Point3 {
/*
| cos θ 0 sin θ| |x| | x cos θ + z sin θ| |x'|
| 0 1 0| |y| = | y | = |y'|
|−sin θ 0 cos θ| |z| |−x sin θ + z cos θ| |z'|
*/
let xNew = x * cos(degrees) + z * sin(degrees)
let yNew = y
let zNew = -x * sin(degrees) + z * cos(degrees)
return Point3(xNew, yNew, zNew)
}
public func rotateZ(_ degrees: Int) -> Point3 {
/*
|cos θ −sin θ 0| |x| |x cos θ − y sin θ| |x'|
|sin θ cos θ 0| |y| = |x sin θ + y cos θ| = |y'|
| 0 0 1| |z| | z | |z'|
*/
let xNew = x * cos(degrees) - y * sin(degrees)
let yNew = x * sin(degrees) + y * cos(degrees)
let zNew = z
return Point3(xNew, yNew, zNew)
}
private func sin(_ degrees: Int) -> Int {
switch degrees {
case 0: return 0
case 90: return 1
case 180: return 0
case 270: return -1
default: fatalError("invalid angle \(degrees)")
}
}
private func cos(_ degrees: Int) -> Int {
switch degrees {
case 0: return 1
case 90: return 0
case 180: return -1
case 270: return 0
default: fatalError("invalid angle \(degrees)")
}
}
}
// MARK: - neighbors
extension Point3 {
public enum Adjacency: Sendable {
case cardinal
case ordinal
case all
public static let orthogonal = Adjacency.cardinal
public static let diagonal = Adjacency.ordinal
}
// swiftlint:disable comma
private static let cardinalOffsets = [
Point3( 1, 0, 0),
Point3(-1, 0, 0),
Point3( 0, 1, 0),
Point3( 0, -1, 0),
Point3( 0, 0, 1),
Point3( 0, 0, -1)
]
private static let ordinalOffsets = [
Point3( 1, 0, 1),
Point3( 1, 0, -1),
Point3( 1, 1, 0),
Point3( 1, 1, 1),
Point3( 1, 1, -1),
Point3( 1, -1, 0),
Point3( 1, -1, 1),
Point3( 1, -1, -1),
Point3(-1, 0, 1),
Point3(-1, 0, -1),
Point3(-1, 1, 0),
Point3(-1, 1, 1),
Point3(-1, 1, -1),
Point3(-1, -1, 0),
Point3(-1, -1, 1),
Point3(-1, -1, -1),
Point3( 0, 1, 1),
Point3( 0, 1, -1),
Point3( 0, -1, 1),
Point3( 0, -1, -1)
]
// swiftlint:enable comma
private static let allOffsets = Self.cardinalOffsets + Self.ordinalOffsets
public func neighbors(adjacency: Adjacency = .cardinal) -> [Point3] {
let offsets: [Point3]
switch adjacency {
case .cardinal: offsets = Self.cardinalOffsets
case .ordinal: offsets = Self.ordinalOffsets
case .all: offsets = Self.allOffsets
}
return offsets.map { self + $0 }
}
}
extension Point3: CustomStringConvertible {
public var description: String {
"\(x),\(y),\(z)"
}
}