I probably used the wrong terminology in my description, but here is what I am trying to do.
I want to set up 2 arraylists, one of points, and one of lines. The line class simply refers to 2 points. I've got this working, but I'm not sure I'm using the best syntax. Here is the section I am wondering about:
first I create the points, then I grab them, then I use them to assign the line. Can I do this all in one step?
here is the entire code so you can see it in context. Thanks for any help.
I want to set up 2 arraylists, one of points, and one of lines. The line class simply refers to 2 points. I've got this working, but I'm not sure I'm using the best syntax. Here is the section I am wondering about:
- allPoints.add ( new myPoint (new PVector(random(width),random(height)) ) );
allPoints.add ( new myPoint (new PVector(random(width),random(height)) ) );
myPoint point1 = (myPoint) allPoints.get(allPoints.size()-1) ;
myPoint point2 = (myPoint) allPoints.get(allPoints.size()-2) ;
allLines.add (new myLine (point1,point2) );
first I create the points, then I grab them, then I use them to assign the line. Can I do this all in one step?
here is the entire code so you can see it in context. Thanks for any help.
ArrayList allPoints;
ArrayList allLines;
//----------------------------------------------------------------------
class myPoint{
PVector pos;
myPoint ( PVector _pos){
pos = _pos;
}
}
//----------------------------------------------------------------------
class myLine{
myPoint p1;
myPoint p2;
myLine (myPoint head, myPoint tail){
p1 = head;
p2 = tail;
}
}
//----------------------------------------------------------------------
void setup(){
size(500,500);
smooth();
allPoints = new ArrayList();
allLines = new ArrayList();
}
void draw(){
background(128);
drawLines();
drawPoints();
}
//----------------------------------------------------------------------
void mousePressed(){
allPoints.add ( new myPoint (new PVector(random(width),random(height)) ) );
allPoints.add ( new myPoint (new PVector(random(width),random(height)) ) );
myPoint point1 = (myPoint) allPoints.get(allPoints.size()-1) ;
myPoint point2 = (myPoint) allPoints.get(allPoints.size()-2) ;
allLines.add (new myLine (point1,point2) );
}
//----------------------------------------------------------------------
void drawLines(){
for (int i = allLines.size()-1; i >= 0; i--) {
myLine l = (myLine) allLines.get(i);
line (l.p1.pos.x , l.p1.pos.y,l.p2.pos.x , l.p2.pos.y );
}
}
//----------------------------------------------------------------------
void drawPoints(){
for (int i = allPoints.size()-1; i >= 0; i--) {
myPoint p = (myPoint) allPoints.get(i);
ellipse (p.pos.x , p.pos.y, 10,10);
}
}
//----------------------------------------------------------------------
1