123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- import math
- # 巷道间距
- # 计算旋转角度
- def calculate_route(point_1, point_2):
- a, b = point_1
- c, d = point_2
- return math.atan((d - b) / (c - a))
- # 计算旋转坐标
- def calculate_route_coordinate(point, route):
- x = point[0]
- y = point[1]
- x1 = x * math.cos(route) + y * math.sin(route)
- y1 = -x * math.sin(route) + y * math.cos(route)
- return (x1, y1)
- def find_point_on_line(A, B, fraction):
- """
- 计算线段AB上距离A点fraction长度处的点C的坐标。
- 参数:
- A -- 点A的坐标,形式为(x1, y1)
- B -- 点B的坐标,形式为(x2, y2)
- fraction -- 线段AB上距离A点的比例(0到1之间)
- 返回:
- 点C的坐标,形式为(x, y)
- """
- x1, y1 = A
- x2, y2 = B
- # 使用线性插值计算C点的坐标
- x = x1 + (x2 - x1) * fraction
- y = y1 + (y2 - y1) * fraction
- return (x, y)
- # 计算 a点到b点之间1/x处(距离a点)的c点坐标
- def calculate_point_c(point_a, point_b, x):
- x_a, y_a = point_a
- x_b, y_b = point_b
- # 计算方向向量
- dx = x_b - x_a
- dy = y_b - y_a
- # 计算方向向量的模长
- distance_ab = math.sqrt(dx ** 2 + dy ** 2)
- # 计算单位方向向量
- unit_vector_x = dx / distance_ab
- unit_vector_y = dy / distance_ab
- # 计算点C的坐标
- x_c = x_a + (1 / x) * unit_vector_x
- y_c = y_a + (1 / x) * unit_vector_y
- return x_c, y_c
- # 找线段之间某点的坐标
- def rotate_point_around_another(point_a, point_b, a):
- x_a, y_a = point_a
- x_b, y_b = point_b
- """
- 旋转点A(x_a, y_a)围绕点B(x_b, y_b) a 弧度后的新坐标。
- """
- # 将A点的坐标转换到以B点为原点的坐标系中
- dx = x_a - x_b
- dy = y_a - y_b
- # 旋转坐标
- x_prime = dx * math.cos(a) - dy * math.sin(a)
- y_prime = dx * math.sin(a) + dy * math.cos(a)
- # 将旋转后的坐标转换回原来的坐标系
- x_rotated = x_prime + x_b
- y_rotated = y_prime + y_b
- return x_rotated, y_rotated
- def calculate_angle_with_x_axis(point1, point2):
- x1, y1 = point1
- x2, y2 = point2
- """
- 计算线段(x1, y1)到(x2, y2)与x轴的夹角(以弧度为单位)。
- 结果角度是从x轴正方向逆时针旋转到线段方向所经过的角度。
- """
- # 首先计算线段的斜率
- if x1 == x2: # 避免除以零
- if y2 > y1:
- return math.pi / 2 # 垂直于x轴且向上
- elif y2 < y1:
- return -math.pi / 2 # 垂直于x轴且向下
- else:
- return 0 # 与x轴重合
- else:
- slope = (y2 - y1) / (x2 - x1)
- # 使用atan2计算角度,它返回从x轴正方向到点(y, x)的向量与x轴之间的夹角
- # 注意:atan2的参数顺序是(y的差, x的差),即(y2-y1, x2-x1)
- angle_radians = math.atan2(y2 - y1, x2 - x1)
- return angle_radians
- def get_color_by_layer(layer_id):
- return {
- '1': 53,
- '2': 83,
- '3': 133,
- '4': 173
- }.get(layer_id)
- def min_distance_between_segments(seg1, seg2):
- """
- 计算两条线段之间的最小距离(基于端点之间的最小距离)
- seg1, seg2: 线段的端点坐标,例如 [(x1, y1), (x2, y2)]
- """
- # 提取端点
- p1, p2 = seg1
- p3, p4 = seg2
- # 计算所有端点之间的距离
- dist1 = distance(p1, p3)
- dist2 = distance(p1, p4)
- dist3 = distance(p2, p3)
- dist4 = distance(p2, p4)
- # 返回最小距离
- return min(dist1, dist2, dist3, dist4)
- def distance(p1, p2):
- """计算两点之间的距离"""
- return math.sqrt((p2[0] - p1[0]) ** 2 + (p2[1] - p1[1]) ** 2)
|