This commit is contained in:
upload
2026-10-08 17:34:02 +08:00
commit f216f6120b
961 changed files with 25144 additions and 0 deletions
+104
View File
@@ -0,0 +1,104 @@
import math
import random
from dataclasses import dataclass
from typing import Tuple
@dataclass
class HumanLikeRandomMover:
"""
生成“更像人移动”的随机坐标(输入/输出均为微度整数,如 23392745, 113219430)。
用法:
mover = HumanLikeRandomMover(23392745, 113219430)
lat_i, lng_i = mover.next_point()
"""
center_lat_i: int
center_lng_i: int
half_side_m: float = 80.0 # 正方形半边长(米)
speed_min_m_s: float = 0.7 # 最小步行速度(米/秒)
speed_max_m_s: float = 1.8 # 最大步行速度(米/秒)
turn_sigma_deg: float = 10.0 # 每步方向抖动(度),越小越平滑
pause_prob: float = 0.06 # 每次调用“轻微停顿”概率
dt_s: float = 1.0 # 两次调用间隔(秒)
def __post_init__(self):
self._micro = 1_000_000
self._x_m = 0.0 # 相对中心东向偏移(米)
self._y_m = 0.0 # 相对中心北向偏移(米)
self._heading = random.uniform(0, 2 * math.pi)
self._speed = random.uniform(self.speed_min_m_s, self.speed_max_m_s)
# --------- 对外方法 ---------
def next_point(self, center_lat_i: int = None, center_lng_i: int = None) -> Tuple[int, int]:
"""
返回下一个坐标(微度整数)。
可选传入新中心点;不传则使用初始化时中心点。
"""
if center_lat_i is not None and center_lng_i is not None:
self.center_lat_i = center_lat_i
self.center_lng_i = center_lng_i
# 1) 速度平滑变化(模拟人步速变化)
target_speed = random.uniform(self.speed_min_m_s, self.speed_max_m_s)
self._speed = 0.85 * self._speed + 0.15 * target_speed
# 2) 方向小幅变化(有惯性)
self._heading += math.radians(random.gauss(0, self.turn_sigma_deg))
# 3) 偶尔轻微停顿
step_m = self._speed * self.dt_s
if random.random() < self.pause_prob:
step_m *= random.uniform(0.0, 0.25)
# 4) 前进一步
nx = self._x_m + step_m * math.cos(self._heading)
ny = self._y_m + step_m * math.sin(self._heading)
# 5) 边界处理:在正方形内反弹,避免瞬移到另一边
bounced = False
if nx > self.half_side_m:
nx = self.half_side_m
self._heading = math.pi - self._heading
bounced = True
elif nx < -self.half_side_m:
nx = -self.half_side_m
self._heading = math.pi - self._heading
bounced = True
if ny > self.half_side_m:
ny = self.half_side_m
self._heading = -self._heading
bounced = True
elif ny < -self.half_side_m:
ny = -self.half_side_m
self._heading = -self._heading
bounced = True
if bounced:
self._heading += math.radians(random.uniform(-8, 8))
self._x_m, self._y_m = nx, ny
# 6) 米 -> 经纬度微度
out_lat_i, out_lng_i = self._offset_to_microdeg(self.center_lat_i, self.center_lng_i, self._x_m, self._y_m)
return out_lat_i, out_lng_i
def reset(self):
"""重置到中心点,方向和速度重新随机。"""
self._x_m = 0.0
self._y_m = 0.0
self._heading = random.uniform(0, 2 * math.pi)
self._speed = random.uniform(self.speed_min_m_s, self.speed_max_m_s)
# --------- 内部方法 ---------
def _offset_to_microdeg(self, center_lat_i: int, center_lng_i: int, x_m: float, y_m: float) -> Tuple[int, int]:
center_lat = center_lat_i / self._micro
center_lng = center_lng_i / self._micro
dlat = y_m / 111_320.0
cos_lat = max(1e-6, math.cos(math.radians(center_lat)))
dlng = x_m / (111_320.0 * cos_lat)
lat = center_lat + dlat
lng = center_lng + dlng
return int(round(lat * self._micro)), int(round(lng * self._micro))