Skip to content

Instantly share code, notes, and snippets.

@AppleVegas
Created June 30, 2023 12:24
Show Gist options
  • Save AppleVegas/10b0a00031a490523b4880a268479370 to your computer and use it in GitHub Desktop.
Save AppleVegas/10b0a00031a490523b4880a268479370 to your computer and use it in GitHub Desktop.
import numpy as np
import matplotlib.pyplot as plt
import random
from scipy.optimize import fmin
idealspeed = 3*(10**8) # meters в секунду
sigma = 10 * 10**-9 # +- 10 nanoseconds
class vec2:
def __init__(self, x: float, y: float) -> None:
self.x = x
self.y = y
def random():
r_x = np.round(np.random.rand() * 1000, 3)
r_y = np.round(np.random.rand() * 1000, 3)
return vec2(r_x, r_y)
def distto(self, vec):
return np.sqrt(((vec.x - self.x)**2) + ((vec.y - self.y)**2))
def __str__(self):
return "[%g, %g]" % (self.x, self.y)
class tower:
def __init__(self, pos: vec2) -> None:
self.pos = pos
self.time = 0
def __str__(self):
return "tower at [%g, %g]" % (self.pos.x, self.pos.y)
class hooman:
def __init__(self, towers: list) -> None:
r1 = np.random.rand()
r2 = np.random.rand()
sqrtR1 = np.sqrt(r1)
x = (1 - sqrtR1) * towers[0].pos.x + (sqrtR1 * (1 - r2)) * towers[1].pos.x + (sqrtR1 * r2) * towers[2].pos.x
y = (1 - sqrtR1) * towers[0].pos.y + (sqrtR1 * (1 - r2)) * towers[1].pos.y + (sqrtR1 * r2) * towers[2].pos.y
self.pos = vec2(x,y)
class task_dist:
def __init__(self) -> None:
self.towers = []
self.human = hooman(self.towers)
def lmao():
towers = []
for i in range(0,3):
towers.append(tower(vec2.random()))
human = hooman(towers)
tpoints = [[t.pos.x, t.pos.y] for t in towers]
plt.figure(0)
plt.scatter(tpoints[:][0], tpoints[:][1], s = 170, color = "blue", alpha=0)
tt1 = plt.Polygon(tpoints[:3][:], color="red", fill=False)
plt.gca().add_patch(tt1)
for t in towers:
distance = t.pos.distto(human.pos)
t.time = distance/idealspeed
plt.plot([t.pos.x, human.pos.x], [t.pos.y, human.pos.y], linestyle="dotted",linewidth=1, markersize=12, label = '{0:.7f} секунд'.format(t.time))
plt.legend()
plt.plot(human.pos.x, human.pos.y,'bo', markersize=2)
plt.figure(1)
plt.scatter(tpoints[:][0], tpoints[:][1], s = 170, color = "blue", alpha=0)
tt1 = plt.Polygon(tpoints[:3][:], color="red", fill=False)
plt.gca().add_patch(tt1)
for t in towers:
t.time = random.gauss(t.time, sigma)
plt.plot([t.pos.x, human.pos.x], [t.pos.y, human.pos.y], linestyle="dotted",linewidth=1, markersize=12, label = '{0:.7f} секунд'.format(t.time))
circle = plt.Circle((t.pos.x, t.pos.y), t.time*idealspeed, color='g', fill=False)
plt.gca().add_patch(circle)
def f(X0):
x, y = X0
summ = 0
for tower in towers:
summ += (((x - tower.pos.x)**2) + ((y - tower.pos.y)**2) - ((tower.time*idealspeed)**2))**2
return summ
guess = (1, 1)
guess = fmin(f, guess)
print(guess)
plt.plot(guess[0], guess[1],'bo', markersize=2, color = "r")
plt.legend()
plt.show()
if __name__ == "__main__":
lmao()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment