xu3kev commited on
Commit
6f70e6f
1 Parent(s): bf6a775

Update myturtle.py

Browse files
Files changed (1) hide show
  1. myturtle.py +30 -38
myturtle.py CHANGED
@@ -1,49 +1,28 @@
1
- import re
2
- from PIL import Image
3
- from matplotlib import pyplot as plt
4
- import matplotlib as mpl
5
- import matplotlib.style as mplstyle
6
  import numpy as np
7
-
8
- mpl.use('Agg')
9
- mpl.rcParams["path.simplify_threshold"] = 0.0
10
- mpl.rcParams['agg.path.chunksize'] = 10000
11
- mplstyle.use('fast')
12
 
13
  HALF_INF = 63
14
  INF = 126
15
  EPS_DIST = 1/20
16
  EPS_ANGLE = 2.86
 
17
 
18
  class Turtle:
19
- def __init__(self, ax=None):
20
- self.x = 0
21
- self.y = 0
22
  self.heading = 0
23
- if ax is None:
24
- self.fig, self.ax = plt.subplots(1, 1, figsize=(20, 20))
25
- else:
26
- self.ax = ax
27
- self.ax.set_xlim(-50, 50)
28
- self.ax.set_ylim(-50, 50)
29
- self.ax.set_xticklabels([])
30
- self.ax.set_yticklabels([])
31
- self.ax.set_xticks([])
32
- self.ax.set_yticks([])
33
- self.ax.spines['top'].set_visible(False)
34
- self.ax.spines['bottom'].set_visible(False)
35
- self.ax.spines['left'].set_visible(False)
36
- self.ax.spines['right'].set_visible(False)
37
  self.is_down = True
38
 
39
  def forward(self, dist):
 
40
  x0, y0 = self.x, self.y
41
- x1 = x0 + dist * np.cos(self.heading)
42
- y1 = y0 + dist * np.sin(self.heading)
43
  if self.is_down:
44
- self.ax.plot([x0, x1], [y0, y1], color='black', linewidth=3)
45
- self.x = x1
46
- self.y = y1
47
 
48
  def left(self, angle):
49
  self.heading += angle * np.pi / 180
@@ -58,11 +37,9 @@ class Turtle:
58
  self.is_down = True
59
 
60
  def save(self, path):
61
- self.fig.canvas.draw()
62
- pil_img = Image.frombytes('RGB', self.fig.canvas.get_width_height(), self.fig.canvas.tostring_rgb())
63
  if path:
64
- pil_img.save(path)
65
- return pil_img
66
 
67
  class _TurtleState:
68
  def __init__(self, turtle):
@@ -135,5 +112,20 @@ if __name__ == "__main__":
135
 
136
  right(90)
137
  forward(50)
138
- save("test.png")
139
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import numpy as np
2
+ import cv2
 
 
 
 
3
 
4
  HALF_INF = 63
5
  INF = 126
6
  EPS_DIST = 1/20
7
  EPS_ANGLE = 2.86
8
+ SCALE = 15.5
9
 
10
  class Turtle:
11
+ def __init__(self, canvas_size=(2000, 2000)):
12
+ self.x = canvas_size[0] // 2
13
+ self.y = canvas_size[1] // 2
14
  self.heading = 0
15
+ self.canvas = np.ones((canvas_size[1], canvas_size[0], 3), dtype=np.uint8) * 255
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  self.is_down = True
17
 
18
  def forward(self, dist):
19
+ dist = dist * SCALE
20
  x0, y0 = self.x, self.y
21
+ x1 = int(x0 + dist * np.cos(self.heading))
22
+ y1 = int(y0 - dist * np.sin(self.heading))
23
  if self.is_down:
24
+ cv2.line(self.canvas, (x0, y0), (x1, y1), (0, 0, 0), 3)
25
+ self.x, self.y = x1, y1
 
26
 
27
  def left(self, angle):
28
  self.heading += angle * np.pi / 180
 
37
  self.is_down = True
38
 
39
  def save(self, path):
 
 
40
  if path:
41
+ cv2.imwrite(path, self.canvas)
42
+ return self.canvas
43
 
44
  class _TurtleState:
45
  def __init__(self, turtle):
 
112
 
113
  right(90)
114
  forward(50)
115
+ save("test2.png")
116
+
117
+ def plot2():
118
+ for j in range(2):
119
+ forward(2)
120
+ left(0.0)
121
+ for i in range(4):
122
+ forward(2)
123
+ left(90)
124
+ forward(0)
125
+ left(180.0)
126
+ forward(2)
127
+ left(180.0)
128
+ FINAL_IMAGE = turtle.save("")
129
+
130
+ example_plot()
131
+ # plot2()