This is a Java method that takes as input a 2 dimensional array called grid
.
It uses the i1
and j1
values from when the corresponding grid[i1][j1]
position is true
for the first time, and then i2
and j2
from when the corresponding grid[i2][j2]
position is true
for the second time.
A call is then made to drawLine(i1, j1, i2, j2)
to draw line between those two points in the screen.
I wrote the following code to take all stuff I want to plot, but the code works but looks bad, is there any better way to write this. This code works fine, I just want to improve on it.
for(int x1 = 0; x1 < rows; x1++) {
for (int y1 = 0; y1 < columns; y1++) {
if (grid[x1][y1] == true) {
int x2 = x1 + 1;
for(int y2 = 0; y2 < 500; y2++) {
if (grid[x2][y2] == true) {
drawLine(x1, y1, x2, y2);
break;
}
}
}
}
}
500
. You should declare this as a well-made constant so that its meaning is clear (and so it can be re-used more easily without needing to update its value in more than one place). – Sepster Oct 12 '12 at 0:56