|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
以下是我代码的核心部分,当number为1的时候有1层循环,2的时候有两层循环,以此类推,每层循环中又有自身的循环,我需要如何简化我的代码,这样写下去我觉得越来越复杂
希望能得到好心人的回复,多谢!
if math.floor(xstart)==math.floor(xend) and math.floor(ystart)==math.floor(yend):
way = dis(xstart, xend, ystart, yend, zstart, zend)
else:
(boundx,boundy) = bound(xstart, xend, ystart, yend)
number = len(boundx)
for i in range(number):
zmay.append(zdep)
if number==1:
for i1 in range(len(zdep)):
way0 = dis(xstart, boundx[0], ystart, boundy[0], zstart, zmay[0][i1])
way1 = dis(boundx[0], xend, boundy[0], yend, zmay[0][i1], zend)
time0 = time(way0, max(zmay[0][i1], zstart))
time1 = time(way1, max(zmay[0][i1], zend))
waysum.append(way0+way1)
timesum.append(time0+time1)
waypos.append((i1))
elif number==2:
for i1 in range(len(zdep)):
for i2 in range(len(zdep)):
way0 = dis(xstart, boundx[0], ystart, boundy[0], zstart, zmay[0][i1])
way1 = dis(boundx[0], boundx[1], boundy[0], boundy[1], zmay[0][i1], zmay[1][i2])
way2 = dis(boundx[1], xend, boundy[1], yend, zmay[1][i2], zend)
time0 = time(way0, max(zmay[0][i1], zstart))
time1 = time(way1, max(zmay[0][i1], zmay[0][i2]))
time2 = time(way2, max(zmay[0][i2], zend))
waysum.append(way0+way1+way2)
timesum.append(time0+time1+time2)
waypos.append((i1,i2))
elif number==3:
for i1 in range(len(zdep)):
for i2 in range(len(zdep)):
for i3 in range(len(zdep)):
way0 = dis(xstart, boundx[0], ystart, boundy[0], zstart, zmay[0][i1])
way1 = dis(boundx[0], boundx[1], boundy[0], boundy[1], zmay[0][i1], zmay[1][i2])
way2 = dis(boundx[1], boundx[2], boundy[1], boundy[2], zmay[1][i2], zmay[2][i3])
way3 = dis(boundx[2], xend, boundy[2], yend, zmay[2][i3], zend)
time0 = time(way0, max(zmay[0][i1], zstart))
time1 = time(way1, max(zmay[0][i1], zmay[0][i2]))
time2 = time(way2, max(zmay[0][i2], zmay[0][i3]))
time3 = time(way3, max(zmay[0][i3], zend))
waysum.append(way0+way1+way2+way3)
timesum.append(time0+time1+time2+time3)
waypos.append((i1,i2,i3)) |
|