Created
January 26, 2015 19:07
-
-
Save Reflejo/f5addfa6408d521a971f to your computer and use it in GitHub Desktop.
Bezier path creation
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 bezierPath(p1: CLLocationCoordinate2D, toPoint p2: CLLocationCoordinate2D, steps: Int = 100) | |
-> [CLLocationCoordinate2D] | |
{ | |
let auxiliaryPoint = self.fetchThirdPointByLocations(p1, p2: p2, angle: 90.0) | |
var targetPoints = [CLLocationCoordinate2D]() | |
let doubleSteps = Double(steps) | |
for index in 0 ... steps { | |
let t = Double(index) / doubleSteps | |
// Start point of the Bezier curve | |
let bezier1x = p1.longitude + (auxiliaryPoint.longitude - p1.longitude) * t | |
let bezier1y = p1.latitude + (auxiliaryPoint.latitude - p1.latitude) * t | |
// End point of the Bezier curve | |
let bezier2x = auxiliaryPoint.longitude + (p2.longitude - auxiliaryPoint.longitude) * t | |
let bezier2y = auxiliaryPoint.latitude + (p2.latitude - auxiliaryPoint.latitude) * t | |
let bezierPoint = CLLocationCoordinate2D(latitude: bezier1y + (bezier2y - bezier1y) * t, | |
longitude: bezier1x + (bezier2x - bezier1x) * t) | |
targetPoints.append(bezierPoint) | |
} | |
return targetPoints | |
} | |
func fetchThirdPointByLocations(p1: CLLocationCoordinate2D, p2: CLLocationCoordinate2D, angle: Double) | |
-> CLLocationCoordinate2D | |
{ | |
let btpAngle = atan2(fabs(p1.latitude - p2.latitude), fabs(p1.longitude - p2.longitude)) * 180 / M_PI | |
let center = CLLocationCoordinate2D(latitude: (p1.latitude + p2.latitude) / 2.0, | |
longitude: (p1.longitude + p2.longitude) / 2.0) | |
let a = (p1.latitude - p2.latitude) * (p1.latitude - p2.latitude) | |
let b = (p1.longitude - p2.longitude) * (p1.longitude - p2.longitude) | |
let distance = sqrt(a + b) | |
let adis = (distance / 2.0) / tan(angle / 2.0 * M_PI / 180) | |
let lng = adis * cos((90 - btpAngle) * M_PI / 180) | |
let lat = adis * sin((90 - btpAngle) * M_PI / 180) | |
return CLLocationCoordinate2D(latitude: center.latitude + lat, longitude: center.longitude + lng) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment