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.
import processing.opengl.*; import igeo.*; size( 480, 360, IG.GL ); ArrayList< IVec > pointList = new ArrayList< IVec >(); //empty array // adding vectors pointList.add( new IVec(0,0,0) ); pointList.add( new IVec(20,0,0) ); pointList.add( new IVec(40,0,0) ); pointList.add( new IVec(60,0,0) ); // size() gives the length of the array for(int i=0; i < pointList.size(); i++){ new IPoint( pointList.get(i) ); //access i-th member } // insert a vector at index 1. pointList.add( 1, new IVec(0, 0, 30) ); // remove a vector at index 2. pointList.remove( 2 ); // replace the vector at index 3 with a new one. pointList.set( 3, new IVec(60, 0, 30) ); // convert into fixed size array with size() and for loop. IVec[] fixedArray = new IVec[ pointList.size() ]; for(int i=0; i < pointList.size(); i++){ fixedArray[i] = pointList.get(i); } // or shorter way to convert into fixed size array. IVec[] fixedArray2 = pointList.toArray( new IVec[ pointList.size() ] ); // creating a curve. new ICurve(fixedArray).clr(1.,0,0);
The class ArrayList has several methods to manipulate member of the array content .
import processing.opengl.*; import igeo.*; size( 480, 360, IG.GL ); ArrayList < ArrayList < IVec > > ptlist2 = new ArrayList < ArrayList < IVec > >(); for(int i=0; i < 100; i++ ){ // needs to make a child list individually ArrayList < IVec > childList = new ArrayList < IVec >(); int num = 0; if( i%11 == 0 ){ num = 110; } else if( i%8 == 0 ){ num = 80; } else if( i%7 == 0 ){ num = 70; } else if( i%5 == 0 ){ num = 50; } else if( i%4 == 0 ){ num = 40; } else if( i%3 == 0 ){ num = 30; } else if( i%2 == 0 ){ num = 20; } for(int j=0; j < num; j++){ childList.add( new IVec( j, 50-i, j*j*0.01 ) ); } // add the child list to the parent (ptlist2) ptlist2.add(childList); } // create curves for(int i=0; i < ptlist2.size(); i++){ // don't create curve if number of points is less than 2 if( ptlist2.get(i).size() > 1 ){ IVec[] fixedArray = ptlist2.get(i).toArray(new IVec[ptlist2.get(i).size()]); new ICurve(fixedArray).clr(i*0.01); } }
import processing.opengl.*; import igeo.*; size( 480, 360, IG.GL ); ArrayList < IVec > pts = new ArrayList < IVec >(); for(int i=0; i < 100; i++){ IVec p = IRandom.pt(-80,-80,0,80,80,0); pts.add(p); } for(int i=0; i < pts.size(); i++){ new IPoint(pts.get(i)); } // sort points in x direction (blue) ISort.sort(pts, new IXComparator()); new ICurve(pts.toArray(new IVec[pts.size()])).clr(0,0,1.); // sort points in y direction (cyan) ISort.sort(pts, new IYComparator()); new ICurve(pts.toArray(new IVec[pts.size()])).clr(0,1.,1.); // sort points in radial direction (red) ISort.sort(pts, new IRadialComparator()); new ICurve(pts.toArray(new IVec[pts.size()])).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.