wuxin542101186
wuxin542101186
2017-11-15 02:48

求指点下这段Python代码怎么改错?

  • python
  • 编程题

这段代码大致目的是希望通过初始的点A,点A'及所有的点B坐标,通过一个公式不断迭代计算的到所有点B'的坐标,我用的列表元素表示坐标值,报错后不知道该怎么修改了 刚学Python,问题浅薄,请多指教
代码如下
import math

#两点坐标距离公式
def sqrtpowsum(A,B):
return math.sqrt(math.pow(A[0]-B[0],2)+math.pow(A[1]-B[1],2)+math.pow(A[2]-B[2],2))

#型材拉伸量
L0e = 0.1
#初始点A坐标
Coordinate_A = [16.6588, 14.0349, 0.0]
#初始点A'坐标
Coordinate_A_quote = [16.6588, 14.0349, 300]
#每一轮点B坐标
Coordinate_B = [
[16.6603, 14.0594, -4.86946],
[16.6662, 14.1329, -9.72499],
[16.6763, 14.2555, -14.5686]
]
""" ========================
分割线下方内容请勿修改
========================
"""

#点A的三个坐标
A_x = Coordinate_A[0]
A_y = Coordinate_A[1]
A_z = Coordinate_A[2]
#点A'的三个坐标
A_quote_x = Coordinate_A_quote[0]
A_quote_y = Coordinate_A_quote[1]
A_quote_z = Coordinate_A_quote[2]
#计算点B的个数
n = len(Coordinate_B)
#该轮点B'的坐标
Coordinate_B_quote =[]

for i in range(n):
#该轮B点的三个坐标
B_x = Coordinate_B[i][0]
B_y = Coordinate_B[i][1]
B_z = Coordinate_B[i][2]
#计算L和l
L = sqrtpowsum(Coordinate_A,Coordinate_A_quote) +L0e
l = sqrtpowsum(Coordinate_A,Coordinate_B)
m = L/l
#计算该轮点B'的三个坐标
B_quote_x = m*B_x + (1-m)*A_x
B_quote_y = m*B_y + (1-m)*A_y
B_quote_z = m*B_z + (1-m)*A_z
#该轮点B’的坐标
Coordinate_B_quote[0] = B_quote_x
Coordinate_B_quote[1] = B_quote_y
Coordinate_B_quote[2] = B_quote_z

迭代

Coordinate_A = Coordinate_B
Coordinate_A_quote = Coordinate_B_quote
#输出该轮B'坐标
print("B'" + i + "=" + Coordinate_B_quote +"\n")
报错如下
Traceback (most recent call last):
File "E:/python code/trace code.py", line 44, in
l = sqrtpowsum(Coordinate_A,Coordinate_B)
File "E:/python code/trace code.py", line 5, in sqrtpowsum
return math.sqrt(math.pow(A[0]-B[0],2)+math.pow(A[1]-B[1],2)+math.pow(A[2]-B[2],2))
TypeError: unsupported operand type(s) for -: 'float' and 'list'

需要迭代计算的B点有好几百个,如果能优化一下就更好了

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答