在Python中连接顶点形成三角形

1 投票
3 回答
1179 浏览
提问于 2025-04-18 11:39

我有一个双重循环,代码大概是这样的:

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)

运行后得到这个结果:

1
(0, 1)
2
(0, 5)
3
(1, 0)
4
(1, 1)
5
(1, 2)
6
(1, 4)
7
(1, 5)
8
(1, 6)
9
(2, 0)
10
(2, 1)
11
(2, 3)
12
(2, 4)
13
(3, 2)
14
(3, 3)
15
(3, 6)
16
(3, 7)
17
(4, 6)
18
(4, 7)

我想通过检查点的位置来创建“三角形”。这里的s表示在网格上数字点的位置,简单来说,我会根据这些点的位置连接点1、3、4来形成一个三角形。那么,有什么好的方法可以做到这一点吗?

我可以阅读哪些资料呢?我正在创建一个facet文件,所以我需要用顶点等来制作三角形。

3 个回答

0

你有没有听说过matplotlib?这个回答 在这里 是关于如何用matplotlib画多边形的。

0

你可以试着把坐标反转,这样就能证明两个点是对角线上的。你也可以用这个方法来检查那个对角线位置上是否有点。然后,把它们结合起来,检查那个位置上是否有点——如果有的话,你就得到了一个三角形。

(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

0

这是我的代码,它可以正常工作。如果将来有人需要,可以随意使用,希望它能帮到你们。

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))

撰写回答