-
Notifications
You must be signed in to change notification settings - Fork 0
/
pure_persuit_random.py
76 lines (54 loc) · 1.88 KB
/
pure_persuit_random.py
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
75
76
import matplotlib.pyplot as plt
import random
import sys
def purePersuit(fighterSpeed):
xf = random.uniform(1, 1000)
yf = random.uniform(1, 1000)
xb = random.uniform(1, 1000)
yb = random.uniform(1, 1000)
xFighter = []
yFighter = []
xBomber = []
yBomber = []
time = 0
escapeDistance = 900
caughtDistance = 100
with open('output.txt', 'w') as output_file:
sys.stdout = output_file
while True:
xFighter.append(xf)
yFighter.append(yf)
xBomber.append(xb)
yBomber.append(yb)
plt.clf()
plt.title("Simulation of a Pure Pursuit")
plt.xlabel("X Position")
plt.ylabel("Y Position")
plt.plot(xFighter, yFighter, marker = ".", label="Fighter")
plt.plot(xBomber, yBomber, marker = ".",label="Bomber")
plt.xlim(-1000, 1200)
plt.ylim(-1000, 1200)
plt.legend()
plt.grid()
plt.pause(1)
distance = ((yb - yf) ** 2 + (xb-xf) ** 2) ** 0.5
print(f"Time = {time}\t XF = {xf:.2f}\t YF = {yf:.2f}\t XB = {xb:.2f}\t YB = {yb:.2f}\t Distance = {distance:.2f}")
if distance <= caughtDistance:
print(f"Bomber caught at {time} seconds")
break
if distance >= escapeDistance:
print(f"Bomber escaped from Fighter at {time} seconds")
break
sin = (yb - yf) / distance
cos = (xb - xf) / distance
time += 1
xf += fighterSpeed * cos
yf += fighterSpeed * sin
xb += random.uniform(-100, 100)
yb += random.uniform(-100, 100)
plt.show
def main():
speed = int(input("Enter the Fighter Speed: "))
purePersuit(speed)
if __name__ == "__main__":
main()