def measure_bz_cloth(seg, bz, n = 100): bz_arclen = tocubic.bz_arclength_rk4(bz) arclen_ratio = seg.arclen / bz_arclen dbz = tocubic.bz_deriv(bz) def measure_derivs(x, ys): dx, dy = tocubic.bz_eval(dbz, x) ds = hypot(dx, dy) s = ys[0] * arclen_ratio dscore = ds * (tocubic.mod_2pi(atan2(dy, dx) - seg.th(s)) ** 2) #print s, atan2(dy, dx), seg.th(s) return [ds, dscore] dt = 1./n t = 0 ys = [0, 0] for i in range(n): dydx = measure_derivs(t, ys) tocubic.rk4(ys, dydx, t, dt, measure_derivs) t += dt return ys[1]
def plot_k_of_bz(bz): dbz = tocubic.bz_deriv(bz) ddbz = tocubic.bz_deriv(dbz) cmd = 'moveto' ss = [0] def arclength_deriv(x, ss): dx, dy = tocubic.bz_eval(dbz, x) return [hypot(dx, dy)] dt = 0.01 t = 0 for i in range(101): dx, dy = tocubic.bz_eval(dbz, t) ddx, ddy = tocubic.bz_eval(ddbz, t) k = (ddy * dx - dy * ddx) / (dx * dx + dy * dy) ** 1.5 print 100 + 500 * ss[0], 100 + 200 * k, cmd cmd = 'lineto' dsdx = arclength_deriv(t, ss) tocubic.rk4(ss, dsdx, t, .01, arclength_deriv) t += dt print 'stroke'
def measure_bz_cloth(seg, bz, n=100): bz_arclen = tocubic.bz_arclength_rk4(bz) arclen_ratio = seg.arclen / bz_arclen dbz = tocubic.bz_deriv(bz) def measure_derivs(x, ys): dx, dy = tocubic.bz_eval(dbz, x) ds = hypot(dx, dy) s = ys[0] * arclen_ratio dscore = ds * (tocubic.mod_2pi(atan2(dy, dx) - seg.th(s))**2) #print s, atan2(dy, dx), seg.th(s) return [ds, dscore] dt = 1. / n t = 0 ys = [0, 0] for i in range(n): dydx = measure_derivs(t, ys) tocubic.rk4(ys, dydx, t, dt, measure_derivs) t += dt return ys[1]
def plot_k_of_bz(bz): dbz = tocubic.bz_deriv(bz) ddbz = tocubic.bz_deriv(dbz) cmd = 'moveto' ss = [0] def arclength_deriv(x, ss): dx, dy = tocubic.bz_eval(dbz, x) return [hypot(dx, dy)] dt = 0.01 t = 0 for i in range(101): dx, dy = tocubic.bz_eval(dbz, t) ddx, ddy = tocubic.bz_eval(ddbz, t) k = (ddy * dx - dy * ddx) / (dx * dx + dy * dy)**1.5 print(100 + 500 * ss[0], 100 + 200 * k, cmd) cmd = 'lineto' dsdx = arclength_deriv(t, ss) tocubic.rk4(ss, dsdx, t, .01, arclength_deriv) t += dt print('stroke')