import numpy as np
import math
import random
class bezierTrajectory:
def _bztsg(self, dataTrajectory):
lengthOfdata = len(dataTrajectory)
def staer(x):
t = ((x - dataTrajectory[0][0]) / (dataTrajectory[-1][0] - dataTrajectory[0][0]))
y = np.array([0, 0], dtype=np.float64)
for s in range(len(dataTrajectory)):
y += dataTrajectory[s] * ((math.factorial(lengthOfdata - 1) / (
math.factorial(s) * math.factorial(lengthOfdata - 1 - s))) * math.pow(t, s) * math.pow(
(1 - t), lengthOfdata - 1 - s))
return y[1]
return staer
def _type(self, type, x, numberList):
numberListre = []
pin = (x[1] - x[0]) / numberList
if type == 0:
for i in range(numberList):
numberListre.append(i * pin)
if pin >= 0:
numberListre = numberListre[::-1]
elif type == 1:
for i in range(numberList):
numberListre.append(1 * ((i * pin) ** 2))
numberListre = numberListre[::-1]
elif type == 2:
for i in range(numberList):
numberListre.append(1 * ((i * pin - x[1]) ** 2))
elif type == 3:
dataTrajectory = [np.array([0, 0]), np.array([(x[1] - x[0]) * 0.8, (x[1] - x[0]) * 0.6]),
np.array([x[1] - x[0], 0])]
fun = self._bztsg(dataTrajectory)
numberListre = [0]
for i in range(1, numberList):
numberListre.append(fun(i * pin) + numberListre[-1])
if pin >= 0:
numberListre = numberListre[::-1]
numberListre = np.abs(np.array(numberListre) - max(numberListre))
biaoNumberList = ((numberListre - numberListre[numberListre.argmin()]) / (
numberListre[numberListre.argmax()] - numberListre[numberListre.argmin()])) * (x[1] - x[0]) + x[0]
biaoNumberList[0] = x[0]
biaoNumberList[-1] = x[1]
return biaoNumberList
def getFun(self, s):
dataTrajectory = []
for i in s:
dataTrajectory.append(np.array(i))
return self._bztsg(dataTrajectory)
def simulation(self, start, end, le=1, deviation=0, bias=0.5):
start = np.array(start)
end = np.array(end)
cbb = []
if le != 1:
e = (1 - bias) / (le - 1)
cbb = [[bias + e * i, bias + e * (i + 1)] for i in range(le - 1)]
dataTrajectoryList = [start]
t = random.choice([-1, 1])
w = 0
for i in cbb:
px1 = start[0] + (end[0] - start[0]) * (random.random() * (i[1] - i[0]) + (i[0]))
p = np.array([px1, self._bztsg([start, end])(px1) + t * deviation])
dataTrajectoryList.append(p)
w += 1
if w >= 2:
w = 0
t = -1 * t
dataTrajectoryList.append(end)
return {"equation": self._bztsg(dataTrajectoryList), "P": np.array(dataTrajectoryList)}
def trackArray(self, start, end, numberList, le=1, deviation=0, bias=0.5, type=0, cbb=0, yhh=10):
s = []
fun = self.simulation(start, end, le, deviation, bias)
w = fun['P']
fun = fun["equation"]
if cbb != 0:
numberListOfcbb = round(numberList * 0.2 / (cbb + 1))
numberList -= (numberListOfcbb * (cbb + 1))
xTrackArray = self._type(type, [start[0], end[0]], numberList)
for i in xTrackArray:
s.append([i, fun(i)])
dq = yhh / cbb
kg = 0
ends = np.copy(end)
for i in range(cbb):
if kg == 0:
d = np.array([end[0] + (yhh - dq * i),
((end[1] - start[1]) / (end[0] - start[0])) * (end[0] + (yhh - dq * i)) + (
end[1] - ((end[1] - start[1]) / (end[0] - start[0])) * end[0])])
kg = 1
else:
d = np.array([end[0] - (yhh - dq * i),
((end[1] - start[1]) / (end[0] - start[0])) * (end[0] - (yhh - dq * i)) + (
end[1] - ((end[1] - start[1]) / (end[0] - start[0])) * end[0])])
kg = 0
print(d)
y = self.trackArray(ends, d, numberListOfcbb, le=2, deviation=0, bias=0.5, type=0, cbb=0, yhh=10)
s += list(y['trackArray'])
ends = d
y = self.trackArray(ends, end, numberListOfcbb, le=2, deviation=0, bias=0.5, type=0, cbb=0, yhh=10)
s += list(y['trackArray'])
else:
xTrackArray = self._type(type, [start[0], end[0]], numberList)
for i in xTrackArray:
s.append([i, fun(i)])
return {"trackArray": np.array(s), "P": w}
a = bezierTrajectory()
r = a.trackArray(start=[830, 559], end=[1085, 554], numberList=30, le=4, deviation=0, bias=0.5, type=2, cbb=0,
yhh=0)
g = []
for i, l in enumerate(r['trackArray']):
s = {}
s['pageX'] = int(l[0])
s['clientX'] = int(l[0])
s['pageY'] = int(l[1])
s['clientY'] = int(l[1])
s['type'] = "mousedown" if i == 3 else "mousemove"
s['which'] = 1 if i == 3 else 0
g.append(s)
print(g)