__author__ Lenovo import math from math import pi import numpy as np i

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
__author__ = 'Lenovo'
import math
from math import pi
import numpy as np
import matplotlib.pyplot as plt
def makePath(alpha, v0, k, eps, xmax, ymax):
# print alpha
alpha = math.radians(alpha)
# print alpha
vx = v0 * np.cos(alpha)
vy = v0 * np.sin(alpha)
xcur = xmax*0.5
ycur = ymax*0.5
while vx**2 + vy**2 > eps:
plt.bar(0, ymax, xmax, 0, color = 'w')
if alpha <= pi/2:
if ycur + (xmax - xcur) * np.tan(alpha) > ymax: #top
t1 = np.linspace(xcur, xcur + (ymax - ycur)/ np.tan(alpha), 2)
plt.plot(t1, np.tan(alpha)*t1 + ycur)
xcur = xcur + (ymax - ycur)/ np.tan(alpha)
ycur = ymax
alpha = 2*pi - alpha
else: #right
t1 = np.linspace(xcur, xmax, 2)
plt.plot(t1, np.tan(alpha)*t1 + ycur)
ycur = ycur + xmax * np.tan(alpha)
xcur = xmax
alpha = pi - alpha
elif alpha <= pi:
if ycur + abs(xcur * np.tan(alpha)) > ymax: #top
t1 = np.linspace(xcur - (ymax - ycur)/ np.tan(alpha), xcur, 2)
plt.plot(t1, np.tan(alpha)*t1 + ycur + abs(xcur * np.tan(alpha)))
xcur = xcur - (ymax - ycur)/ np.tan(alpha)
ycur = ymax
alpha = 2*pi - alpha
else: #left
t1 = np.linspace(0, xcur, 2)
plt.plot(t1, np.tan(alpha)*t1 + ycur + abs(xcur * np.tan(alpha)))
ycur = ycur + abs(xcur * np.tan(alpha))
xcur = 0
alpha = pi - alpha
elif alpha <= 3*pi/2:
if ycur - abs(xcur * np.tan(alpha)) < 0: #bottom
t1 = np.linspace(xcur - ycur/ np.tan(alpha), xcur, 2)
plt.plot(t1, np.tan(alpha)*t1 + ycur - abs(xcur * np.tan(alpha)))
xcur = xcur - ycur/ np.tan(alpha)
ycur = 0
alpha = 2*pi - alpha
else: #left
t1 = np.linspace(0, xcur, 2)
plt.plot(t1, np.tan(alpha)*t1 + ycur - abs(xcur * np.tan(alpha)))
ycur = ycur - abs(xcur * np.tan(alpha))
xcur = 0
alpha = 2*pi - alpha
else:
if ycur - abs(xcur * np.tan(alpha)) < 0: #bottom
t1 = np.linspace(xcur - ycur/ np.tan(alpha), xcur, 2)
plt.plot(t1, np.tan(alpha)*t1 + ycur - abs(xcur * np.tan(alpha)))
xcur = xcur - ycur/ np.tan(alpha)
ycur = 0
alpha = 2*pi - alpha
else: #left
t1 = np.linspace(0, xcur, 2)
plt.plot(t1, np.tan(alpha)*t1 + ycur - abs(xcur * np.tan(alpha)))
ycur = ycur - abs(xcur * np.tan(alpha))
xcur = 0
alpha = 2*pi - alpha
vx = vx*k
vy = vy*k
plt.show()
makePath(25, 15, 0.5, 0.1, 10, 20)