Sorting curves!

The java Collections class has very useful methods, including sort(). This allows you to sort objects based on an attribute or something else you specify!

The example below sorts curves based on their length. You need first to create a new class that extends ICurve and that implements the Comparable interface and overrides the compareTo method used to sort the curves.

I’ll keep posting more about this, if you feel like it, do please come back!

code:

import igeo.*;
import processing.opengl.*;
import java.util.*;
void setup() {

size(1200, 822, IG.GL);
IG.top();

ArrayList<ESortableCurve> myList = new ArrayList<ESortableCurve>();
ArrayList<ICurve> mySecondList = new ArrayList<ICurve>();
ArrayList<ICurve> myThirdList = new ArrayList<ICurve>();
IRand currentRandom = new IRand(); //creates a new IRandom Object
currentRandom.initByTime();//Links the random seed to current time millisecs

//create the original list of curves
for (int i=0; i<50; i++)
{
IVec[] v = new IVec[2];
v[0]=new IVec(i*10, 0, 0);
v[1]=new IVec(i*10, currentRandom.getDouble(100), 0);
myList.add(new ESortableCurve(v));
}

//prints the lengh of the curves on their original order
println (“Start of original list”);
for (ESortableCurve crv1 : myList)
{
println(crv1.len());
}
println (“End of original list”);

//Sorts the list according to their lengh. Pls see coments on compareTo method below
Collections.sort(myList);

//prints the lengh of the curves on their sorted order
println (“Start of sorted list”);
for (ESortableCurve crv1 : myList)
{
println(crv1.len());
}
println (“End of sorted list”);

//Draw new curves with ascending length
for (int i=0; i<myList.size(); i++)
{
IVec[] v = new IVec[2];
v[0]=new IVec(i*10, -100, 0);
v[1]=new IVec(i*10, -100+myList.get(i).len(), 0);
mySecondList.add(new ESortableCurve(v).clr(255-(255/100)*i, 255, 0));
}

//Reverses the sorted list according to their lengh. Pls see coments on compareTo method below
Collections.reverse(myList);

//prints the lengh of the curves on their reversed sorted order
println (“Start of reversed list”);
for (ESortableCurve crv3 : myList)
{
println(crv3.len());
}
println (“End of reversed list”);

//Draw new curves with descending length
for (int i=0; i<myList.size(); i++)
{
IVec[] v = new IVec[2];
v[0]=new IVec(i*10, -200, 0);
v[1]=new IVec(i*10, -200+myList.get(i).len(), 0);
myThirdList.add(new ESortableCurve(v).clr((255/100)*i, 0, 255));
}

//prints the minimum lengh found
println (“The shortest curve length is :”);
println (Collections.min(myList).len());

//prints the maximum lengh found
println (“The longest curve length is :”);
println (Collections.max(myList).len());

IG.focus();
}

/*class of ESortableCurves that implements the Comparable interface,
this allows the curve to be sorted according to the behaviour specified
by the compareTo method
*/
class ESortableCurve extends ICurve implements Comparable<ESortableCurve>
{

public ESortableCurve(IVecI[] cpts)
{
super(cpts);
}

/*The compareTo method is the one we need to override to specify how
the objects, in this case ESortableCurves will be sorted.
the implementation below sorts them according to their lengh in
ascending order
*/
int compareTo( ESortableCurve crv)
{
int result = 0;

if (this.len() < crv.len()) result = -1;
if (this.len() > crv.len()) result = 1;
return result;
}
}

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 2.0 UK: England & Wales License.

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.