Ich versuche eine Grid-Klasse zu erstellen, um Elemente auf Zeilen und Spalten anzuordnen. Die Methoden cellXCenter() und cellYCenter() sollen die Koordinaten für eine gegebene i-Zelle im Grid zurückgeben.Was ist falsch an meiner Grid-Klasse?
Wenn dieser Code ausgeführt wird, der eine Ellipse in jede Rasterzelle zeichnen soll, erhält nur das letzte Element die korrekten Koordinaten.
Was mache ich falsch?
Grid grid;
int columns = 3;
int rows = 2;
void setup() {
size(900,600);
background(0);
grid = new Grid(columns,rows);
fill(255,0,0);
for (int i = 0 ; i < columns * rows; i++) {
ellipse(grid.cellXCenter(i+1),grid.cellYCenter(i+1),100,100);
}
}
class Grid {
//grid width, height, number of columns, number of rows
int gw;
int gh;
int cols;
int rows;
int cells; //total de celdas desde 1
float cellWidth;
float cellHeight;
Grid(int cols_, int rows_) {
gw = width;
gh = height;
cols = cols_;
rows = rows_;
cells = rows * cols;
cellWidth = gw/cols;
cellHeight = gh/rows;
}
int rowPos(int index_) {
//all index arguments one based
float i = index_;
int position = (int)Math.ceil(i/cols);
return position;
}
int colPos(int index_) {
int i = index_;
int position = i - (cols * (rowPos(i) -1));
return position;
}
float cellX(int index_) {
int i = index_;
float xPos = gw * (colPos(i)/cols);
return xPos;
}
float cellXCenter(int index_) {
int i = index_;
float xCenterPos = cellX(i) - cellWidth/2;
return xCenterPos;
}
float cellY(int index_) {
int i = index_;
float yPos = gh * (rowPos(i)/rows);
return yPos;
}
float cellYCenter(int index_) {
int i = index_;
float yCenterPos = cellY(i) - cellHeight/2;
return yCenterPos;
}
}
Der Code ist schwer zu lesen und vermischt lokale und externe Variablen. Schwer zu überprüfen, ob die Typen richtig verwendet werden usw. –