-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from lucka-me/fix-improve-performance
Fix issues and improve performance
- Loading branch information
Showing
16 changed files
with
552 additions
and
471 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,7 @@ | ||
.DS_Store | ||
/.build | ||
/Packages | ||
/*.xcodeproj | ||
xcuserdata/ | ||
DerivedData/ | ||
.swiftpm/config/registries.json | ||
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata | ||
.netrc | ||
Package.resolved | ||
|
||
/.vscode | ||
/.vscode | ||
/.swiftpm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Package.swift | ||
*.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
Sources/ingress-drone-explorer/Definitions/Coordinate.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import Foundation | ||
|
||
struct Coordinate { | ||
var lng: Double = 0 | ||
var lat: Double = 0 | ||
} | ||
|
||
extension Coordinate { | ||
init?(_ text: String) { | ||
let components = text.split(separator: ",") | ||
if components.count != 2 { return nil } | ||
guard | ||
let lng = Double(components[0]), | ||
abs(lng) <= 180 | ||
else { | ||
return nil | ||
} | ||
guard | ||
let lat = Double(components[1]), | ||
abs(lat) <= 90 | ||
else { | ||
return nil | ||
} | ||
self.lng = lng | ||
self.lat = lat | ||
} | ||
} | ||
|
||
extension Coordinate: Codable { | ||
init(from decoder: Decoder) throws { | ||
let container = try decoder.container(keyedBy: CodingKeys.self) | ||
lng = try container.decode(Double.self, forKey: .lng) | ||
guard abs(lng) <= 180 else { | ||
throw DecodingError.dataCorruptedError( | ||
forKey: .lng, in: container, debugDescription: "The lng is not in range of [-180, 180]." | ||
) | ||
} | ||
lat = try container.decode(Double.self, forKey: .lat) | ||
guard abs(lat) <= 180 else { | ||
throw DecodingError.dataCorruptedError( | ||
forKey: .lat, in: container, debugDescription: "The lat is not in range of [-90, 90]." | ||
) | ||
} | ||
} | ||
} | ||
|
||
extension Coordinate { | ||
|
||
private static let earthRadius = 6371008.8 | ||
|
||
var theta: Double { | ||
lng * Double.pi / 180.0 | ||
} | ||
|
||
var phi: Double { | ||
lat * Double.pi / 180.0 | ||
} | ||
|
||
func distance(to other: Coordinate) -> Double { | ||
let sinT = sin((other.theta - theta) / 2) | ||
let sinP = sin((other.phi - phi) / 2) | ||
let a = sinP * sinP + sinT * sinT * cos(phi) * cos(other.phi) | ||
return atan2(sqrt(a), sqrt(1 - a)) * 2 * Self.earthRadius | ||
} | ||
|
||
func distance(to segment: (a: Coordinate, b: Coordinate)) -> Double { | ||
let c1 = (segment.b.lat - segment.a.lat) * (lat - segment.a.lat) | ||
+ (segment.b.lng - segment.a.lng) * (lng - segment.a.lng) | ||
if c1 <= 0 { | ||
return distance(to: segment.a) | ||
} | ||
let c2 = (segment.b.lat - segment.a.lat) * (segment.b.lat - segment.a.lat) | ||
+ (segment.b.lng - segment.a.lng) * (segment.b.lng - segment.a.lng) | ||
if c2 <= c1 { | ||
return distance(to: segment.b) | ||
} | ||
let ratio = c1 / c2; | ||
return distance(to: | ||
.init( | ||
lng: segment.a.lng + ratio * (segment.b.lng - segment.a.lng), | ||
lat: segment.a.lat + ratio * (segment.b.lat - segment.a.lat) | ||
) | ||
) | ||
} | ||
|
||
func closer(to a: Coordinate, than b: Coordinate) -> Bool { | ||
let dA = (lng - a.lng) * (lng - a.lng) + (lat - a.lat) * (lat - a.lat) | ||
let dB = (lng - b.lng) * (lng - b.lng) + (lat - b.lat) * (lat - b.lat) | ||
return dA < dB | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
struct Portal : Decodable { | ||
var guid = "" | ||
var title: String? = nil | ||
var coordinate = Coordinate() | ||
} | ||
|
||
extension Portal : Hashable { | ||
static func ==(lhs: Portal, rhs: Portal) -> Bool { | ||
lhs.guid == rhs.guid | ||
} | ||
|
||
func hash(into hasher: inout Hasher) { | ||
hasher.combine(guid) | ||
} | ||
} | ||
|
||
extension Portal { | ||
enum CodingKeys: String, CodingKey { | ||
case guid, title | ||
case coordinate = "lngLat" | ||
} | ||
} |
Oops, something went wrong.