Hi,
I am trying to create an array that divides a 128*64 pixel display in a grid. That way I can use the pixel coordinates to print rectangles. I am basing this project of JavaScript code so some parts of my code are JS code, and should be changed. But they are indicators of what I want my program to do. If someone has a solution on how to solve this with a struct or in a different way that would be great.
I thank you in advance.
Michiel de Jong
struct Coordinates {
int x;
int y;
}
void CreateCoordArray() {
struct Coordinates Coordinates = (x,y);
int i = 0;
int j = 0;
for (int y = 0; y <= 128; y + 8) {
for (int x = 0; x <= 64; x + 8) {
coordinateArray[i][j] = new Coordinates(x, y); //!!!
i++;
}
j++;
i = 0;
}
}
void DrawTetromino() {
for (int i = 0; i < curTetrominoLength; i++) {
int x = curTetromino[i][0] + startX;
int y = curTetromino[i][1] + startY;
gameBoardArray[x][y] = 1;
let coorX = coordinateArray[x][y].x;
let coorY = coordinateArray[x][y].y;
u8g2.setDrawColor(1);
u8g2.drawBox(coorX, coorY, blokjeDimentie, blokjeDimentie);
}
}
[color=#00979c]struct[/color] [color=#000000]Coordinates[/color] [color=#000000]{[/color]
[color=#00979c]int[/color] [color=#000000]x[/color][color=#000000];[/color]
[color=#00979c]int[/color] [color=#000000]y[/color][color=#000000];[/color]
[color=#000000]}[/color][color=#000000];[/color]
[color=#000000]Coordinates[/color] [color=#000000]coordinateArray[/color][color=#000000][[/color][color=#000000]9[/color][color=#000000]][/color][color=#000000][[/color][color=#000000]17[/color][color=#000000]][/color][color=#000000];[/color]
[color=#00979c]void[/color] [color=#000000]CreateCoordArray[/color][color=#000000]([/color][color=#000000])[/color] [color=#000000]{[/color]
[color=#5e6d03]for[/color] [color=#000000]([/color][color=#00979c]int[/color] [color=#000000]y[/color] [color=#434f54]=[/color] [color=#000000]0[/color][color=#434f54],[/color] [color=#000000]j[/color] [color=#434f54]=[/color] [color=#000000]0[/color][color=#000000];[/color] [color=#000000]y[/color] [color=#434f54]<=[/color] [color=#000000]128[/color][color=#000000];[/color] [color=#000000]y[/color] [color=#434f54]+=[/color] [color=#000000]8[/color][color=#434f54],[/color] [color=#000000]j[/color][color=#434f54]++[/color][color=#000000])[/color] [color=#000000]{[/color]
[color=#434f54]// Are you sure you don't mean < 128 instead of <= 128?[/color]
[color=#5e6d03]for[/color] [color=#000000]([/color][color=#00979c]int[/color] [color=#000000]x[/color] [color=#434f54]=[/color] [color=#000000]0[/color][color=#434f54],[/color] [color=#000000]i[/color] [color=#434f54]=[/color] [color=#000000]0[/color][color=#000000];[/color] [color=#000000]x[/color] [color=#434f54]<=[/color] [color=#000000]64[/color][color=#000000];[/color] [color=#000000]x[/color] [color=#434f54]+=[/color] [color=#000000]8[/color][color=#434f54],[/color] [color=#000000]i[/color][color=#434f54]++[/color][color=#000000])[/color] [color=#000000]{[/color]
[color=#000000]coordinateArray[/color][color=#000000][[/color][color=#000000]i[/color][color=#000000]][/color][color=#000000][[/color][color=#000000]j[/color][color=#000000]][/color] [color=#434f54]=[/color] [color=#000000]{[/color][color=#000000]x[/color][color=#434f54],[/color] [color=#000000]y[/color][color=#000000]}[/color][color=#000000];[/color]
[color=#434f54]// watch out, it's very easy to go out of bounds on the array![/color]
[color=#000000]}[/color]
[color=#000000]}[/color]
[color=#000000]}[/color]
You have to watch out for index out of bounds errors in your code. The pixel in the bottom right corner of the display is (127, 63), not (128, 64).
Also be careful when using one index for the loop exit condition, and another for array access, that's just asking for trouble ...
Pieter
Edit: Good catch @TheMemberFormerlyKnownAsAWOL, fixed the x and y increments in my reply.
Power_Broker:
How do you get the code to automatically color keywords like that?
I wrote a little JavaScript tool for it. It simply converts the HTML output from the IDE to BB codes. It's a pretty dumb RegEx matcher, but it gets the job done.
PieterP:
I wrote a little JavaScript tool for it. It simply converts the HTML output from the IDE to BB codes. It's a pretty dumb RegEx matcher, but it gets the job done.