Mar-15-2019, 07:35 PM
(Mar-15-2019, 01:22 PM)ichabod801 Wrote: You put your parameters in the wrong order on line 5.
Thanks! But in the scripts below. I meet a similar problem. The function works fine by its own, but will have an error in the "for loop". I don't know where goes wrong. The orders are correct. Could you help me? Thanks again!
# This is a script to generates layouts for rectangles. The result is expected to be a list of X,Y values. # Lx and Ly are the rectangles' X, Y value lists. Different levels represents different categories. # xgap and ygap are the gaps between rectangles # H is the list of heights for different categories. Lx = [[38.297,34.641,25.81,10.33,54.26],[48.99 ,25.82 ,36.51 ,40 ,51.64]] Ly = [[28.72,25.98,19.36,16.54,41.35],[36.74 ,19.36 ,27.39 ,30 ,38.73]] xgap = 56 ygap = 19 H = [73.35,111.37] # itn is the judge for if each rectangle should stay in the same column or start a new column. # itn2 is the counter. def MoveAcorHeight(lx, ly, xgap, ygap, H, itn = None, K = [(0,0)], itn2=0): #Set a special condition for one element lists. if len(lx) == 1: return K #If the list has more than one elements reset the counter. if len(lx) > 1 and itn2 == 0: itn = ly[0] + ygap + ly[itn2+1] itn2 +=1 return MoveAcorHeight(lx, ly, xgap, ygap, H, itn, K, itn2) #If the judge (itn) > H, start a new column. elif 0 < itn2 < len(lx) - 1 and itn > H : PrebsX = {} for i in K: PrebsX[i[0]] = [] for i in range(len(K)): PrebsX[K[i][0]].append(lx[i]) preJ = [] for i in PrebsX: preJ.append(max(PrebsX[i])) J = sum(preJ) + len(preJ) * xgap K.append((J, 0)) PrebsY = {} for i in K: PrebsY[i[0]] = [] for i in range(len(K)): PrebsY[K[i][0]].append(ly[i]) preI = PrebsY[K[-1][0]] I = sum(preI) + len(preI) * ygap itn2 += 1 itn = I + ly[itn2] + ygap return MoveAcorHeight(lx, ly, xgap, ygap, H, itn, K, itn2) # To deal with the last element. elif itn2 == len(lx) - 1 and itn > H: PrebsX = {} for i in K: PrebsX[i[0]] = [] for i in range(len(K)): PrebsX[K[i][0]].append(lx[i]) preJ = [] for i in PrebsX: preJ.append(max(PrebsX[i])) J = sum(preJ) + len(preJ) * xgap K.append((J, 0)) return K # If the judge (itn) <= H, rectangle stay in the same column. elif 0< itn2 < len(lx) - 1 and itn <= H: PrebsY = {} for i in K: PrebsY[i[0]] = [] for i in range(len(K)): PrebsY[K[i][0]].append(ly[i]) preI = PrebsY[K[-1][0]] I = sum(preI) + len(preI) * ygap K.append((K[itn2 - 1][0], -I)) itn2 += 1 itn = I + ly[itn2 - 1] + ygap + ly[itn2] return MoveAcorHeight(lx, ly, xgap, ygap, H, itn, K, itn2) # To deal with the last element. elif itn2 == len(lx) - 1 and itn <= H: PrebsY = {} for i in K: PrebsY[i[0]] = [] for i in range(len(K)): PrebsY[K[i][0]].append(ly[i]) preI = PrebsY[K[-1][0]] I = sum(preI) + len(preI) * ygap K.append((K[itn2 - 1][0], -I)) return K L = [] ### The script works fine when deal with one list, (L.append(MoveAcorHeight(Lx[0], Ly[0], xgap, ygap, H[0]))). But will have a "out of range" warning while running in the for loop. for i in range(len(Lx)): L.append(MoveAcorHeight(Lx[i], Ly[i], xgap, ygap, H[i])) print (L)