import numpy as np import scipy.integrate as spi import matplotlib.pylab as plt %matplotlib inline #-- Alpha parameters should be given in degrees def calphas(s, alpha, l = 1, k = 1, phi = 0): #-- Convert alpha and phi into radians alpha_rad = alpha * np.pi / 180. phi_rad = phi * np.pi / 180. return np.cos(alpha_rad * np.cos(2 * np.pi * k * s / l + phi_rad)) def salphas(s, alpha, l = 1, k = 1, phi = 0): #-- Convert alpha into radians alpha_rad = alpha * np.pi / 180. phi_rad = phi * np.pi / 180. return np.sin(alpha_rad * np.cos(2 * np.pi * k * s / l + phi_rad)) #-- Scalar functions. Variable s is a real number def serp_x_scalar (s, alpha, l = 1, k = 1, phi = 0): return spi.quad(calphas, 0, s, args =(alpha, l, k, phi) ) def serp_y_scalar (s, alpha, l = 1, k = 1, phi = 0): return spi.quad(salphas, 0, s, args =(alpha, l, k, phi) ) def serp (s, alpha = 45, l = 1, k = 1, phi = 0): """Serpenoid curve Inputs: - alpha : winding angle (in degrees) - l : Curve length - k : Number of undulations S is the variable (it is an array). The x,y arrays are returned """ #-- Results x = [] y = [] for i, vs in enumerate(s): x.append(serp_x_scalar(vs, alpha, l, k, phi)[0]) y.append(serp_y_scalar(vs, alpha, l, k, phi)[0]) return x,y #-- Obtener los puntos a lo largo de la curva s = np.linspace(0, 1, 100) #-- Obtener las coordenadas x,y x, y = serp(s, alpha = 60, l = 1, k = 1) #-- Dibujar la curva plt.plot(x, y, linewidth = 3) plt.grid() plt.axis('equal') plt.show() from IPython.html import widgets # Widget definitions from IPython.display import display # Used to display widgets in the notebook widget_alpha = widgets.FloatSliderWidget() widget_alpha.min = 0.0 widget_alpha.max = 121 widget_alpha.description = "Alpha" widget_alpha.value = 60 widget_k = widgets.FloatSliderWidget() widget_k.min = 0.1 widget_k.max = 20 widget_k.description = "k" widget_k.value = 1 def draw_serp(): #-- Obtener los puntos a lo largo de la curva s = np.linspace(0, 1, 100) #-- Obtener las coordenadas x,y x, y = serp(s, alpha = widget_alpha.value, l = 1, k = widget_k.value) #-- Dibujar la curva plt.plot(x, y, linewidth = 3) plt.grid() plt.axis('equal') plt.show() button_draw = widgets.ButtonWidget(description="Draw!") def on_button_draw_clicked(b): draw_serp() button_draw.on_click(on_button_draw_clicked) display(widget_alpha) display(widget_k) display(button_draw) draw_serp()