home processing download documents tutorial python tutorial gallery source about
 Python Tutorials (back to the list of tutorials)

Variable-Length Arrays

     ArrayList

One issue of arrays in Java is that its length needs to be determined when the variable is declared, and you cannot change the length later.

In the occasion where you want to change the length of array, you can use Java's built-in ArrayList class. When you use this class, the way you specify the type of array is very different. Instead of putting [ ] at the end of type/class name like IVec[ ], you put the type/class name after ArrayList inside < > , like ArrayList< IVec > . See the example below.

add_library('igeo')

size( 480, 360, IG.GL )

pointList = [] # empty list

# adding vectors
pointList.append(IVec(0,0,0))
pointList.append(IVec(20,0,0))
pointList.append(IVec(40,0,0))
pointList.append(IVec(60,0,0))

# size() gives the length of the array 
for i in range(len(pointList)) :
    IPoint( pointList[i] ) #access i-th member

# insert a vector at index 1.
pointList.insert( 1, IVec(0, 0, 30) )

# remove a vector at index 2.
pointList.pop( 2 )

# replace the vector at index 3 with a new one.
pointList[3] = IVec(60, 0, 30)

# creating a curve. 
ICurve(pointList).clr(1.,0,0)

The class ArrayList has several methods to manipulate member of the array content .

  • add( member ) : To add a new member at the end.
  • add( index, member ) : To insert a new member at the specified index. The existing member at and after the index is all pushed back by 1.
  • set( index, member ) : To replace the existing member at the specified index with a new member.
  • remove( index ) : To remove the member at the specified index.
  • get( index ) : To get the member at the specified index.
  • size() : To get the length of the array.
  • toArray( fixed_array_with_same_length_or_less ) : To convert variable-length array to a fixed length array.
For more detailed description about ArrayList, please see Java's official reference (JavaDoc) of ArrayList.

     2 Dimensional Variable-Length Arrays

2 dimensional variable-length array of IVec can be written with ArrayList in the following way. ArrayList < ArrayList < IVec > > . It has a nested description because it's creating a variable-length array of variable-length arrays. See the example below for the usage.

add_library('igeo')

size( 480, 360, IG.GL )

ptlist2 = []

for i in range(100) :
    ptlist2.append([])
    num = 0
    if i%11 == 0 :
        num = 110
    elif i%8 == 0 :
        num = 80
    elif i%7 == 0 :
        num = 70
    elif i%5 == 0 :
        num = 50
    elif i%4 == 0 :
        num = 40
    elif i%3 == 0 :
        num = 30
    elif i%2 == 0 :
        num = 20

    for j in range(num) :
        ptlist2[i].append( IVec( j, 50-i, j*j*0.01 ) )

# create curves
for i in range(len(ptlist2)) : 
    # don't create curve if number of points is less than 2
    if len(ptlist2[i]) > 1 :
        ICurve(ptlist2[i]).clr(i*0.01)


     Sort

IVec contained in ArrayList can be sorted by ISort class.

add_library('igeo')

size( 480, 360, IG.GL )

pts = []

for i in range(100) :
    p = IRandom.pt(-80,-80,0,80,80,0)
    pts.append(p)

for p in pts : # extract each member out of the list
    IPoint(p)

# sort points in x direction (blue)
ISort.sort(pts, IXComparator())
ICurve(pts).clr(0,0,1.)

# sort points in y direction (cyan)
ISort.sort(pts, IYComparator())
ICurve(pts).clr(0,1.,1.)

# sort points in radial direction (red)
ISort.sort(pts, IRadialComparator())
ICurve(pts).clr(1.,0,0)

IRandom.pt() is creating IVec with random location within the minimum and the maximum x-y-z range given in the arguments. For the use of randomness with IRandom class, please see the tutorial section about randomness.

To use ISort.sort() you need to put an array in the form of ArrayList< IVec > and, a comparator class which defines the order in which the array member is to be sorted. Known comparators are IXComparator, IYComparator, IZComparator, IDirectionalComparator, IRadialComparator, and IDistanceComparator.


(back to the list of tutorials)

HOME
FOR PROCESSING
DOWNLOAD
DOCUMENTS
TUTORIALS (Java / Python)
GALLERY
SOURCE CODE(GitHub)
PRIVACY POLICY
ABOUT/CONTACT