def newtonRaphsonRootFind(bez, point, u): """ Newton's root finding algorithm calculates f(x)=0 by reiterating x_n+1 = x_n - f(x_n)/f'(x_n) We are trying to find curve parameter u for some point p that minimizes the distance from that point to the curve. Distance point to curve is d=q(u)-p. At minimum distance the point is perpendicular to the curve. We are solving f = q(u)-p * q'(u) = 0 with f' = q'(u) * q'(u) + q(u)-p * q''(u) gives u_n+1 = u_n - |q(u_n)-p * q'(u_n)| / |q'(u_n)**2 + q(u_n)-p * q''(u_n)| """ d = bezier.q(bez, u) - point numerator = (d * bezier.qprime(bez, u)).sum() denominator = (bezier.qprime(bez, u)**2 + d * bezier.qprimeprime(bez, u)).sum() if denominator == 0.0: return u else: return u - numerator / denominator
def newtonRaphsonRootFind(bez, point, u): """ Newton's root finding algorithm calculates f(x)=0 by reiterating x_n+1 = x_n - f(x_n)/f'(x_n) We are trying to find curve parameter u for some point p that minimizes the distance from that point to the curve. Distance point to curve is d=q(u)-p. At minimum distance the point is perpendicular to the curve. We are solving f = q(u)-p * q'(u) = 0 with f' = q'(u) * q'(u) + q(u)-p * q''(u) gives u_n+1 = u_n - |q(u_n)-p * q'(u_n)| / |q'(u_n)**2 + q(u_n)-p * q''(u_n)| """ d = bezier.q(bez, u)-point numerator = (d * bezier.qprime(bez, u)).sum() denominator = (bezier.qprime(bez, u)**2 + d * bezier.qprimeprime(bez, u)).sum() if denominator == 0.0: return u else: return u - numerator/denominator