【发布时间】:2021-01-23 03:16:36
【问题描述】:
我有两个函数,第一个是 2D 曲线生成器,它有 7 个参数并且工作正常第二个函数主要基于第一个函数,但它在 3D 中工作,但是它不像我生成两个一样工作使用 3D 功能的不同曲线,当它们应该不同时,它们会出现相同的结果。举个例子:
curve_1 = build_curve_3D(0,10,10,2,4,6,5,8,9,10)
curve_2 = build_curve_3D(5,7,2,19,15,12,67,9,2,10)
这应该返回两组 3 个列表,所有包含唯一点,但这是此示例的输出
>>> print(curve_1)
([5, 6.400000000000005, 9.879999999999983, 14.03999999999999, 18.880000000000006, 24.4, 30.59999999999995, 37.48, 45.0399999999999, 53.280000000000136, 67], [7, 7.800000000000003, 9.119999999999996, 10.159999999999997, 10.92, 11.400000000000004, 11.6, 11.519999999999996, 11.160000000000007, 10.51999999999998, 9], [2, 3.0, 4.6, 5.800000000000002, 6.600000000000001, 7.0, 7.0, 6.6000000000000005, 5.8000000000000025, 4.6000000000000005, 2])
>>> print(curve_2)
([5, 6.400000000000005, 9.879999999999983, 14.03999999999999, 18.880000000000006, 24.4, 30.59999999999995, 37.48, 45.0399999999999, 53.280000000000136, 67], [7, 7.800000000000003, 9.119999999999996, 10.159999999999997, 10.92, 11.400000000000004, 11.6, 11.519999999999996, 11.160000000000007, 10.51999999999998, 9], [2, 3.0, 4.6, 5.800000000000002, 6.600000000000001, 7.0, 7.0, 6.6000000000000005, 5.8000000000000025, 4.6000000000000005, 2])
正如您所见,由于某种原因,这两条单独的曲线产生了相同的结果,所以我将所有代码贴出来,以便比较这两个函数并希望能解决问题。
import matplotlib.pyplot as plt
global curve_x , curve_y , curve_z
curve_x, curve_y, curve_z = [], [], []
def build_curve_2D(start_x, start_y, control_x, control_y, end_x, end_y, resolution):
curve_x.clear()
curve_y.clear()
control_line_x = []
control_line_y = []
guide_line_x = []
guide_line_y = []
curve_x.append(start_x)
curve_y.append(start_y)
for cycle in range(0,resolution):
guide_line_x.append(start_x + ((control_x - start_x)/resolution) * cycle)
guide_line_y.append(start_y + ((control_y - start_y)/resolution) * cycle)
control_line_x.append(control_x + ((end_x - control_x)/resolution) * cycle)
control_line_y.append(control_y + ((end_y - control_y)/resolution) * cycle)
for points in range(0,resolution -1):
curve_x.append((__find__intersection__(guide_line_x[points],guide_line_y[points],
control_line_x[points],control_line_y[points],
guide_line_x[points+1],guide_line_y[points+1],
control_line_x[points+1],control_line_y[points+1]))[0])
curve_y.append((__find__intersection__(guide_line_x[points],guide_line_y[points],
control_line_x[points],control_line_y[points],
guide_line_x[points+1],guide_line_y[points+1],
control_line_x[points+1],control_line_y[points+1]))[1])
curve_x.append(end_x)
curve_y.append(end_y)
return curve_x, curve_y
def build_curve_3D(start_x, start_y, start_z, control_x, control_y, control_z, end_x, end_y, end_z, resolution):
curve_x.clear()
curve_y.clear()
curve_z.clear()
curve_x.append(start_x)
curve_y.append(start_y)
curve_z.append(start_z)
control_line_x = []
control_line_y = []
control_line_z = []
guide_line_x = []
guide_line_y = []
guide_line_z = []
for cycle in range(0,resolution):
guide_line_x.append(start_x + ((control_x - start_x)/resolution) * cycle)
guide_line_y.append(start_y + ((control_y - start_y)/resolution) * cycle)
guide_line_z.append(start_z + ((control_z - start_z)/resolution) * cycle)
control_line_x.append(control_x + ((end_x - control_x)/resolution) * cycle)
control_line_y.append(control_y + ((end_y - control_y)/resolution) * cycle)
control_line_z.append(control_z + ((end_z - control_z)/resolution) * cycle)
for points in range(0,resolution -1):
curve_x.append((__find__intersection__(guide_line_x[points],guide_line_y[points],
control_line_x[points],control_line_y[points],
guide_line_x[points+1],guide_line_y[points+1],
control_line_x[points+1],control_line_y[points+1]))[0])
curve_y.append((__find__intersection__(guide_line_x[points],guide_line_y[points],
control_line_x[points],control_line_y[points],
guide_line_x[points+1],guide_line_y[points+1],
control_line_x[points+1],control_line_y[points+1]))[1])
curve_z.append((__find__intersection__(guide_line_x[points],guide_line_z[points],
control_line_x[points],control_line_z[points],
guide_line_x[points+1],guide_line_z[points+1],
control_line_x[points+1],control_line_z[points+1]))[1])
curve_x.append(end_x)
curve_y.append(end_y)
curve_z.append(end_z)
return curve_x, curve_y, curve_z
def __find__intersection__(x1,y1,x2,y2,x3,y3,x4,y4):
px = ((x1*y2-y1*x2)*(x3-x4)-(x1-x2)*(x3*y4-y3*x4))/((x1-x2)*(y3-y4)-(y1-y2)*(x3-x4))
py = ((x1*y2-y1*x2)*(y3-y4)-(y1-y2)*(x3*y4-y3*x4))/((x1-x2)*(y3-y4)-(y1-y2)*(x3-x4))
return px , py
class test():
def single_figure_test(curve,color):
figure = plt.figure('single figure')
ax = plt.axes(projection='3d')
ax.plot(curve[0],curve[1],curve[2],color)
plt.show()
def double_figure_test(curve_1,curve_2,color):
figure = plt.figure('double figure')
ax = plt.axes(projection='3d')
ax.plot(curve_1[0],curve_1[1],curve_1[2],color)
ax.plot(curve_2[0],curve_2[1],curve_2[2],color)
plt.show()
def duplicate_test(curve_1,curve_2):
for test in range(0,len(curve_1[0])):
if curve_1[0][test] == curve_2[0][test]:
print(curve_1[1][test],'- Duplicate Data Detected')
for test in range(0,len(curve_1[1])):
if curve_1[1][test] == curve_2[1][test]:
print(curve_1[0][test],'- Duplicate Data Detected')
for test in range(0,len(curve_1[2])):
if curve_1[2][test] == curve_2[2][test]:
print(curve_1[2][test],'- Duplicate Data Detected')
print('Test Completed')
【问题讨论】:
-
摆脱全局变量可能有助于解决问题。顺便说一句,
test类是如何使用的?可以分享minimal reproducible example吗? -
@AMC 在 shell 中使用测试类只是为了比较曲线,它只是暂时的