import numpy as np import matplotlib.pyplot as plt # Turtle Graphics status class myTurtle: def __init__(self, r=0, theta=0, cur_x=0, cur_y=0): self.r = r # length self.theta = theta # turning angle self.cur_x = cur_x # current (x, y) coordinates self.cur_y = cur_y self.x_list = [cur_x] # list of positions visitted self.y_list = [cur_y] def goto(self, x=0, y=0): # goto(x,y) with drawing self.cur_x = x self.cur_y = y self.x_list.append(self.cur_x) self.y_list.append(self.cur_y) def jumpto(self, x=0, y=0): # goto(x,y) without changing drawing status # this should disconnect current list of positions (TODO) self.cur_x = x self.cur_y = y def forward(self, r=1): delta_x = r * np.cos(self.theta/180.0*np.pi) delta_y = r * np.sin(self.theta/180.0*np.pi) self.cur_x += delta_x self.cur_y += delta_y self.x_list.append(self.cur_x) self.y_list.append(self.cur_y) def backward(self, r=1): delta_x = r * np.cos(self.theta/180.0*np.pi) delta_y = r * np.sin(self.theta/180.0*np.pi) self.cur_x -= delta_x self.cur_y -= delta_y self.x_list.append(self.cur_x) self.y_list.append(self.cur_y) def right(self, angle=90): self.theta -= angle def left(self, angle=90): self.theta += angle def reset(self): self.__init__() # Whenever you change the attributes/methods above, # you need to re-create objects so their members are # updated. (Or, there will be NO Attribute Error.) tt = myTurtle() # tt = myTurtle() tt.reset() for i in range(50): tt.forward(i) tt.right(90) x_list1 = tt.x_list y_list1 = tt.y_list # Plot the points using matplotlib plt.plot(x_list1, y_list1) plt.xlabel('x coordinate') plt.ylabel('y coordinate') plt.title('Spiral') plt.legend(['90-cw']) plt.show() tt.reset() for i in range(50): tt.forward(i) tt.left(60) # draw the trace... x_list2 = tt.x_list y_list2 = tt.y_list # Plot the points using matplotlib plt.plot(x_list2, y_list2) plt.xlabel('x coordinate') plt.ylabel('y coordinate') plt.title('Spiral') plt.legend(['60-ccw']) plt.show() tt.reset() for i in range(50): tt.forward(i) tt.left(90+1) # draw the trace... x_list3 = tt.x_list y_list3 = tt.y_list # Plot the points using matplotlib plt.plot(x_list3, y_list3) plt.xlabel('x coordinate') plt.ylabel('y coordinate') plt.title('Spiral') plt.legend(['91-ccw']) plt.show() tt.reset() tt.jumpto(-10,0) # so the center of the graph is near to (0,0) for i in range(50): # tt.forward(i) tt.forward(20) tt.left(170) # draw the trace... x_list4 = tt.x_list y_list4 = tt.y_list # Plot the points using matplotlib plt.plot(x_list4, y_list4) plt.xlabel('x coordinate') plt.ylabel('y coordinate') plt.title('Spiral (constant steps)') plt.legend(['170-ccw-20']) plt.show() # Plot the points using matplotlib plt.plot(x_list1, y_list1, x_list2, y_list2, x_list3, y_list3, x_list4, y_list4) plt.xlabel('x coordinate') plt.ylabel('y coordinate') plt.title('Spiral') plt.legend(['90-cw', '60-ccw', '91-ccw', '170-ccw-20']) plt.show() # FIN # # plot at a 2x2 subplot matrix # # subplot[2,2][1] plt.subplot(2, 2, 1) plt.plot(x_list1, y_list1) # plt.xlabel('x coordinate') plt.ylabel('y coordinate') plt.title('Spiral') plt.legend(['90-cw']) # subplot[2,2][2] plt.subplot(2, 2, 2) plt.plot(x_list2, y_list2) # plt.xlabel('x coordinate') # plt.ylabel('y coordinate') plt.title('Spiral') plt.legend(['60-ccw']) # subplot[2,2][3] plt.subplot(2, 2, 3) plt.plot(x_list3, y_list3) plt.xlabel('x coordinate') plt.ylabel('y coordinate') # plt.title('Spiral') plt.legend(['91-ccw']) # subplot[2,2][4] plt.subplot(2, 2, 4) plt.plot(x_list4, y_list4) plt.xlabel('x coordinate') # plt.ylabel('y coordinate') # plt.title('Spiral') plt.legend(['170-ccw-20']) # plt.show() # ?? can not .show() if you want to savefig() plt.savefig('Spiral_2x2_.png', dpi=150) # plt.scatter(x_list3, y_list3, s=20) out_png = 'Sprial_91_ccw.png' plt.savefig(out_png, dpi=150) # FIN