56 lines
1.3 KiB
Python
56 lines
1.3 KiB
Python
import math
|
|
import random
|
|
from time import sleep
|
|
|
|
# from scipy import
|
|
from matplotlib import pyplot as plt
|
|
|
|
random.seed(1)
|
|
|
|
class RandomPilot():
|
|
angle: float
|
|
|
|
def __init__(self, velocity: float = 1):
|
|
self.dangle = 0
|
|
self.angle = random.random() * math.pi * 2
|
|
self.velocity = velocity
|
|
|
|
def step(self) -> tuple[float, float]:
|
|
# Поворот угла траектории
|
|
self.dangle += (random.random() - 0.5) * 0.1
|
|
self.dangle = max(min(self.dangle, 0.2), -0.2)
|
|
self.angle += self.dangle
|
|
|
|
dx = math.cos(self.angle) * self.velocity
|
|
dy = math.sin(self.angle) * self.velocity
|
|
|
|
return dx, dy
|
|
|
|
def _test():
|
|
randomPilot = RandomPilot()
|
|
point = [0, 0]
|
|
iter_count = 100
|
|
points = [point.copy()]
|
|
|
|
for i in range(iter_count):
|
|
dx, dy = randomPilot.step()
|
|
prev_point = point.copy()
|
|
point[0] += dx
|
|
point[1] += dy
|
|
points.append(point.copy())
|
|
|
|
coords = list(zip(*points))
|
|
padding = 5
|
|
plt.axis([
|
|
min(coords[0]) - padding, max(coords[0]) + padding,
|
|
min(coords[1]) - padding, max(coords[1]) + padding])
|
|
|
|
for i in range(iter_count):
|
|
plt.plot(coords[0][i:i+2], coords[1][i:i+2], color='#5e5')
|
|
plt.pause(0.05)
|
|
|
|
|
|
sleep(1)
|
|
|
|
if __name__ == '__main__':
|
|
_test() |