from pylab import * from direct_linear import curve_editing_1d from myarray import * # for a mouse click at (xdata, ydata) # determine the closest curve sample point on the x axis # if the click is outside the plot area, return None def get_index(event): global h,steps if event.xdata != None and event.ydata != None: fixed_index = int(round(event.xdata/h)) fixed_index = max(1, min(steps,fixed_index)) else: fixed_index = None return fixed_index # on mouse button down, enable mouse motion callback # and update the curve def on_press(event): global motion_id, deformed fixed_index = get_index(event) if fixed_index != None and fabs(event.ydata - deformed[fixed_index]) < h: # enable callback for dragging motion_id = connect('motion_notify_event', on_motion) print('press', event.xdata, event.ydata,deformed[fixed_index]) # change fixed point fixed = {fixed_index:event.ydata} # recompute the curve, and update curve data in the plot object deformed = curve_editing_1d(fixed, undeformed, h) plt.set_ydata(deformed) draw() # on mouse button release, desable motion callback def on_release(event): global motion_id disconnect(motion_id) # mouse motion callback: update fixed point position, # recompute the curve and redraw the plot def on_motion(event): global deformed fixed_index = get_index(event) if fixed_index != None: fixed = {fixed_index:event.ydata} deformed = curve_editing_1d(fixed, undeformed, h) plt.set_ydata(deformed) draw() # number of sample points on the curve steps = 100 # initial positions of curve points: all zeros # in this version of the code, only the first and the last 2 are used undeformed = zerosf(steps+1) # step h = 1 # intial position for a fixed point in the center fixed = {steps//2: 1.0} # solve for positions satisfying this constraint deformed = curve_editing_1d(fixed, undeformed, h) # plot returns a list of plot handles, in our case, just one plt = plot(deformed)[0] # initially, no callback for mouse motion motion_id = 0 # callback for mouse button press and release press_id = connect('button_press_event', on_press) release_id = connect('button_release_event', on_release) show()