We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Problem with 2D-Array
Page Index Toggle Pages: 1
Problem with 2D-Array (Read 627 times)
Problem with 2D-Array
May 23rd, 2009, 8:11am
 
Hello,
i want to save the x-coordinate,y-coordinate and the color of each point in the window in the array "field". First i want to give every Point a random color, then save it in the array and at last i want to draw the points into the window. But my code doesn't work.
What's my mistake?
Thanks for your help  in advance.  Wink

Code:
 color[] colours={#000000,#123456,#FF3300,#00FF00,#FFFF00};
int cols = width;
int rows = height;
Point[][] field = new Point[cols][rows];

class Point {
float xpos,ypos;
color colour;
Point(float gxpos,float gypos,color gcolour) {
   xpos=gxpos;
   ypos=gypos;
   colour=gcolour;
 }
}
void setup() {
size(400,400);
background(255);
for (int i = 0; i < cols; i++) {
 for (int j = 0; j < rows; j++) {
   field[i][j]=new Punkt(i,j,colours[int(random(colours.length-1))]);
   stroke(field[i][j].colour);
   point(field[i][j].xpos,field[i][j].ypos);
 }
}
}

Re: Problem with 2D-Array
Reply #1 - May 23rd, 2009, 11:15am
 
I see a number of problems...
- Initialization of cols and rows is done before setup() is called, so width and height are not defined at this stage. You can declare the variables there and initialize them in setup.
- There is some redundancy in storing coordinates in Point, and storing points in a two dimensions array. Not a real problem, just something I wanted to point out.
- new Punkt isn't consistent with Point class... Smiley I suppose you translated for the forum.
- With your random expression, you will never use the last color. random is up to the given number, exclusive.

That's "all". Wink
Re: Problem with 2D-Array
Reply #2 - May 24th, 2009, 10:59am
 
Quote:
- Initialization of cols and rows is done before setup() is called, so width and height are not defined at this stage. You can declare the variables there and initialize them in setup.

You are right. I forgot that.
Quote:
- new Punkt isn't consistent with Point class... Smiley I suppose you translated for the forum.

Yes i translated it...sorry
Quote:
- With your random expression, you will never use the last color. random is up to the given number, exclusive.

Thanks, I didn't know that.
Thanks a lot  Smiley
Page Index Toggle Pages: 1