def _adjusted_speed(start_position, goal_position, position): r = np.array([start_position, goal_position]) clipped_position = np.clip(position, r.min(), r.max()) # Clip keeps the position in the defined range f = interp1d(r, [0,1]) #interpolates a 1D function adj = _easing_derivative(f(clipped_position)) / _easing_derivative(0.5) amp = easeOutSine(abs(goal_position - start_position) / 1023.0) return np.int(MIN_SPEED + (MAX_SPEED - MIN_SPEED) * adj * amp)
def _adjusted_speed(start_position, goal_position, position): r = np.array([start_position, goal_position]) clipped_position = np.clip(position, r.min(), r.max()) f = interp1d(r, [0, 1]) adj = _easing_derivative(f(clipped_position)) / _easing_derivative(0.5) amp = easeOutSine(abs(goal_position - +start_position) / 1023.0) return np.int_(MIN_SPEED + (MAX_SPEED - MIN_SPEED) * adj * amp)
def make_frame(t): #colorDiv = t + 0.01 #color1= [1.0 / colorDiv, 0.0, 0.0] #color2 = [0.0, 1.0 / colorDiv, 0.0] #gradient= gizeh.ColorGradient("linear",((0,(0,.5,1)),(1,(0,1,1))), xy1=(-cosR,-sinR), xy2=(cosR,sinR)) #gradRad1 = radius1 - 20 #gradRad2 = radius1 + 20 #gradient = gizeh.ColorGradient(type="radial", # stops_colors = [(0,color1),(1,color2)], # xy1=[0.0,0.0], xy2=[gradRad1,0.0], xy3 = [0.0,gradRad2]) surface = gizeh.Surface(W,H) # orbit halo #circle1 = gizeh.circle(radius1, xy = (W/2, H/2), stroke=gradient, stroke_width=5) #circle1.draw(surface) for i in range(numParticles): # Orbiting planet particle = particles[i] if (particle.easing == 1): angle = pytweening.linear((duration - t) / duration) * 360 * particle.direction elif (particle.easing == 2): angle = pytweening.easeInQuad((duration - t) / duration) * 360 * particle.direction elif (particle.easing == 3): angle = pytweening.easeOutQuad((duration - t) / duration) * 360 * particle.direction elif (particle.easing == 4): angle = pytweening.easeInOutQuad((duration - t) / duration) * 360 * particle.direction elif (particle.easing == 5): angle = pytweening.easeInSine((duration - t) / duration) * 360 * particle.direction elif (particle.easing == 6): angle = pytweening.easeOutSine((duration - t) / duration) * 360 * particle.direction elif (particle.easing == 7): angle = pytweening.easeInOutSine((duration - t) / duration) * 360 * particle.direction radians = math.radians(angle) cosR = math.cos(radians) sinR = math.sin(radians) x = W/2 + cosR * particle.orbit_radius y = H/2 + sinR * particle.orbit_radius fill = particle.color #circle = gizeh.circle(particle.radius, xy = (x, y), fill=(1,0,1)) circle = gizeh.circle(particle.radius, xy = (x, y), fill=fill) circle.draw(surface) return surface.get_npimage()
def ease_out_sine(n): """ 开始快,结尾慢(正弦函数) """ return pytweening.easeOutSine(n)
def run_pattern(df, target, iters=100000, num_frames=100, decimals=2, shake=0.2, max_temp=0.4, min_temp=0, ramp_in=False, ramp_out=False, freeze_for=0, labels=["X Mean", "Y Mean", "X SD", "Y SD", "Corr."], reset_counts=False, custom_points=False): global frame_count global it_count if reset_counts: it_count = 0 frame_count = 0 r_good = df.copy() # this is a list of frames that we will end up writing to file write_frames = [ int(round(pytweening.linear(x) * iters)) for x in np.arange(0, 1, 1 / (num_frames - freeze_for)) ] if ramp_in and not ramp_out: write_frames = [ int(round(pytweening.easeInSine(x) * iters)) for x in np.arange(0, 1, 1 / (num_frames - freeze_for)) ] elif ramp_out and not ramp_in: write_frames = [ int(round(pytweening.easeOutSine(x) * iters)) for x in np.arange(0, 1, 1 / (num_frames - freeze_for)) ] elif ramp_out and ramp_in: write_frames = [ int(round(pytweening.easeInOutSine(x) * iters)) for x in np.arange(0, 1, 1 / (num_frames - freeze_for)) ] extras = [iters] * freeze_for write_frames.extend(extras) # this gets us the nice progress bars in the notbook, but keeps it from crashing looper = trange if is_kernel(): looper = tnrange # this is the main loop, were we run for many iterations to come up with the pattern for i in looper(iters + 1, leave=True, ascii=True, desc=target + " pattern"): t = (max_temp - min_temp) * s_curve(((iters - i) / iters)) + min_temp if target in all_targets: test_good = perturb(r_good.copy(), initial=df, target=target, temp=t) else: raise Exception("bah, that's not a proper type of pattern") # here we are checking that after the purturbation, that the statistics are still within the allowable bounds if is_error_still_ok(df, test_good, decimals): r_good = test_good # save this chart to the file for x in xrange(write_frames.count(i)): save_scatter_and_results(r_good, target + "-image-" + format(int(frame_count), '05'), 150, labels=labels) #save_scatter(r_good, target + "-image-"+format(int(frame_count), '05'), 150) r_good.to_csv(target + "-data-" + format(int(frame_count), '05') + ".csv") frame_count = frame_count + 1 return r_good
def calculate_next_step(cls, current_step, start_value, change_in_value, number_of_steps): position = LinearEase.calculate_next_step(current_step, start_value, change_in_value, number_of_steps) return pytweening.easeOutSine(position)