Created
June 22, 2018 11:49
-
-
Save XCarbone/483c25a8926ee09724506a63a6b9d537 to your computer and use it in GitHub Desktop.
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
func isLineStrait(polyline: GMSPath) -> Bool { | |
let pointFrom = CLLocationCoordinate2D(latitude: polyline.coordinate(at: 0).latitude, longitude: polyline.coordinate(at: 0).longitude) | |
let pointTo = CLLocationCoordinate2D(latitude: polyline.coordinate(at: polyline.count() - 1).latitude, longitude: polyline.coordinate(at: polyline.count() - 1).longitude) | |
var equation = (slope: 0.0, intercept: 0.0) | |
equation = getLineEquation(locationFrom: pointFrom, locationTo: pointTo) | |
var numberOfPointNotInThePath = 0 | |
for i in 0...(polyline.count() - 1) { | |
let location = CLLocationCoordinate2D(latitude: polyline.coordinate(at: i).latitude, longitude: polyline.coordinate(at: i).longitude) | |
var distance = ((equation.slope * location.latitude) - location.longitude + equation.intercept) / sqrt((pow(equation.slope, 2) + pow (-1, 2))) | |
distance = distance * 100000 | |
//print("distance = \(distance)") | |
if (distance < 0) { | |
distance = distance * -1 | |
} | |
if (distance >= 7) { | |
numberOfPointNotInThePath += 1 | |
} | |
} | |
if Double(numberOfPointNotInThePath) > (Double(polyline.count()) - 2) / 2 { | |
return false | |
} | |
return true | |
} | |
//line equation with 2 point | |
func getLineEquation(locationFrom: CLLocationCoordinate2D, locationTo: CLLocationCoordinate2D) ->(slope: Double, intercept: Double) { | |
let slope = (locationTo.longitude - locationFrom.longitude) / (locationTo.latitude - locationFrom.latitude) | |
let intercept = locationFrom.longitude - (slope * locationFrom.latitude) | |
return (slope, intercept) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment