我有一个像这样的双循环:
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文件,这就是为什么我需要使三角形与顶点等
你可以试着把坐标倒过来——从而证明两个点彼此是对角线。你也可以用这个来检查对角线上是否有一个点。然后,把它们组合起来,检查在那个位置是否有一个点——如果有,你就得到了一个三角形。如
(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
您检查过matplotlib了吗?这里的答案是用于绘制多边形
这是我的代码,它可以工作,对于未来的情况,如果人们需要它的感觉自由,希望它可以帮助
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))