python
import math
import numpy as np
class LineSegment:
def __init__(self, start_point, end_point):
self.start = np.array(start_point)
self.end = np.array(end_point)
class CircularArc:
def __init__(self, center, radius, start_angle, end_angle):
self.center = np.array(center)
self.radius = radius
self.start_angle = start_angle # 弧度制
self.end_angle = end_angle # 弧度制
class EllipticalArc:
def __init__(self, center, radius_x, radius_y, rotation_angle, start_angle, end_angle):
self.center = np.array(center)
self.radius_x = radius_x
self.radius_y = radius_y
self.rotation_angle = rotation_angle # 弧度制
self.start_angle = start_angle # 弧度制
self.end_angle = end_angle # 弧度制
class Polygon:
def __init__(self, edges):
self.edges = edges
def distance_to_line_segment(point, segment):
p = np.array(point)
a = segment.start
b = segment.end
ab = b - a
t = np.dot(p - a, ab) / np.dot(ab, ab)
t = np.clip(t, 0.0, 1.0)
projection = a + t * ab
dist = np.linalg.norm(p - projection)
return dist
def distance_to_circular_arc(point, arc):
p = np.array(point)
c = arc.center
r = arc.radius
v = p - c
d = np.linalg.norm(v)
if d == 0.0:
# 点在圆心上,距离为半径
return r
else:
phi = math.atan2(v[1], v[0])
# 规范化角度到 [0, 2π)
start_angle = arc.start_angle % (2 * np.pi)
end_angle = arc.end_angle % (2 * np.pi)
phi = phi % (2 * np.pi)
# 判断角度是否在弧段范围内
if start_angle <= end_angle:
inside_arc = start_angle <= phi <= end_angle
else:
inside_arc = phi >= start_angle or phi <= end_angle
if inside_arc:
# 最近点在弧上
dist = abs(d - r)
else:
# 最近点为弧的起点或终点
point1 = c + r * np.array([math.cos(arc.start_angle), math.sin(arc.start_angle)])
point2 = c + r * np.array([math.cos(arc.end_angle), math.sin(arc.end_angle)])
dist1 = np.linalg.norm(p - point1)
dist2 = np.linalg.norm(p - point2)
dist = min(dist1, dist2)
return dist
def distance_to_elliptical_arc(point, arc):
# 椭圆弧的距离计算较为复杂,此处可进行近似或使用数值方法
# 为简化,我们暂时返回一个占位值
# TODO: 实现点到椭圆弧的距离计算
return float('inf')
def minimal_distance_to_polygon(point, polygon):
min_dist = float('inf')
for edge in polygon.edges:
if isinstance(edge, LineSegment):
dist = distance_to_line_segment(point, edge)
elif isinstance(edge, CircularArc):
dist = distance_to_circular_arc(point, edge)
elif isinstance(edge, EllipticalArc):
dist = distance_to_elliptical_arc(point, edge)
else:
continue
if dist < min_dist:
min_dist = dist
return min_dist
def angle_between_vectors(v1, v2):
# 计算两个向量之间的有向角度
angle = math.atan2(np.cross(v1, v2), np.dot(v1, v2))
return angle
def angle_contribution_from_circular_arc(point, arc):
p = np.array(point)
c = arc.center
v1 = c + arc.radius * np.array([math.cos(arc.start_angle), math.sin(arc.start_angle)]) - p
v2 = c + arc.radius * np.array([math.cos(arc.end_angle), math.sin(arc.end_angle)]) - p
angle = angle_between_vectors(v1, v2)
# 根据弧的方向调整角度(顺时针或逆时针)
# 如果弧的起始角度大于终止角度,我们假设其方向为顺时针,角度取负值
if (arc.end_angle - arc.start_angle) % (2 * np.pi) < np.pi:
# 逆时针方向
return angle
else:
# 顺时针方向
return -angle
def winding_number(point, polygon):
wn = 0 # 绕数计数器
p = np.array(point)
for edge in polygon.edges:
if isinstance(edge, LineSegment):
v1 = edge.start - p
v2 = edge.end - p
angle = angle_between_vectors(v1, v2)
wn += angle
elif isinstance(edge, CircularArc):
angle = angle_contribution_from_circular_arc(point, edge)
wn += angle
elif isinstance(edge, EllipticalArc):
# TODO: 实现椭圆弧的角度贡献计算
pass
# 总角度除以 2π,得到绕数
wn = wn / (2 * np.pi)
# 如果绕数的绝对值大于等于 0.5,则认为在内部
if abs(wn) >= 0.5:
return 1
else:
return 0
def is_point_inside_polygon(point, polygon):
wn = winding_number(point, polygon)
return wn != 0
def signed_distance_to_polygon(point, polygon):
dist = minimal_distance_to_polygon(point, polygon)
inside = is_point_inside_polygon(point, polygon)
if inside:
return -dist
else:
return dist
# 示例
if __name__ == "__main__":
# 定义多边形
edges = [
LineSegment([0, 0], [1, 0]),
CircularArc([1, 1], 1, -0.5 * np.pi, 0),
LineSegment([2, 1], [2, 0]),
LineSegment([2, 0], [0, 0])
]
polygon = Polygon(edges)
# 测试点
test_point = [1, 0.5]
# 计算符号距离并判断点的位置
dist = signed_distance_to_polygon(test_point, polygon)
if dist < 0:
print(f"点 {test_point} 在多边形内部,距离为 {dist}")
else:
print(f"点 {test_point} 在多边形外部,距离为 {dist}")