core.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. import math
  2. import random
  3. import sys
  4. import numpy as np
  5. # hex 转rgb
  6. def hex_to_rgb(hex_color):
  7. # 确保输入以 '#' 开头并且长度为 7
  8. if hex_color.startswith('#') and len(hex_color) == 7:
  9. # 去掉 '#' 并将字符串拆分为 RGB 部分
  10. hex_rgb = hex_color[1:]
  11. # 每两个字符一组转换为十进制数
  12. r = int(hex_rgb[0:2], 16)
  13. g = int(hex_rgb[2:4], 16)
  14. b = int(hex_rgb[4:6], 16)
  15. return r, g, b
  16. else:
  17. raise ValueError("Invalid hex color code")
  18. # 计算旋转角度
  19. def calculate_route(point_1, point_2):
  20. a, b = point_1
  21. c, d = point_2
  22. if a == c:
  23. return 1
  24. else:
  25. return math.atan((d - b) / (c - a))
  26. # 计算旋转坐标
  27. def calculate_route_coordinate(point, route):
  28. x = point[0]
  29. y = point[1]
  30. x1 = x * math.cos(route) + y * math.sin(route)
  31. y1 = -x * math.sin(route) + y * math.cos(route)
  32. return (x1, y1)
  33. def find_point_on_line(A, B, fraction):
  34. """
  35. 计算线段AB上距离A点fraction长度处的点C的坐标。
  36. 参数:
  37. A -- 点A的坐标,形式为(x1, y1)
  38. B -- 点B的坐标,形式为(x2, y2)
  39. fraction -- 线段AB上距离A点的比例(0到1之间)
  40. 返回:
  41. 点C的坐标,形式为(x, y)
  42. """
  43. x1, y1 = A
  44. x2, y2 = B
  45. # 使用线性插值计算C点的坐标
  46. x = x1 + (x2 - x1) * fraction
  47. y = y1 + (y2 - y1) * fraction
  48. return (x, y)
  49. def scale_point(x, y, k):
  50. """
  51. 缩放二维点坐标。
  52. 参数:
  53. x, y: 原点的x和y坐标。
  54. k: 缩放因子。
  55. 返回:
  56. 新的x'和y'坐标。
  57. """
  58. x_prime = k * x
  59. y_prime = k * y
  60. return x_prime, y_prime
  61. # 计算 a点到b点之间1/x处(距离a点)的c点坐标
  62. def calculate_point_c(point_a, point_b, x):
  63. x_a, y_a = point_a
  64. x_b, y_b = point_b
  65. # 计算方向向量
  66. dx = x_b - x_a
  67. dy = y_b - y_a
  68. # 计算方向向量的模长
  69. distance_ab = math.sqrt(dx ** 2 + dy ** 2)
  70. # 计算单位方向向量
  71. unit_vector_x = dx / distance_ab
  72. unit_vector_y = dy / distance_ab
  73. # 计算点C的坐标
  74. x_c = x_a + (1 / x) * unit_vector_x
  75. y_c = y_a + (1 / x) * unit_vector_y
  76. return x_c, y_c
  77. # 找线段之间某点的坐标
  78. def rotate_point_around_another(point_a, point_b, a):
  79. x_a, y_a = point_a
  80. x_b, y_b = point_b
  81. """
  82. 旋转点A(x_a, y_a)围绕点B(x_b, y_b) a 弧度后的新坐标。
  83. """
  84. # 将A点的坐标转换到以B点为原点的坐标系中
  85. dx = x_a - x_b
  86. dy = y_a - y_b
  87. # 旋转坐标
  88. x_prime = dx * math.cos(a) - dy * math.sin(a)
  89. y_prime = dx * math.sin(a) + dy * math.cos(a)
  90. # 将旋转后的坐标转换回原来的坐标系
  91. x_rotated = x_prime + x_b
  92. y_rotated = y_prime + y_b
  93. return x_rotated, y_rotated
  94. def calculate_angle_with_x_axis(point1, point2):
  95. x1, y1 = point1
  96. x2, y2 = point2
  97. """
  98. 计算线段(x1, y1)到(x2, y2)与x轴的夹角(以弧度为单位)。
  99. 结果角度是从x轴正方向逆时针旋转到线段方向所经过的角度。
  100. """
  101. # 首先计算线段的斜率
  102. if x1 == x2: # 避免除以零
  103. if y2 > y1:
  104. return math.pi / 2 # 垂直于x轴且向上
  105. elif y2 < y1:
  106. return -math.pi / 2 # 垂直于x轴且向下
  107. else:
  108. return 0 # 与x轴重合
  109. else:
  110. slope = (y2 - y1) / (x2 - x1)
  111. # 使用atan2计算角度,它返回从x轴正方向到点(y, x)的向量与x轴之间的夹角
  112. # 注意:atan2的参数顺序是(y的差, x的差),即(y2-y1, x2-x1)
  113. angle_radians = math.atan2(y2 - y1, x2 - x1)
  114. return angle_radians
  115. def get_color_by_layer(layer_id):
  116. return {
  117. '1': 53,
  118. '2': 83,
  119. '3': 133,
  120. '4': 173,
  121. '5': 162,
  122. '6': 77
  123. }.get(layer_id)
  124. def min_distance_between_segments(seg1, seg2):
  125. """
  126. 计算两条线段之间的最小距离(基于端点之间的最小距离)
  127. seg1, seg2: 线段的端点坐标,例如 [(x1, y1), (x2, y2)]
  128. """
  129. # 提取端点
  130. p1, p2 = seg1
  131. p3, p4 = seg2
  132. # 计算所有端点之间的距离
  133. dist1 = distance(p1, p3)
  134. dist2 = distance(p1, p4)
  135. dist3 = distance(p2, p3)
  136. dist4 = distance(p2, p4)
  137. # 返回最小距离
  138. return min(dist1, dist2, dist3, dist4)
  139. def distance(p1, p2):
  140. """计算两点之间的距离"""
  141. return math.sqrt((p2[0] - p1[0]) ** 2 + (p2[1] - p1[1]) ** 2)
  142. def symmetric_point(A, B):
  143. """
  144. 计算点A相对于点B的对称点C的坐标
  145. 参数:
  146. A -- 点A的坐标,形式为(x_a, y_a)
  147. B -- 点B的坐标,形式为(x_b, y_b)
  148. 返回:
  149. 点C的坐标,形式为(x_c, y_c)
  150. """
  151. x_a, y_a = A
  152. x_b, y_b = B
  153. x_c = 2 * x_b - x_a
  154. y_c = 2 * y_b - y_a
  155. return x_c, y_c
  156. def divide_segment(A, B, x):
  157. # A 和 B 是元组,表示线段的两个端点 (x, y)
  158. # x 是分割段数
  159. # 计算线段 AB 的长度
  160. length_AB = ((B[0] - A[0]) ** 2 + (B[1] - A[1]) ** 2) ** 0.5
  161. # 计算每段的长度
  162. segment_length = length_AB / (x + 1) # 加1是因为包括端点在内有x+1个点
  163. # 初始化点的列表,包括起点 A
  164. points = [A]
  165. # 使用线性插值法计算每个分割点的坐标
  166. for i in range(1, x + 1):
  167. t = i / (x + 1) # 插值参数,从0到1
  168. Cx = A[0] + t * (B[0] - A[0])
  169. Cy = A[1] + t * (B[1] - A[1])
  170. points.append((Cx, Cy))
  171. # 包括终点 B
  172. points.append(B)
  173. return points
  174. def get_random_id():
  175. return random.randint(0, 99999999)
  176. def parallel_line(start, end, distance):
  177. """
  178. 计算与给定线段平行且距离为distance的线段(只返回上方的一条)。
  179. 参数:
  180. start -- 原始线段的起点坐标,形式为(x, y)
  181. end -- 原始线段的终点坐标,形式为(x, y)
  182. distance -- 平行线段与原始线段的距离
  183. 返回:
  184. tuple -- 平行线段的起点和终点坐标,形式为((new_start_x, new_start_y), (new_end_x, new_end_y))
  185. """
  186. # 计算原始线段的斜率
  187. if start[0] == end[0]: # 斜率不存在(垂直线)
  188. # 对于垂直线,我们只需在x坐标上保持不变,y坐标增减distance
  189. new_start = (start[0], start[1] + distance)
  190. new_end = (end[0], end[1] + distance)
  191. else:
  192. # 计算斜率
  193. slope = (end[1] - start[1]) / (end[0] - start[0])
  194. # 计算法线的斜率(负倒数)
  195. if slope == 0:
  196. slope = 1
  197. normal_slope = -1 / slope
  198. # 计算法线方向上从起点到距离distance的点的偏移量
  199. # 注意:这里我们使用勾股定理的简化形式,因为距离是垂直于线段的
  200. # 实际上,我们只需要y方向的偏移量,因为x方向的偏移量由斜率决定
  201. dx = distance / math.sqrt(1 + slope ** 2)
  202. dy = slope * dx
  203. # 计算新线段的起点和终点
  204. # 注意:这里我们假设距离是正值,只计算上方的平行线
  205. # 如果需要下方的平行线,可以将dy的符号取反
  206. new_start = (start[0] - dy / slope, start[1] + dx)
  207. new_end = (end[0] - dy / slope, end[1] + dx)
  208. # 注意:上面的计算方式在斜率为负时可能不正确,因为它没有考虑线段的方向
  209. # 一种更健壮的方法是分别计算与start和end距离为distance的点,然后取这两个点作为新线段的端点
  210. # 但为了简单起见,这里我们假设斜率不是负数或垂直线的情况已经处理
  211. # 注意:上面的计算方式在斜率为负或接近0时可能不准确,下面是一个更准确的版本
  212. # 它直接计算与start和end等距的点
  213. # 考虑到斜率可能接近无穷(即几乎垂直),我们使用向量叉积的概念来避免除以0
  214. vector = (end[0] - start[0], end[1] - start[1])
  215. norm = (-vector[1], vector[0]) # 法线向量
  216. norm_length = math.sqrt(norm[0] ** 2 + norm[1] ** 2)
  217. unit_norm = (norm[0] / norm_length, norm[1] / norm_length) # 单位法线向量
  218. offset_point_start = (start[0] + unit_norm[0] * distance, start[1] + unit_norm[1] * distance)
  219. offset_point_end = (end[0] + unit_norm[0] * distance, end[1] + unit_norm[1] * distance)
  220. return (offset_point_start, offset_point_end)
  221. def shrink_line(start, end, scale_factor):
  222. """
  223. 将线段缩小指定的倍数。
  224. 参数:
  225. start -- 原始线段的起点坐标,形式为(x, y)
  226. end -- 原始线段的终点坐标,形式为(x, y)
  227. scale_factor -- 缩小倍数,例如0.5表示缩小到原来的一半
  228. 返回:
  229. tuple -- 缩小后的线段的起点和终点坐标,形式为((new_start_x, new_start_y), (new_end_x, new_end_y))
  230. """
  231. # 计算原始线段的长度和方向向量
  232. length = ((end[0] - start[0]) ** 2 + (end[1] - start[1]) ** 2) ** 0.5
  233. direction = ((end[0] - start[0]) / length, (end[1] - start[1]) / length)
  234. # 计算缩小后的线段的长度
  235. new_length = length * scale_factor
  236. # 计算缩小后的线段的起点和终点
  237. new_start = start
  238. new_end = (start[0] + direction[0] * new_length, start[1] + direction[1] * new_length)
  239. return (new_start, new_end)
  240. # 计算两点之间的欧几里得距离
  241. def euclidean_distance(p1, p2) -> float:
  242. return math.sqrt((p2[0] - p1[0]) ** 2 + (p2[1] - p1[1]) ** 2)
  243. # 寻找距离最远的两个点
  244. def farthest_points(points):
  245. if len(points) < 2:
  246. raise ValueError("至少需要两个点")
  247. max_distance = -1 # 初始化为一个较小的值
  248. farthest_pair = (None, None)
  249. # 遍历所有点对
  250. for i in range(len(points)):
  251. for j in range(i + 1, len(points)):
  252. distance_ = euclidean_distance(points[i], points[j])
  253. if distance_ > max_distance:
  254. max_distance = distance_
  255. farthest_pair = (points[i], points[j])
  256. return farthest_pair
  257. def find_vector_on_d(A,d,route):
  258. x1 = A[0]+d*math.sin(math.pi-route)
  259. y1 = A[1]+d*math.cos(math.pi-route)
  260. x2 = A[0]-d*math.sin(math.pi-route)
  261. y2 = A[1]-d*math.cos(math.pi-route)
  262. return (x1,y1),(x2,y2)
  263. def find_point_on_segment(A, B, d):
  264. # 将 A 和 B 转换为 numpy 数组
  265. A = np.array(A)
  266. B = np.array(B)
  267. # 计算向量 AB
  268. AB = B - A
  269. # 计算 AB 的长度
  270. AB_length = np.linalg.norm(AB)
  271. # 检查 d 是否在 AB 的长度范围内
  272. if d < 0 or d > AB_length:
  273. raise ValueError("d is out of range of the segment AB")
  274. # 计算单位向量 AB_hat
  275. AB_hat = AB / AB_length
  276. # 计算向量 AP
  277. AP = d * AB_hat
  278. # 计算点 P 的坐标
  279. P = A + AP
  280. return P