Python Tutorials | (back to the list of tutorials) |
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_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)
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.