Skip to content

Instantly share code, notes, and snippets.

@codelynx
Created February 9, 2017 09:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codelynx/0f88983c0ba3e52776d2adeac77cb4f9 to your computer and use it in GitHub Desktop.
Save codelynx/0f88983c0ba3e52776d2adeac77cb4f9 to your computer and use it in GitHub Desktop.
Calculate quadratic bezier curve length in Swift
func quadraticBezierLength(_ p0: CGPoint, _ p1: CGPoint, _ p2: CGPoint) -> CGFloat {
// cf. http://www.malczak.linuxpl.com/blog/quadratic-bezier-curve-length/
let a = CGPoint(p0.x - 2 * p1.x + p2.x, p0.y - 2 * p1.y + p2.y)
let b = CGPoint(2 * p1.x - 2 * p0.x, 2 * p1.y - 2 * p0.y)
let A = 4 * (a.x * a.x + a.y * a.y)
let B = 4 * (a.x * b.x + a.y * b.y)
let C = b.x * b.x + b.y * b.y
let Sabc = 2 * sqrt(A + B + C)
let A_2 = sqrt(A)
let A_32 = 2 * A * A_2
let C_2 = 2 * sqrt(C)
let BA = B / A_2
let L = (A_32 * Sabc + A_2 * B * (Sabc - C_2) + (4 * C * A - B * B) * log((2 * A_2 + BA + Sabc) / (BA + C_2))) / (4 * A_32)
return L
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment