算法暴露接口(xhs、dy、ks、wx、hnw)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.7 KiB

7 months ago
  1. import random
  2. class Generate_trajectory:
  3. @staticmethod
  4. def __ease_out_expo(sep):
  5. if sep == 1:
  6. return 1
  7. else:
  8. return 1 - pow(2, -10 * sep)
  9. @staticmethod
  10. def __generate_y():
  11. init_y = random.randint(10, 30)
  12. y = [init_y]
  13. for i in range(20):
  14. _ = random.randint(-1, 1)
  15. y += [y[-1] + _] * random.randint(5, 10)
  16. return y
  17. @classmethod
  18. def get_slide_track(cls, distance):
  19. """
  20. :param distance:
  21. """
  22. if not isinstance(distance, int) or distance < 0:
  23. raise ValueError(f"distance类型必须是大于等于0的整数: distance: {distance}, type: {type(distance)}")
  24. # 共记录count次滑块位置信息
  25. count = 30 + int(distance / 20)
  26. # 初始化滑动时间
  27. t = random.randint(50, 100)
  28. # 记录上一次滑动的距离
  29. _x = random.randint(0, 5)
  30. # _x = 0
  31. _y = cls.__generate_y()
  32. # 初始化轨迹列表
  33. slide_track = [
  34. '|'.join([str(random.randint(15, 30)), str(_y.pop(0)), str(0)])
  35. ]
  36. for i in range(count):
  37. # 已滑动的横向距离
  38. x = round(cls.__ease_out_expo(i / count) * distance)
  39. # 滑动过程消耗的时间
  40. t += random.randint(30, 40)
  41. if x == _x:
  42. continue
  43. slide_track.append('|'.join([str(x), str(_y[i]), str(t)]))
  44. _x = x
  45. slide_track.append(slide_track[-1])
  46. return ','.join(slide_track)
  47. if __name__ == '__main__':
  48. print(Generate_trajectory().get_slide_track(500))