One-Dimensional Array Query

Hello,

I have a simple attractor script. When I append the ‘i’ and ‘j’ loop values to tuples in the main list, I am unable to use the tuples as point3D objects. I thought that many functions that required 3d point objects would also accepts tuples.

Anyway, when I create another for loop and choose the ‘ptlist[i]’, it somehow transforms the tuples into point3D objects. This, I don’t understand.

import rhinoscriptsyntax as rs
#import random

ptList = []
rs.EnableRedraw(False)

def pointMatrix():
    iMax = 20
    jMax = 20
    k = 0
    
    testPt = rs.GetObject('select Attractor pt', rs.filter.point) 
    
    #loop to create list of short values
    for i in range(iMax):
        for j in range(jMax):
            
            x = i
            y = j
            z = k
            ptList.append([x,y,z])
            
            
    #loop to create circles
    for i in range(len(ptList)):
        distance = rs.Distance(testPt, ptList[i])
        radius = distance / 20
        
        if radius > .5:
            radius = .5
        elif radius < .125:
            radius = .125
        
        
        rs.AddCircle(ptList[i], radius)
        print(ptList[i])
        #here the tuples are now point3D objects or read that way
    
pointMatrix()

Incidentally, I am attempting to create a two-dimensional matrix with this code. Is this simple? Should I define a two- dimensional matrix like this?

grid = [[0]*8 for n in range(8)]

for row in grid:
    for col in row:

No, they are still lists (not tuples). Nothing has changed them into 3d point objects. Try the following:

import rhinoscriptsyntax as rs
pt_a=rs.coerce3dpoint([0,0,0])
pt_b=[0,0,0]
pt_c=(0,0,0)
print "pt_a is a {}".format(type(pt_a))
print "pt_b is a {}".format(type(pt_b))
print "pt_c is a {}".format(type(pt_c))

>>>
pt_a is a <type 'Point3d'>
pt_b is a <type 'list'>
pt_c is a <type 'tuple'>

BTW,
Tuples look like this
(x,y,z)

Lists look like this:
[x,y,z]

Tuples are immutable - you cannot change the value of their elements
List elements can be changed.

HTH, --Mitch

Thank you Helvetosaur,

I understand and I appreciate the corrections; however, I still am not certain as to why this segment of the code does not return acceptable lists.

I assume it is because they are of incremented lengths? I would assume that the ‘i’ and ‘j’ for loops would return a 2-dimensional array; however, it seems that the third for loop translates the incremented list into a one dimensional list.

In the above suggestions are you implying to replace:

ptList.append([x,y,z])

with

ptList = rs.coerce3dpoint(x,y,z)]

If you already answered this, I apologize. Here is the code that is returning lists of incrementing lengths which are confusing. Thank you for your help.

import rhinoscriptsyntax as rs
import random

ptList = []
rs.EnableRedraw(False)

def pointMatrix():
    iMax = 20
    jMax = 20
    k = 0
    
    testPt = rs.GetObject('select Attractor pt', rs.filter.point) 
    
    #loop to create list of short values
    for i in range(iMax):
        for j in range(jMax):
            
            x = i
            y = j
            z = k
            ptList.append([x,y,z])
            print(ptList)

pointMatrix()

No, that is not the case… Why? Because in the above, you are still only creating one flat list - every “point” is simply appended to the main “ptList” in the same way. If you are trying to create a nested list, you would still use a double for loop, but you would need to go about it in a different manner:

def PointMatrix(i_max,j_max,k):
    #create empty main list
    main_list = []
    for i in range(i_max):
        #on each i loop iteration, create new empty sublist
        sub_list=[]
        for j in range(j_max):
            x = i
            y = j
            z = k
            #append j loop points to sublist
            sub_list.append([x,y,z])
        #at the end of each i iteration, append the "j" sublist to the main list
        main_list.append(sub_list)
    return main_list

for item in PointMatrix(20,20,0): print item

Edit: for compactness, instead of x=, y=, z=, you also can assign point values directly like this:

        for j in range(j_max):
            #append j loop points to sublist
            sub_list.append([i,j,k])

Hope this helps, --Mitch

Ahh ok this makes sense.
It is cleared up. As always a huge help! Thanks again for posting the example.

Erik