core.py 11 KB

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