我认为你正在处理的是一个旅行推销员问题(TSP)(这里是博士课程的幻灯片,讨论如何尝试解决它)和最小化激光压力的路径是一个最小化移动它所需的力和力的变化,因此它是具有最小曲率的路径,所以我认为最好的方法是用圆弧圆角化 3 对点之间的路径。
可以在此处找到有关如何计算通过 3 个点的圆的参数的示例
我对 C# 不是很流利,所以我将在 Python 中添加一个实现,希望你也觉得它有用。
这个想法是,对于点 A、B、CI 的每个三元组,找到通过这 3 个点的圆弧,并且该弧将是连接 B 和 C 的路径。
我还没有时间测试这个,所以可能有一些错误的迹象。
# Initial points
points = [(1,1),(2,3),(5,3),(-4.1),(12,3)]
#List of point in the order find by the solution of the TSP
spl = tsp_solve(points) # generic function to solve the TSP
# Append the first two point of the list so that I can iterate over the list
# and parse every triplet of points in the same way.
spl = spl + spl[:2]
# The list where will be added every path that connect the points
paths = []
# For each tirplets of sequential points
for A,B,C in zip(spl[:-2],spl[1:-1],spl[2:]):
# Calculate the angular coefficent of the two line that pass on A,B and B,C
coeff_ab = (B[1] - A[1]) / (B[0] - A[0])
coeff_bc = (C[1] - B[1]) / (C[0] - B[0])
# If the two line have the same coeff then all the 3 point are on the same line
# and therfore the best path is that line.
if(coeff_ab == coeff_bc):
offset_y = A[1] - coeff_ab * A[0]
delta_x = C[0] - B[0]
paths.append({"type":"Line","coeff":coeff_ab,"offset_y":offset_y,"deta_x":delta_x})
continue
# Calculate the x of the center of the circle
center_x = coeff_ab *coeff_bc *(C[0]-A[0])
center_x += coeff_ab *(B[0]+C[0])
center_x -= coeff_bc *(A[0]+B[0])
center_x /= 2*(coeff_ab - coeff_bc)
# Calculate the y of the center of the circle
center_y = (A[1]+B[1)/2
center_y -= (center_x - (A[0] + B[0])/2)
center_y /= coeff_bc
radius = sqrt(center_x**2 + center_y**2)
paths.append({"type":"Circle","Radius":radius,"center_x":center_x,"center_y":center_y})
# Function To Calculate the X and Y of the lines and circles.
def calculate_circle_x(circle,time):
"""Function that return the x of a circle at a given time"""
time = time + circle["time_off"]
return circle["radius"] * cos(2*pi*time) + circle["center_x"]
def calculate_circle_y(circle,time):
"""Function that return the y of a circle at a given time"""
time = time + circle["time_off"]
return circle["radius"] * sin(2*pi*time) + circle["center_y"]
def calculate_line_x(line,time):
"""Function that return the x of a line at a given time"""
time = (line['delta_x']*time) + line["time_off"]
return time
def calculate_line_y(line,time):
"""Function that return the y of a line at a given time"""
time = (line['delta_x']*time) + line["time_off"]
return time * line["coeff"] + line['offset_y']
def calculate_x(obj,time):
"""Function that return the x of whatever it's passed"""
if(obj['type'] == 'Circle'):
return calculate_circle_x(obj,time)
else:
return calculate_line_x(obj,time)
def calculate_y(obj,time):
"""Function that return the y of whatever it's passed"""
if(obj['type'] == 'Circle'):
return calculate_circle_y(obj,time)
else:
return calculate_line_y(obj,time)
# Calculate some sample of the global path to plot it or do whatever with it.
number_of_sample = 100000
path_points = []
number_of_paths = len(paths)
# Calculate some time equidistant point's sample
for i in range(number_of_sample):
# Calculate the global time
global_time = i*number_of_paths/number_of_sample
# Calculate in which path the point it is
path_number = int(global_time)
# Calculate which time of the path it is
local_time = global_time - path_number
path = paths[path_number]
# Calculate the sampled point
new_point = (calculate_x(path,local_time),calculate_y(path,local_time))
# Add the sampled point to the path_points list
path_points.append(new_point)
# Print the result of the path point sampled.
print(path_points)
现在您有了积分或至少有关于如何计算积分的示例,您可以将其转换为 C#。我试图对它进行很多评论,以便即使您不了解 Python 也能理解它。