Howdy, Stranger!

We are about to switch to a new forum software. Until then we have removed the registration on this forum.

I'm new to Processing & need to use Array Lists (I think)

edited October 2013

Hello everyone, I'm working on my first Processing project and need to store coordinates in an array list. I've looked at the documentation online and I'm still a bit lost.

I'm drawing lines and I'm storing them as PVectors. I'd like to put these PVectors inside an array list. I'm unsure how I should do it. I'm very new to programming in general so I may be using the wrong terminology.

The next step for the program should be to draw a new line between two coordinates that have been drawn already so I'm thinking the best way should be to store every PVector line in an array list and somehow call two of them at random.

The step after that (which I'm not too concerned about just now) would be to find out if the new line intersects any others and create a new "crosshair" point at that coordinate.

Project objective: To replicate an old method of starting a traditional pen & paper drawing by subdividing a page into compositional shapes. This method was created by Loomis and can be seen here: http://3.bp.blogspot.com/-2p98h4ha6v4/UD0Ch2jhqeI/AAAAAAAABb8/0VeSfKP3_jU/s1600/loomisinformalsubdivision.jpg

I'd appreciate any help or mentorship. Let me know if I haven't explained everything clearly.

THanks, James

Here's the project as it stands just now (no array lists yet)

//Two arrays. One with coordinates, one with length & slope of lines.

//Set width & height of canvas
//Draw diagonal line (one of two possible diagonals) (add diagonal vertice points to array)
//Draw the vertical line with random x (add vertical vertice points to array)
//Find where lines intersect (add point to array)
//Draw horizontal line at intersection point (add vertices of this new line to array)
//Pick two vertices from array and draw line between them IF it will intersect an existing line

float rand=random(1);
float xrand;

PVector p1, p2, p3, p4, intersection, coords;

void setup(){
size (1024,576);
noLoop();
xrand=random(width);

// get vertical line
p3 = new PVector (xrand, 0);
p4 = new PVector (xrand, height);

// get diagonal line
if(rand>0.5) {
p1 = new PVector (0,0);
p2 = new PVector (width, height);

} else {
p1 = new PVector (width, 0);
p2 = new PVector (0, height);

}

line (p1.x, p1.y, p2.x, p2.y); //Draw diagonal line
line (p3.x, p3.y, p4.x, p4.y); //Draw random vertical line

// find intersection point for above lines
intersection = intersect(p1, p2, p3, p4);
print (p1 + " " + p2 + " " + p3 + " " + p4);

line (0, intersection.y, width, intersection.y); //draws horizontal at intersect

}

void draw(){

}

// To do: save the vertex coordinates of every intersection to an array list
Tagged:

• Because you always have pairs of coordinates, I would recommend using a 2d array or two arrayLists. Simple arrays are much easier to work with than arrayLists. If you don't need to remove lines, and if you're comfortable with setting a maximum of lines you will use (I guess no one would want more than a hundred lines or something), you could define a normal 2d array:

PVector[][] lines = new PVector;

this creates a table of 100x2 PVectors, so for each line you can store the starting and end point together like this:

start of line 0:

lines.x = xrand;
lines.y = 0;

end of line 0:

lines.x = xrand;
lines.y = height;

You can do the same thing with 2 ArrayLists. I think it's confusing to store start and end points in the same Arraylist.. it's too easy to confuse what is start and end, and wich ones belong together. Also, it becomes more complicated to loop through the lines. I would do it something like this: creating the arraylists:

startingPoints = new ArrayList<PVector>();
endPoints = new ArrayList<PVector>();

adding objects to the arraylist:

startingPoints.add(new PVector (xrand, 0)));
endPoints.add(new PVector (xrand, height)));

Using objects from the arrayList is a little bit weird. You need to create a "temporary" object, and than sort of copy the information from the arraylist into the new object:

PVector aStartingPoint = startingPoints.get(0);

this calls the first object you added to the list, .get(1) would call the second and so on.

To e.g. draw all the lines, you would loop through the arrayList like this:

for (int i = 0; i < startingPoints.size(); i++) {
PVector  anEndPoint = endPoints.get(i);
PVector aStartingPoint = startingPoints.get(i);
line (aStartingPoint.x,aStartingPoint.y,anEndPoint.x,anEndPoint.y);
}

I would use a normal 2d Array, but arrayLists are really usefull and worth learning if you have the time. I don't have the processing software on the computer I'm writing from, so I couldn't debug the code I wrote, but it should be alright. I hope I could help, let me know if what I wrote is unclear! greetings, Martin

**EDIT: I made a mistake in the last part of the code, sorry. Just corrected it.

• "I would recommend using a 2d array or two arrayLists. Simple arrays are much easier to work with than arrayLists."
I disagree...

See From several arrays to classes for some thoughts on it. ArrayLists aren't really harder to use than arrays, and are more flexible.

But, well, you gave both solutions, so it is up to 1030 to choose. :-)