We are about to switch to a new forum software. Until then we have removed the registration on this forum.
How do I divide array squ
into 2 sets of new Square [3]
new Square [4]
and loop through everything without having to declare everything everytime in setup and draw? (coz' I might have 100 of them in the future)
Square [] squ = new Square[7];
int [] numberX0 = { 12, 12, 12, 12, 30, 30, 30};
int [] numberY0 = { 10, 50, 105, 173, 215, 280,426};
void setup() {
size(500, 500);
for (int i = 0; i<squ.length; i++) {
squ[i] = new Square(numberX0[i], numberY0[i]);
}
}
void draw() {
background(242, 242, 240);
for (int i = 0; i<squ.length; i++) {
squ[i].bsq();
}
}
what I have is below, but I know this should be cleaner, otherwise when I have 100 of them, it will be a mess, but I couldn't figure it out how to increase squ number hereSquare [] squ = new Square[3];
and X,Y number here
int [] numberX = {12};
int [] numberY = {10, 50, 105};
int [] numberX1 = {30};
int [] numberY1 = {173, 215, 280, 426};
at the same time... and in setup and draw too...
Square [] squ = new Square[3];
Square [] squ1 = new Square[4];
int [] numberX = {12};
int [] numberY = {10, 50, 105};
int [] numberX1 = {30};
int [] numberY1 = {173, 215, 280, 426};
void setup() {
size(500, 500);
for (int i = 0; i<squ.length; i++) {
squ[i] = new Square(numberX[0], numberY[i]);
}
for (int i = 0; i<squ1.length; i++) {
squ1[i] = new Square(numberX1[0], numberY1[i]);
}
}
void draw() {
background(242, 242, 240);
for (int i = 0; i<squ.length; i++) {
squ[i].bsq();
}
for (int i = 0; i<squ1.length; i++) {
squ1[i].bsq();
}
}
class Square {
float x, y;
Square(float _x, float _y) {
x = _x;
y = _y;
}
void bsq() {
fill(54, 80, 172);
rect(x, y, 10, 10);
}
}
Answers
I don't understand what you want to achieve...?
What is your problem?
why not stick to
Square [] squ = new Square[3000];
?
No need to split it up, really. Why?
hints
you can append data to the array using append() - see reference
or use an ArrayLIst in the 1st place (you can add items easily here, when you don't know how many items you'll have in the end)
the initial lines with the values
when your issue are those lines:
int [] numberY1 = {173, 215, 280, 426};
you can make them as long as you want and also insert line breaks inside of them
or you store those data in a text file and load it in a for-loop
Chrisir ;-)
I mean if I have 3000 numbers in one array, wouldn't that be hard to find, let's say, the number on spot 349? Or I just use println to find out? The reason why I wanted to separate values into different array is because I will be easier to find like my attachment x,y value for the second column 4th square when I have 100 columns and a lot more squares.
no...
I see your problem....
but your solution is not wise
you can let the square println out it's position when you click on it with the mouse
there are also techniques to tell each square not only the x,y position (in pixels) but also to tell it the column and row it has (when you think of a grid)
here
hmm... yes, I do have println out my mouseX and mouseY ... but I think a grid system is what I need. So I can have access to each individual square, say make square 1,2,3 move, but not 4,5,6,7...
sorry, let me think it through before I have other questions, I don't wanna waste your time when I don't even know what I wanna ask...thanks.
Got the grid code, will study those too, thanks Chrisir, again!
it depends what you are aiming at of course...
do you want to find it with the mouse?
or does your sketch have to find the rect's number? And the sketch shows one attachment then?
yes, you can let your sketch check
whether a square was clicked (using mouseX and mouseY) and also
which square was clicked
and then it's selected
and you can drag and drop it
or show its data
Various online grid examples: :bz
here you got some fancy drag and drop