侧边栏壁纸
博主头像
分享你我博主等级

行动起来,活在当下

  • 累计撰写 108 篇文章
  • 累计创建 13 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录

某东滑动算法

管理员
2024-11-13 / 0 评论 / 0 点赞 / 7 阅读 / 4932 字
import random
import matplotlib.pyplot as plt
import numpy as np


class GTrace(object):
    def __init__(self):
        self.__pos_x = []
        self.__pos_y = []
        self.__pos_z = []
        self.__distance = 0
        self.__need_time = 0

    def __set_distance(self, _dist):
        """
        设置要生成的轨迹长度
        """
        self.__distance = _dist

        if _dist < 100:
            self.__need_time = int(random.uniform(500, 1500))
        else:
            self.__need_time = int(random.uniform(1000, 2000))

    def __set_pt_time(self):
        """
        设置各节点的时间
        """
        total_move_time = self.__need_time * random.uniform(0.8, 0.9)
        start_point_time = int(random.uniform(110, 200))
        self.__pos_z = [0, 0, start_point_time]

        sum_move_time = 0
        tmp_total_move_time = total_move_time

        while tmp_total_move_time > 0:
            delta_time = min(random.uniform(15, 20), tmp_total_move_time)
            sum_move_time += delta_time
            tmp_total_move_time -= delta_time
            self.__pos_z.append(int(start_point_time + sum_move_time))

        other_point_time = self.__need_time - (start_point_time + total_move_time)
        end_first_ptime = other_point_time / 2

        while end_first_ptime > 0:
            delta_time = min(random.uniform(110, 200), end_first_ptime)
            end_first_ptime -= delta_time
            self.__pos_z.append(int(self.__pos_z[-1] + delta_time))

        self.__pos_z.append(int(self.__pos_z[-1] + other_point_time / 2 + end_first_ptime))

    def __get_pos_y(self):
        """
        生成 Y 轴坐标
        """
        _pos_y = [random.uniform(-40, -18), 0]
        point_count = len(self.__pos_z)
        x = np.linspace(-10, 15, point_count - len(_pos_y))
        arct_y = np.arctan(x)

        for val in arct_y:
            _pos_y.append(val)

        return _pos_y

    def __get_pos_x(self):
        """
        生成 X 轴坐标
        """
        _pos_x = [random.uniform(-40, -18), 0]
        point_count = len(self.__pos_z)
        x = np.linspace(-1, 19, point_count - len(_pos_x))
        ss = np.arctan(x)
        th = np.tanh(x)

        for idx in range(len(th)):
            if th[idx] < ss[idx]:
                th[idx] = ss[idx]

        th += 1
        th *= (self.__distance / 2.5)

        start_idx = int(point_count / 10)
        end_idx = int(point_count / 50)
        delta_pt = abs(np.random.normal(scale=1.1, size=point_count - start_idx - end_idx))

        for idx in range(start_idx, point_count):
            if idx * 1.3 > len(delta_pt):
                break

            th[idx] += delta_pt[idx - start_idx]

        _pos_x.extend(th)
        return _pos_x[-1], _pos_x

    def __get_pos_z(self):
        return self.__pos_z

    def get_mouse_pos_path(self, distance):
        """
        获取滑动滑块鼠标的滑动轨迹坐标集合
        """
        self.__set_distance(distance)
        self.__set_pt_time()

        _distance, x = self.__get_pos_x()
        y = self.__get_pos_y()
        z = self.__get_pos_z()

        result = []
        for idx in range(len(x)):
            result.append([int(x[idx]), int(y[idx]), int(z[idx])])

        plt.plot(z, x)
        plt.show()

        return int(_distance), result

运行

trace = GTrace()
# distance = random.uniform(70, 150)
distance = 125

print("长度为: %d " % distance)
distance, mouse_pos_path = trace.get_mouse_pos_path(distance)
print("长度为: %d , 坐标为: " % distance, mouse_pos_path)

效果:

长度为: 150 
长度为: 151 , 坐标为:  [[-28, -24, 0], [0, 0, 0], [14, -1, 128], [21, -1, 144], [32, -1, 159], [45, -1, 178], [60, -1, 197], [74, -1, 217], [87, -1, 235], [98, -1, 251], [107, -1, 266], [113, -1, 284], [119, -1, 302], [124, -1, 320], [126, -1, 339], [129, -1, 359], [131, -1, 374], [133, -1, 392], [135, -1, 408], [137, -1, 426], [138, -1, 446], [139, -1, 462], [141, -1, 481], [140, -1, 496], [143, -1, 516], [142, -1, 535], [142, -1, 552], [143, -1, 570], [145, -1, 586], [144, -1, 604], [144, 0, 621], [145, 0, 636], [145, 0, 651], [146, 0, 666], [145, 0, 682], [147, 0, 699], [147, 0, 718], [146, 0, 735], [147, 0, 754], [148, 1, 770], [147, 1, 789], [148, 1, 806], [147, 1, 823], [149, 1, 842], [150, 1, 857], [149, 1, 877], [148, 1, 894], [148, 1, 909], [148, 1, 925], [149, 1, 944], [149, 1, 961], [149, 1, 979], [149, 1, 997], [151, 1, 1016], [149, 1, 1034], [149, 1, 1050], [150, 1, 1070], [150, 1, 1089], [150, 1, 1106], [149, 1, 1125], [149, 1, 1142], [149, 1, 1159], [149, 1, 1179], [150, 1, 1197], [150, 1, 1214], [150, 1, 1233], [150, 1, 1250], [150, 1, 1269], [150, 1, 1286], [150, 1, 1306], [150, 1, 1325], [150, 1, 1343], [150, 1, 1360], [150, 1, 1380], [150, 1, 1398], [150, 1, 1414], [150, 1, 1431], [150, 1, 1448], [150, 1, 1466], [150, 1, 1485], [151, 1, 1503], [151, 1, 1592], [151, 1, 1681]]

0

评论区