| # https://math.stackexchange.com/questions/2493537/smooth-transition-between-linear-functions | |
| class oscdot4(Scene): | |
| def construct(self): | |
| def f1(x): | |
| return 0 + 0.5*x*2*PI | |
| def f2(x): | |
| return f1(3) + 2.5*(x-3)*2*PI | |
| x0 = 2 | |
| x1 = 4 | |
| def g(x): | |
| y0 = f1(x0) | |
| y1 = f2(x1) | |
| p0 = f1(1)-f1(0) | |
| p1 = f2(1)-f2(0) | |
| return ( | |
| f1(x) if x<x0 | |
| else | |
| y0*(x-x1)**2/(x1-x0)**3*(2*x-3*x0+x1) - y1*(x-x0)**2/(x1-x0)**3*(2*x+x0-3*x1)+p0*(x-x1)**2/(x1-x0)**2*(x-x0)+p1*(x-x0)**2/(x1-x0)**2*(x-x1) | |
| if x<x1 | |
| else | |
| f2(x) | |
| ) | |
| ax = Axes( | |
| x_range=[0,10,1], | |
| y_range=[0,80,5], | |
| tips=False, | |
| ).add_coordinates() | |
| self.add(ax) | |
| p1 = ax.plot(f1) | |
| p2 = ax.plot(f2) | |
| p3 = ax.plot(g) | |
| self.add(p1,p2,p3) | |
| class oscdot5(Scene): | |
| def construct(self): | |
| def f1(x): | |
| return 0 + 0.5*x*2*PI | |
| def f2(x): | |
| return f1(3) + 2.5*(x-3)*2*PI | |
| x0 = 2 | |
| x1 = 4 | |
| def g(x): | |
| y0 = f1(x0) | |
| y1 = f2(x1) | |
| p0 = f1(1)-f1(0) | |
| p1 = f2(1)-f2(0) | |
| return ( | |
| f1(x) if x<x0 | |
| else | |
| y0*(x-x1)**2/(x1-x0)**3*(2*x-3*x0+x1) - y1*(x-x0)**2/(x1-x0)**3*(2*x+x0-3*x1)+p0*(x-x1)**2/(x1-x0)**2*(x-x0)+p1*(x-x0)**2/(x1-x0)**2*(x-x1) | |
| if x<x1 | |
| else | |
| f2(x) | |
| ) | |
| time_tracker = ValueTracker(0) | |
| particle = Dot() | |
| def p_upd(mobj): | |
| mobj.move_to([0.5 * np.cos(g(time_tracker.get_value())),0,0]) | |
| particle.add_updater(p_upd) | |
| self.add(particle) | |
| self.play( | |
| time_tracker.animate.set_value(10), | |
| rate_func=linear, | |
| run_time=10, | |
| ) | |
| self.wait() | |