Skip to content

Commit 7327ee4

Browse files
committed
🎨 Improve code style
1 parent 6af53ab commit 7327ee4

File tree

4 files changed

+34
-28
lines changed

4 files changed

+34
-28
lines changed

‎Sources/GeoModels/2D/BoundingBox2D.swift

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -107,16 +107,18 @@ public struct BoundingBox2D: Hashable {
107107
Coordinate2D(latitude: latitude, longitude: northEast.longitude)
108108
}
109109

110-
public func offsetBy(dLat: Latitude = 0, dLong: Longitude = 0) -> BoundingBox2D {
111-
BoundingBox2D(
110+
public func offsetBy(dLat: Latitude = .zero, dLong: Longitude = .zero) -> BoundingBox2D {
111+
Self.init(
112112
southWest: southWest.offsetBy(dLat: dLat, dLong: dLong),
113-
northEast: northEast.offsetBy(dLat: dLat, dLong: dLong)
113+
width: width,
114+
height: height
114115
)
115116
}
116-
public func offsetBy(dx: Longitude = 0, dy: Latitude = 0) -> BoundingBox2D {
117-
BoundingBox2D(
117+
public func offsetBy(dx: Coordinate2D.X = .zero, dy: Coordinate2D.Y = .zero) -> BoundingBox2D {
118+
Self.init(
118119
southWest: southWest.offsetBy(dx: dx, dy: dy),
119-
northEast: northEast.offsetBy(dx: dx, dy: dy)
120+
width: width,
121+
height: height
120122
)
121123
}
122124

@@ -125,19 +127,19 @@ public struct BoundingBox2D: Hashable {
125127
extension BoundingBox2D: BoundingBox {
126128

127129
public static var zero: BoundingBox2D {
128-
BoundingBox2D(southWest: .zero, width: .zero, height: .zero)
130+
Self.init(southWest: .zero, width: .zero, height: .zero)
129131
}
130132

131133
/// The union of bounding boxes gives a new bounding box that encloses the given two.
132134
public func union(_ other: BoundingBox2D) -> BoundingBox2D {
133-
BoundingBox2D(
135+
Self.init(
134136
southWest: Coordinate2D(
135-
latitude: min(self.southWest.latitude, other.southWest.latitude),
136-
longitude: min(self.southWest.longitude, other.southWest.longitude)
137+
latitude: min(self.southLatitude, other.southLatitude),
138+
longitude: min(self.westLongitude, other.westLongitude)
137139
),
138140
northEast: Coordinate2D(
139-
latitude: max(self.northEast.latitude, other.northEast.latitude),
140-
longitude: max(self.northEast.longitude, other.northEast.longitude)
141+
latitude: max(self.northLatitude, other.northLatitude),
142+
longitude: max(self.eastLongitude, other.eastLongitude)
141143
)
142144
)
143145
}

‎Sources/GeoModels/2D/Coordinate2D.swift

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,37 @@
88

99
public struct Coordinate2D: Hashable {
1010

11+
public typealias X = Longitude
12+
public typealias Y = Latitude
13+
1114
public static var zero: Coordinate2D {
12-
Coordinate2D(latitude: 0, longitude: 0)
15+
Self.init(latitude: .zero, longitude: .zero)
1316
}
1417

1518
public var latitude: Latitude
1619
public var longitude: Longitude
1720

18-
public var x: Longitude { longitude }
19-
public var y: Latitude { latitude }
21+
public var x: X { longitude }
22+
public var y: Y { latitude }
2023

2124
public var withPositiveLongitude: Coordinate2D {
22-
Coordinate2D(latitude: latitude, longitude: longitude.positive)
25+
Self.init(latitude: latitude, longitude: longitude.positive)
2326
}
2427

25-
public init(
26-
latitude: Latitude,
27-
longitude: Longitude
28-
) {
28+
public init(latitude: Latitude, longitude: Longitude) {
2929
self.latitude = latitude
3030
self.longitude = longitude
3131
}
3232

33+
public init(x: X, y: Y) {
34+
self.init(latitude: y, longitude: x)
35+
}
36+
3337
public func offsetBy(dLat: Latitude = 0, dLong: Longitude = 0) -> Coordinate2D {
34-
Coordinate2D(latitude: latitude + dLat, longitude: longitude + dLong)
38+
Self.init(latitude: latitude + dLat, longitude: longitude + dLong)
3539
}
36-
public func offsetBy(dx: Longitude = 0, dy: Latitude = 0) -> Coordinate2D {
37-
Coordinate2D(latitude: latitude + dy, longitude: longitude + dx)
40+
public func offsetBy(dx: X = .zero, dy: Y = .zero) -> Coordinate2D {
41+
Self.init(x: x + dx, y: y + dy)
3842
}
3943

4044
}
@@ -52,7 +56,7 @@ extension Coordinate2D {
5256
}
5357

5458
public prefix static func - (value: Coordinate2D) -> Coordinate2D {
55-
return Coordinate2D(latitude: -value.latitude, longitude: -value.longitude)
59+
return Self.init(latitude: -value.latitude, longitude: -value.longitude)
5660
}
5761

5862
}

‎Sources/Turf/Boundable.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ public protocol Boundable {
1919
extension Coordinate2D: Boundable {
2020

2121
public var bbox: BoundingBox2D {
22-
BoundingBox2D(southWest: self, width: 0, height: 0)
22+
BoundingBox2D(southWest: self, width: .zero, height: .zero)
2323
}
2424

2525
}
2626

2727
extension Coordinate3D: Boundable {
2828

2929
public var bbox: BoundingBox3D {
30-
BoundingBox3D(twoDimensions.bbox, lowAltitude: altitude, zHeight: .zero)
30+
BoundingBox3D(southWestLow: self, width: .zero, height: .zero, zHeight: .zero)
3131
}
3232

3333
}

‎Sources/Turf/Turf.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ public func centerOfMass(for coords: [Coordinate2D]) -> Coordinate2D {
135135
///
136136
/// Ported from <https://github.com/Turfjs/turf/blob/84110709afda447a686ccdf55724af6ca755c1f8/packages/turf-centroid/index.ts#L21-L40>
137137
public func centroid(for coordinates: [Coordinate2D]) -> Coordinate2D {
138-
var sumLongitude: Longitude = 0.0
139-
var sumLatitude: Latitude = 0.0
138+
var sumLongitude: Longitude = .zero
139+
var sumLatitude: Latitude = .zero
140140

141141
for coordinate in coordinates {
142142
sumLongitude += coordinate.longitude

0 commit comments

Comments
 (0)