我在这个脚本的某个地方有一个错误,它应该生成一个渐变。当深度等于差异时,渐变是完美的,但浮点增量似乎会将其搞砸。我一直盯着这段代码,我看不出答案。
这段代码出了什么问题?
def interpolate(s, e, n):
start = list(s)
end = list(e)
incrementlst = []
for i in range(0,len(start)):
diff = int(end[i]) - int(start[i])
if diff == 0:
increment = 0.0
else:
increment =diff/n
incrementlst.append(increment)
return incrementlst
def incrementedValue(s, i, n):
start = list(s)
increment = list(i)
n = n-1
finallst = [0,0,0,0]
if n < 1:
return start
for i in range(0,len(start)):
finallst[i] = start[i] + (n*(increment[i]))
return finallst
def formatIncrementedValue(l):
cmykList = list(l)
formattedString = str(int(round(cmykList[0], 0))) + " " + str(int(round(cmykList[1], 0))) + " " + str(int(round(cmykList[2], 0))) + " " + str(int(round(cmykList[3], 0)))
return formattedString
# Get user inputs.
depth = int(ca_getcustomvalue ("depth", "0"))
start = ca_getcustomvalue("start", "0")
end = ca_getcustomvalue("end", "0")
startlst = start.split(" ")
startlst = [int(i) for i in startlst]
endlst = end.split(" ")
endlst = [int(i) for i in endlst]
# draw a line and incrementally change the pen colour towards the end colour
colorlst = interpolate(startlst, endlst, depth)
for i in range(1,depth-1):
color = formatIncrementedValue(incrementedValue(startlst, colorlst, i))
#Draw line at correct offset in colour "color"
答案 0 :(得分:2)
此:
increment =diff/n
正在进行整数除法,例如,如果diff
为3且n
为2,则得1,而不是1.5。
使表达式浮动,以解决此问题:
increment = float(diff) / n