1

i have a double loop that looks like so:

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)

gives this:

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)

I'm trying to creating "triangles" by checking the dots the s represents the location of the number point on a grid, basically i would connect points 1,3,4 to make one triangle based on there location. So any ideas on how to go about this?

things i can read? Im creating a facet file which is why i need to make triangles with vertexes and such

CDspace
  • 2,639
  • 18
  • 30
  • 36
learner
  • 137
  • 1
  • 1
  • 6
  • Are you trying to find points that are "closest together" to make your triangles? Are your points in a plane, or 3D? You might find the link at http://www.blendernation.com/2012/01/12/python-script-point-cloud-skinner/ helpful - it sounds like you are trying to create a mesh from a point cloud. – Floris Jul 07 '14 at 18:50
  • @Floris thanks for the respond,however; i've figured it. thank you though – learner Jul 07 '14 at 21:11
  • that's good. Did you know you can write your own answer - or delete the question if it is no longer relevant? Leaving it open but not needed is neither one nor the other - it may end up wasting people's time. – Floris Jul 07 '14 at 21:25

3 Answers3

0

You could possibly try inverting the coords - thus proving 2 dots are diagonal from eachother. You could use this also to check if there is a dot in that diagonal position. Then, combine them and check if there is a dot in that location - if there is, you have a triangle. Eg

(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!

Just a suggestion which might stem some thought onto the correct path :) I just thought of this so go easy if you find a hole :P

Byron Coetsee
  • 3,533
  • 5
  • 20
  • 31
0

Have you checked out matplotlib? This answer here is for drawing polygons.

Community
  • 1
  • 1
walexnelson
  • 424
  • 2
  • 10
0

This is my code, it works, for future cases if people need it feel free , hope it may help

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))
learner
  • 137
  • 1
  • 1
  • 6