用Python連接頂點以製作三角形

2024-09-30 04:31:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个双循环,看起来是这样的:

for i in xrange(len(myVVV)):
    for j in xrange(len(myVVV[i])):
        if myVVV[i][j]!= -1:
              s=i,j
              print(myVVV[i][j])
              print(s)

给出了:

^{pr2}$

我试着通过检查s代表网格上数字点位置的点来创建“三角形”,基本上我会连接点1,3,4,根据那里的位置形成一个三角形。有什么办法吗?在

我能读的东西?我正在创建一个刻面文件,这就是为什么我需要用顶点等三角形


Tags: in网格forlenif代表数字print
3条回答

这是我的代码,它很管用,对于以后的情况如果人们需要它请放心,希望它能有所帮助

MTri=0 #num of triangles 
numI=len(myVVV)#to check in range of i
numJ=len(myVVV[0])# to check in range of j
idsT=0 #for triangles
for i in xrange(len(myVVV)):
#print(myVVV[i])
for j in xrange(len(myVVV[i])):
    if myVVV[i][j] != -1:
        # line below check that if point has neighboring
        #points open to make a sqaure makeing it counter clockwise 
        #-> right, ^ up, <- left, v down (first case)
        if i-1 >= 0 and j+1 < numJ and myVVV[i][j+1] !=-1 and myVVV[i-1][j+1] !=-1 and myVVV[i-1][j] != -1:
            MTri= MTri +2#plus 2 since 2 traingles are made from a square
            A=myVVV[i][j]
            B=myVVV[i][j+1]
            C=myVVV[i-1][j]
            D=myVVV[i-1][j+1] 
            idsT=idsT+1
            #A,B,D create -> right ,^ up, back   _|
            #A,D,C create corner , <- left, back |/  
            print("create Triangle from points" + ' '+str(A)+' '+ str(B)+' '+ str(D))
            openwrite.write(str(idsT)+' '+str(A)+ ' '+str(B)+' '+str(D)+'\n')
            idsT=idsT+1
            openwrite.write(str(idsT)+' '+str(A)+ ' '+str(D)+' '+str(C)+'\n')
            print("create Triangle from points" + ' '+str(A)+' '+ str(D)+' '+ str(C))
        elif i-1 >= 0 and j+1 < numJ and myVVV[i][j+1] != -1 and myVVV[i-1][j+1] != -1:
            MTri= MTri+1#plus 1 
            A=myVVV[i][j]
            B=myVVV[i][j+1]# same index ,j shift -> one to the right
            C=myVVV[i-1][j+1] # index above, j shifted -> one to the right
            #A,B,C creates ->right,^ up, back  _|
            idsT=idsT+1
            openwrite.write(str(idsT)+' '+str(A)+ ' '+str(B)+' '+str(C)+'\n')
            print("create Triangle from points" + ' '+str(A)+' '+ str(B)+' '+ str(C))

        elif i-1 >= 0 and j+1 < numJ and myVVV[i][j+1] != -1 and myVVV[i-1][j] != -1:
            MTri= MTri+1#plus 1 
            A=myVVV[i][j]
            B=myVVV[i][j+1] #same index ,j shift -> one to the right
            C=myVVV[i-1][j]  #index above, same j position 
            #A,B,C creates ->right,corner, back  |_
            idsT=idsT+1
            openwrite.write(str(idsT)+' '+str(A)+ ' '+str(B)+' '+str(C)+'\n')
            print("create Triangle from points" + ' '+str(A)+' '+ str(B)+' '+ str(C))


        elif i-1 >= 0 and j+1 < numJ and myVVV[i-1][j+1] != -1 and myVVV[i-1][j] != -1:
            MTri= MTri+1#plus 1 
            A=myVVV[i][j]
            B=myVVV[i-1][j+1] # index above, j shifted -> one to the right
            C=myVVV[i-1][j]  #index above, same j position 
            #A,B,C creates corner right, left back  |/
            idsT=idsT+1
            openwrite.write(str(idsT)+' '+str(A)+ ' '+str(B)+' '+str(C)+'\n')
            print("create Triangle from points" + ' '+str(A)+' '+ str(B)+' '+ str(C))

你查过matplotlib了吗?这个答案here是用来画多边形的。

你可以试着反转坐标,从而证明两个点是对角的。你也可以用这个来检查对角线位置是否有一个点。然后,把它们组合起来,检查那个位置是否有一个点-如果有,你就有一个三角形。 例如

(0, 1) // One dot
Invert it to check for surrounding dots
(1, 0) // Dot found (inverted 0, 1)
(1, 1) // Combination - check if dot exists there. if true - triangle!

只是一个建议,可能会阻止一些想法到正确的道路:)我只是想到了这个,所以如果你找到一个洞,就放轻松点:p

相关问题 更多 >

    热门问题