def on_key(*args): P = np.random.randint(100,700,(4,2)) C = CubicBezier(*P) plt.cla() plt.ion() cubic_bezier(C.p0,C.p1,C.p2,C.p3) # P = C.flatten_forward_iterative(n=50) # P = np.array(P) # polyline(P, linewidth=100, alpha=.25) # plt.scatter(P[:,0], P[:,1], s=25, # edgecolor='k', facecolor='w', zorder=10, lw=.1) P = C.flatten_iterative(flatness=.125, angle=10) P = np.array(P) polyline(P, linewidth=100, alpha=.1) plt.scatter(P[:,0], P[:,1], s=25, edgecolor='k', facecolor='w', zorder=10, lw=.5) # P = C.flatten_recursive(flatness=.125, angle=10) # P = np.array(P) # polyline(P, linewidth=100, alpha=.1) # plt.scatter(P[:,0], P[:,1], s=25, edgecolor='k', facecolor='w', zorder=10, lw=.5) #A = C.flatten_behdad_arc(0.125) #polyarc(A, linewidth=100, alpha=.1) print "[%d,%d %d,%d %d,%d, %d,%d] : %s" % ( C.p0[0],C.p0[1], C.p1[0],C.p1[1], C.p2[0],C.p2[1], C.p3[0],C.p3[1], cubic_bezier_type( vec2(C.p0[0],C.p0[1]), vec2(C.p1[0],C.p1[1]), vec2(C.p2[0],C.p2[1]), vec2(C.p3[0],C.p3[1]) )) print '%d points generated' % len(P) plt.ioff()
# ------------------------------------------------------------------------------ if __name__ == '__main__': import matplotlib import matplotlib.pyplot as plt from cubic_bezier import CubicBezier p0,p1,p2,p3 = np.random.randint(100,700,(4,2)) C = CubicBezier(p0[0],p0[1],p1[0],p1[1],p2[0],p2[1],p3[0],p3[1]) P = C.flatten_forward_iterative(n=25) print polyline_to_cubic(P, p0, p1, p2, p3, n=100) P = C.flatten_forward_iterative(n=50) print polyline_to_cubic(P, p0, p1, p2, p3, n=100) P = C.flatten_iterative(0.125) print polyline_to_cubic(P, p0, p1, p2, p3, n=100) P = C.flatten_recursive(0.125) print polyline_to_cubic(P, p0, p1, p2, p3, n=100) A = C.flatten_behdad_arc(0.125) print polyarc_to_cubic(A, p0, p1, p2, p3, n=100) # # Check distance to arc # T = np.linspace(0,2*math.pi,100) # for t in T: # x,y = math.cos(t), math.sin(t) # angle = math.atan2(y,x) # angle = math.fmod(angle+2*math.pi,2*math.pi) # print 180*t/math.pi, point_to_arc((x,y), (0,0), 1, (0, math.pi/2))
np.save(filename, E2) else: print "Loading", filename E2 = np.load(filename) # Smart iterative # ------------------------------------- filename = 'smart-iterative.npy' if not os.path.exists(filename): print "Computing", filename E3 = [] for i in range(NTESTS): update_progress(i/float(NTESTS)) p0,p1,p2,p3 = curves[i] C = CubicBezier(p0[0],p0[1],p1[0],p1[1],p2[0],p2[1],p3[0],p3[1]) P = C.flatten_iterative(flatness=flatness, angle=angle) d = distance.polyline_to_cubic(P, p0, p1, p2, p3, n=100) E3.append(d) update_progress(1) E3 = np.array(E3) np.save(filename, E3) else: print "Loading", filename E3 = np.load(filename) # Recursive # ------------------------------------- filename = 'recursive.npy' if not os.path.exists(filename): print "Computing", filename E4 = []
np.save(filename, E2) else: print "Loading", filename E2 = np.load(filename) # Smart iterative # ------------------------------------- filename = 'smart-iterative.npy' if not os.path.exists(filename): print "Computing", filename E3 = [] for i in range(NTESTS): update_progress(i / float(NTESTS)) p0, p1, p2, p3 = curves[i] C = CubicBezier(*curves[i]) P = C.flatten_iterative(flatness=flatness, angle=angle) d = distance.polyline_to_cubic(P, *curves[i], n=100) E3.append(d) update_progress(1) E3 = np.array(E3) np.save(filename, E3) else: print "Loading", filename E3 = np.load(filename) # Recursive # ------------------------------------- filename = 'recursive.npy' if not os.path.exists(filename): print "Computing", filename E4 = []