array or loop?
in
Programming Questions
•
1 year ago
hi there!!!
i am very new in programing and i do not know how to move from hard code to a simple way.
or maybe i need to use an array.
attach code
int numCircles = 27;
int [][] circles; // use a two-dimensional array
PrintWriter output;
PImage b;
int value = 0;
void setup () {
// Create a new file in the sketch directory
output = createWriter("positions.txt");
size(512, 350);
smooth();
noStroke();
b = loadImage("manikin.jpg");
imageMode(CENTER);
image(b, width/2, height/2, width, height);
circles = new int [numCircles][3]; // define the array, every circle needs three parameters (xPos, yPos, circleDiameter)
// fill array only once
for (int i=0; i<numCircles; i++) {
int circleDiameter = int(20);
int xPos = int(109); // keep distance from border
int yPos = int(67); // keep distance from border
circles[i][0]= xPos + (i % 9) * 38; //horizontal position
circles[i][1]= yPos + (i / 9) * 135;//vertical position
circles[i][2]= circleDiameter;
}
}
void draw() {
for (int i=0; i<numCircles; i++) {
fill(value);
ellipse(circles[i][0], circles[i][1], circles[i][2],circles[i][2]);
}
}
void mousePressed(){
//row 1
if (mouseX > 100 && mouseX < 120){
if (mouseY > 50 && mouseY < 80){
println("box 1");
output.println("box 1");
}
if (mouseY > 190 && mouseY < 210){
println("box 10");
output.println("box 10");
}
if (mouseY > 330 && mouseY < 350){
println("box 19");
output.println("box 19");
}
}
//row 2
if (mouseX > 138 && mouseX < 158){
if (mouseY > 50 && mouseY < 80){
println("box 2");
output.println("box 2");
}
if (mouseY > 190 && mouseY < 210){
println("box 11");
output.println("box 11");
}
if (mouseY > 330 && mouseY < 350){
println("box 20");
output.println("box 20");
}
}
//row 3
if (mouseX > 180 && mouseX < 200){
if (mouseY > 50 && mouseY < 80){
println("box 3");
output.println("box 3");
}
if (mouseY > 190 && mouseY < 210){
println("box 12");
output.println("box 12");
}
if (mouseY > 330 && mouseY < 350){
println("box 21");
output.println("box 21");
}
}
//row 4
if (mouseX > 210 && mouseX < 230){
if (mouseY > 50 && mouseY < 80){
println("box 4");
output.println("box 4");
}
if (mouseY > 190 && mouseY < 210){
println("box 13");
output.println("box 13");
}
if (mouseY > 330 && mouseY < 350){
println("box 22");
output.println("box 22");
}
}
//row 5
if (mouseX > 250 && mouseX < 270){
if (mouseY > 50 && mouseY < 80){
println("box 5");
output.println("box 5");
}
if (mouseY > 190 && mouseY < 210){
println("box 14");
output.println("box 14");
}
if (mouseY > 330 && mouseY < 350){
println("box 23");
output.println("box 23");
}
}
//row 6
if (mouseX > 290 && mouseX < 310){
if (mouseY > 50 && mouseY < 80){
println("box 6");
output.println("box 6");
}
if (mouseY > 190 && mouseY < 210){
println("box 15");
output.println("box 15");
}
if (mouseY > 330 && mouseY < 350){
println("box 24");
output.println("box 24");
}
}
//row 7
if (mouseX > 330 && mouseX < 350){
if (mouseY > 50 && mouseY < 80){
println("box 7");
output.println("box 7");
}
if (mouseY > 190 && mouseY < 210){
println("box 16");
output.println("box 16");
}
if (mouseY > 330 && mouseY < 350){
println("box 25");
output.println("box 25");
}
}
//row 8
if (mouseX > 370 && mouseX < 390){
if (mouseY > 50 && mouseY < 80){
println("box 8");
output.println("box 8");
}
if (mouseY > 190 && mouseY < 210){
println("box 17");
output.println("box 17");
}
if (mouseY > 330 && mouseY < 350){
println("box 26");
output.println("box 26");
}
}
//row 9
if (mouseX > 400 && mouseX < 420){
if (mouseY > 50 && mouseY < 80){
println("box 9");
output.println("box 9");
}
if (mouseY > 190 && mouseY < 210){
println("box 18");
output.println("box 18");
}
if (mouseY > 330 && mouseY < 350){
println("box 27");
output.println("box 27");
}
}
output.flush(); // Writes the remaining data to the file
}
1