Hi, there
With a lot of help in this forum, I strated to use Python to accomplish something. Here I am looking for guide about how to improve my code speed.
This code is to determine the normal direction of a polygon with four points.
Every four points have 6 different orders and so i calculate them to find one with maximum value, which is the order I need. It works and however runs slowly when I use it to handle a lot of data.
path_value=[28817,29283,30676,29284,30677]
def order_nodes(path_value):
# print path_value
f9 = open('inf.txt', 'r')
alllines9 = f9.readlines()
f9.close()
x=[0,0,0,0,0,0] # save all coordinate values of these nodes into a
y=[0,0,0,0,0,0]
i=1
# get all coordinate values of these nodes
while i < len(path_value):
for line100 in alllines9:
line100=line100.strip()
line1000=line100.split(',')
# print line1000[0]
if float(path_value[i]) == float(line1000[0]):
x[i-1]=line1000[1]
y[i-1]=line1000[2]
i=i+1
# print "i=", i
# fetch all coordinate values of these nodes
x[-1]=x[0]
y[-1]=y[0]
# determine these order
total_area=[0,0,0,0,0,0]
total_order=[0,0,0,0,0,0]
# case 0
j=0
area=0
while j < len(x)-1:
area= area + (float(x[j])*float(y[j+1])-float(y[j])*float(x[j+1]))
j=j+1
total_area[0]=area
total_order[0]=[1,2,3,4]
# case 1
x1=x[:]
y1=y[:]
x1_temp=x1[2]
y1_temp=y1[2]
x1[2]=x1[3]
y1[2]=y1[3]
x1[3]=x1_temp
y1[3]=y1_temp
total_order[1]=[1,2,4,3]
j1=0
area1=0
while j1 < len(x1)-1:
area1= area1 + (float(x1[j1])*float(y1[j1+1])-float(y1[j1])*float(x1[j1+1]))
j1=j1+1
total_area[1]=area1
# case 2
x2=x[:]
y2=y[:]
x2_temp=x2[1]
y2_temp=y2[1]
x2[1]=x2[2]
y2[1]=y2[2]
x2[2]=x2_temp
y2[2]=y2_temp
total_order[2]=[1,3,2,4]
j2=0
area2=0
while j2 < len(x2)-1:
area2= area2 + (float(x2[j2])*float(y2[j2+1])-float(y2[j2])*float(x2[j2+1]))
j2=j2+1
total_area[2]=area2
# case 3
x3=x2[:]
y3=y2[:]
x3_temp=x3[2]
y3_temp=y3[2]
x3[2]=x3[3]
y3[2]=y3[3]
x3[3]=x3_temp
y3[3]=y3_temp
total_order[3]=[1,3,4,2]
j3=0
area3=0
while j3 < len(x3)-1:
area3= area3 + (float(x3[j3])*float(y3[j3+1])-float(y3[j3])*float(x3[j3+1]))
j3=j3+1
total_area[3]=area3
# case 4
x4=x[:]
y4=y[:]
x4_temp1=x4[1]
x4_temp2=x4[2]
y4_temp1=y4[1]
y4_temp2=y4[2]
x4[1]=x4[3]
x4[2]=x4_temp1
x4[3]=x4_temp2
y4[1]=y4[3]
y4[2]=y4_temp1
y4[3]=y4_temp2
total_order[4]=[1,4,2,3]
j4=0
area4=0
while j < len(x4)-1:
area4= area4 + (float(x4[j4])*float(y4[j4+1])-float(y4[j4])*float(x4[j4+1]))
j4=j4+1
total_area[4]=area4
# case 5
x5=x4[:]
y5=y4[:]
x5_temp=x5[2]
y5_temp=y5[2]
x5[2]=x5[3]
y5[2]=y5[3]
x5[3]=x5_temp
y5[3]=y5_temp
total_order[5]=[1,4,3,2]
j5=0
area5=0
while j < len(x5)-1:
area5= area5 + (float(x5[j5])*float(y5[j5+1])-float(y5[j5])*float(x5[j5+1]))
j5=j5+1
total_area[5]=area5
max_location = total_area.index(max(total_area))
node_order=total_order[max_location]
return node_order
node_order=order_slab_nodes(path_value)
Could you please comment my code? Thank you so much.
ning