core.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import math
  2. # 巷道间距
  3. # 计算旋转角度
  4. def calculate_route(point_1, point_2):
  5. a, b = point_1
  6. c, d = point_2
  7. return math.atan((d - b) / (c - a))
  8. # 计算旋转坐标
  9. def calculate_route_coordinate(point, route):
  10. x = point[0]
  11. y = point[1]
  12. x1 = x * math.cos(route) + y * math.sin(route)
  13. y1 = -x * math.sin(route) + y * math.cos(route)
  14. return (x1, y1)
  15. def find_point_on_line(A, B, fraction):
  16. """
  17. 计算线段AB上距离A点fraction长度处的点C的坐标。
  18. 参数:
  19. A -- 点A的坐标,形式为(x1, y1)
  20. B -- 点B的坐标,形式为(x2, y2)
  21. fraction -- 线段AB上距离A点的比例(0到1之间)
  22. 返回:
  23. 点C的坐标,形式为(x, y)
  24. """
  25. x1, y1 = A
  26. x2, y2 = B
  27. # 使用线性插值计算C点的坐标
  28. x = x1 + (x2 - x1) * fraction
  29. y = y1 + (y2 - y1) * fraction
  30. return (x, y)
  31. # 计算 a点到b点之间1/x处(距离a点)的c点坐标
  32. def calculate_point_c(point_a, point_b, x):
  33. x_a, y_a = point_a
  34. x_b, y_b = point_b
  35. # 计算方向向量
  36. dx = x_b - x_a
  37. dy = y_b - y_a
  38. # 计算方向向量的模长
  39. distance_ab = math.sqrt(dx ** 2 + dy ** 2)
  40. # 计算单位方向向量
  41. unit_vector_x = dx / distance_ab
  42. unit_vector_y = dy / distance_ab
  43. # 计算点C的坐标
  44. x_c = x_a + (1 / x) * unit_vector_x
  45. y_c = y_a + (1 / x) * unit_vector_y
  46. return x_c, y_c
  47. # 找线段之间某点的坐标
  48. def rotate_point_around_another(point_a, point_b, a):
  49. x_a, y_a = point_a
  50. x_b, y_b = point_b
  51. """
  52. 旋转点A(x_a, y_a)围绕点B(x_b, y_b) a 弧度后的新坐标。
  53. """
  54. # 将A点的坐标转换到以B点为原点的坐标系中
  55. dx = x_a - x_b
  56. dy = y_a - y_b
  57. # 旋转坐标
  58. x_prime = dx * math.cos(a) - dy * math.sin(a)
  59. y_prime = dx * math.sin(a) + dy * math.cos(a)
  60. # 将旋转后的坐标转换回原来的坐标系
  61. x_rotated = x_prime + x_b
  62. y_rotated = y_prime + y_b
  63. return x_rotated, y_rotated
  64. def calculate_angle_with_x_axis(point1, point2):
  65. x1, y1 = point1
  66. x2, y2 = point2
  67. """
  68. 计算线段(x1, y1)到(x2, y2)与x轴的夹角(以弧度为单位)。
  69. 结果角度是从x轴正方向逆时针旋转到线段方向所经过的角度。
  70. """
  71. # 首先计算线段的斜率
  72. if x1 == x2: # 避免除以零
  73. if y2 > y1:
  74. return math.pi / 2 # 垂直于x轴且向上
  75. elif y2 < y1:
  76. return -math.pi / 2 # 垂直于x轴且向下
  77. else:
  78. return 0 # 与x轴重合
  79. else:
  80. slope = (y2 - y1) / (x2 - x1)
  81. # 使用atan2计算角度,它返回从x轴正方向到点(y, x)的向量与x轴之间的夹角
  82. # 注意:atan2的参数顺序是(y的差, x的差),即(y2-y1, x2-x1)
  83. angle_radians = math.atan2(y2 - y1, x2 - x1)
  84. return angle_radians
  85. def get_color_by_layer(layer_id):
  86. return {
  87. '1': 53,
  88. '2': 83,
  89. '3': 133,
  90. '4': 173
  91. }.get(layer_id)
  92. def min_distance_between_segments(seg1, seg2):
  93. """
  94. 计算两条线段之间的最小距离(基于端点之间的最小距离)
  95. seg1, seg2: 线段的端点坐标,例如 [(x1, y1), (x2, y2)]
  96. """
  97. # 提取端点
  98. p1, p2 = seg1
  99. p3, p4 = seg2
  100. # 计算所有端点之间的距离
  101. dist1 = distance(p1, p3)
  102. dist2 = distance(p1, p4)
  103. dist3 = distance(p2, p3)
  104. dist4 = distance(p2, p4)
  105. # 返回最小距离
  106. return min(dist1, dist2, dist3, dist4)
  107. def distance(p1, p2):
  108. """计算两点之间的距离"""
  109. return math.sqrt((p2[0] - p1[0]) ** 2 + (p2[1] - p1[1]) ** 2)