[misc.bezierTools] Implement cusp loop for calcQuadraticArcLength()

Part of https://github.com/fonttools/fonttools/issues/1142
This commit is contained in:
Behdad Esfahbod 2018-01-08 13:40:11 +00:00
parent fe5a2ea870
commit 5dd888da1d

View File

@ -59,10 +59,10 @@ def calcQuadraticArcLength(pt1, pt2, pt3):
120.21581243984076 120.21581243984076
>>> calcQuadraticArcLength((0, 0), (50, -10), (80, 50)) >>> calcQuadraticArcLength((0, 0), (50, -10), (80, 50))
102.53273816445825 102.53273816445825
>>> calcQuadraticArcLength((0, 0), (40, 0), (-40, 0), True) # collinear points, control point outside, exact result should be 66.6666666666667 >>> calcQuadraticArcLength((0, 0), (40, 0), (-40, 0)) # collinear points, control point outside
69.41755572720999 66.66666666666666
>>> calcQuadraticArcLength((0, 0), (40, 0), (0, 0), True) # collinear points, looping back, exact result should be 40 >>> calcQuadraticArcLength((0, 0), (40, 0), (0, 0)) # collinear points, looping back
34.4265186329548 40.0
""" """
return calcQuadraticArcLengthC(complex(*pt1), complex(*pt2), complex(*pt3)) return calcQuadraticArcLengthC(complex(*pt1), complex(*pt2), complex(*pt3))
@ -81,10 +81,11 @@ def calcQuadraticArcLengthC(pt1, pt2, pt3):
if scale == 0.: if scale == 0.:
return abs(pt3-pt1) return abs(pt3-pt1)
origDist = _dot(n,d0) origDist = _dot(n,d0)
if origDist == 0.: if abs(origDist) < epsilon:
if _dot(d0,d1) >= 0: if _dot(d0,d1) >= 0:
return abs(pt3-pt1) return abs(pt3-pt1)
assert 0 # TODO handle cusps a, b = abs(d0), abs(d1)
return a + b - 2 * (a*b) / (a+b)
x0 = _dot(d,d0) / origDist x0 = _dot(d,d0) / origDist
x1 = _dot(d,d1) / origDist x1 = _dot(d,d1) / origDist
Len = abs(2 * (_intSecAtan(x1) - _intSecAtan(x0)) * origDist / (scale * (x1 - x0))) Len = abs(2 * (_intSecAtan(x1) - _intSecAtan(x0)) * origDist / (scale * (x1 - x0)))