-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPoint.swift
132 lines (111 loc) · 3.25 KB
/
Point.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
//
// Point.swift
//
// Advent of Code Tools
//
/// A point in a 2d coordinate system.
public struct Point: Hashable, Sendable {
public let x, y: Int
public static let zero = Point(0, 0)
@inlinable
public init(_ x: Int, _ y: Int) {
self.x = x
self.y = y
}
@inlinable
public static func + (_ lhs: Point, _ rhs: Point) -> Point {
Point(lhs.x + rhs.x, lhs.y + rhs.y)
}
@inlinable
public static func + (_ lhs: Point, _ rhs: Direction) -> Point {
Point(lhs.x + rhs.offset.x, lhs.y + rhs.offset.y)
}
@inlinable
public static func += (_ lhs: inout Point, _ rhs: Point) {
lhs = lhs + rhs
}
@inlinable
public static func += (_ lhs: inout Point, _ rhs: Direction) {
lhs = lhs + rhs
}
@inlinable
public static func - (_ lhs: Point, _ rhs: Point) -> Point {
Point(lhs.x - rhs.x, lhs.y - rhs.y)
}
@inlinable
public static func -= (_ lhs: inout Point, _ rhs: Point) {
lhs = lhs - rhs
}
@inlinable
public static func * (_ lhs: Point, _ rhs: Int) -> Point {
Point(lhs.x * rhs, lhs.y * rhs)
}
// manhattan distance
@inlinable
public func distance(to point: Point = .zero) -> Int {
abs(x - point.x) + abs(y - point.y)
}
// aka chess distance
@inlinable
public func chebyshevDistance(to point: Point = .zero) -> Int {
max(abs(x - point.x), abs(y - point.y))
}
}
// MARK: - rotation
extension Point {
public func rotated(by degrees: Int) -> Point {
switch degrees {
case 0, 360: return self
case 90: return Point(-y, x)
case 180: return Point(-x, -y)
case 270: return Point(y, -x)
default: fatalError("invalid angle \(degrees)")
}
}
}
// MARK: - neighbors
extension Point {
public enum Adjacency: Sendable {
case cardinal
case ordinal
case all
public static let orthogonal = Adjacency.cardinal
public static let diagonal = Adjacency.ordinal
}
public func neighbors(adjacency: Adjacency = .cardinal) -> [Point] {
let offsets: [Direction]
switch adjacency {
case .cardinal: offsets = Direction.cardinal
case .ordinal: offsets = Direction.ordinal
case .all: offsets = Direction.allCases
}
return offsets.map { self + $0.offset }
}
@inlinable
public func moved(to direction: Direction, steps: Int = 1) -> Point {
self + direction.offset * steps
}
// return all points between self and `end`. Angle between self and `end` must be a multiple of 45°
public func line(to end: Point) -> [Point] {
let dx = (end.x - x).signum()
let dy = (end.y - y).signum()
let range = max(abs(x - end.x), abs(y - end.y))
return (0 ..< range).map { step in
Point(x + dx * step, y + dy * step)
}
}
}
extension Point: CustomStringConvertible {
public var description: String {
"\(x),\(y)"
}
}
extension Point: Comparable {
/// compare in "reading order" (top to bottom, left to right)
public static func < (lhs: Point, rhs: Point) -> Bool {
if lhs.y != rhs.y {
return lhs.y < rhs.y
}
return lhs.x < rhs.x
}
}