Skip to content

Commit

Permalink
Get bearing improvements ready for merge
Browse files Browse the repository at this point in the history
* Fix Swiftlint violations
* Add a couple simple tests
  • Loading branch information
aaronbrethorst committed Jul 7, 2018
1 parent f089593 commit e133a60
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
12 changes: 6 additions & 6 deletions ARCL/Source/CLLocation+Extensions.swift
Expand Up @@ -78,30 +78,30 @@ public extension CLLocation {
}

public extension CLLocationCoordinate2D {
public func coordinateWithBearing(bearing:Double, distanceMeters:Double) -> CLLocationCoordinate2D {
public func coordinateWithBearing(bearing: Double, distanceMeters: Double) -> CLLocationCoordinate2D {
// formula by http://www.movable-type.co.uk/scripts/latlong.html
let lat1 = self.latitude * Double.pi / 180
let lon1 = self.longitude * Double.pi / 180

let distance = distanceMeters / EARTH_RADIUS
let angularBearing = bearing * Double.pi / 180

var lat2 = lat1 + distance * cos(angularBearing)
let dLat = lat2 - lat1
let dPhi = log(tan(lat2 / 2 + Double.pi/4) / tan(lat1 / 2 + Double.pi/4))
let q = (dPhi != 0) ? dLat/dPhi : cos(lat1) // E-W line gives dPhi=0
let dLon = distance * sin(angularBearing) / q

// check for some daft bugger going past the pole
if (fabs(lat2) > Double.pi/2) {
if fabs(lat2) > Double.pi/2 {
lat2 = lat2 > 0 ? Double.pi - lat2 : -(Double.pi - lat2)
}
var lon2 = lon1 + dLon + 3 * Double.pi
while (lon2 > 2 * Double.pi) {
while lon2 > 2 * Double.pi {
lon2 -= 2 * Double.pi
}
lon2 -= Double.pi

return CLLocationCoordinate2D(latitude: lat2 * 180 / Double.pi, longitude: lon2 * 180 / Double.pi)
}
}
2 changes: 1 addition & 1 deletion ARCL/Source/SceneLocationView.swift
Expand Up @@ -295,7 +295,7 @@ public class SceneLocationView: ARSCNView, ARSCNViewDelegate {
node.removeFromParentNode()
}
}

/// Determine if scene contains a node with the specified tag
///
/// - Parameter tag: tag text
Expand Down
18 changes: 9 additions & 9 deletions ARCLTests/ARCLTests.swift
Expand Up @@ -7,6 +7,8 @@
//

import XCTest
import CoreLocation

@testable import ARCL

class ARCLTests: XCTestCase {
Expand All @@ -21,15 +23,13 @@ class ARCLTests: XCTestCase {
super.tearDown()
}

func testExample() {
// This is an example of a functional test case.
// Use XCTAssert and related functions to verify your tests produce the correct results.
}
func testBearing() {
let pub = CLLocationCoordinate2D(latitude: 47.6235858, longitude: -122.3128663)

let north = pub.coordinateWithBearing(bearing: 0, distanceMeters: 500)
XCTAssertEqual(pub.latitude, north.latitude, accuracy: 0.01)

func testPerformanceExample() {
// This is an example of a performance test case.
self.measure {
// Put the code you want to measure the time of here.
}
let east = pub.coordinateWithBearing(bearing: 90, distanceMeters: 500)
XCTAssertEqual(pub.longitude, east.longitude, accuracy: 0.01)
}
}

0 comments on commit e133a60

Please sign in to comment.