+ 1
Grids question
Ok, so I have this code. https://code.sololearn.com/cLR6Y4BgijMz/?ref=app plotPoint for some reason is plotting incorrectly for example, plotting (3,1) adds an X 3 down rather than 3 to the right. my tired brain can't seem to figure out why this is happening and I don't have the time to explore more options. Can someone explain why this is happening? Edit: TL;DR X and y are reversed I did find a fix where I just set a new x and y variable where Y = x-1 and X = x-1 and use those to plot and delete points. But this fix is just annoying and I'd rather x just be x and y be y. Lol
2 odpowiedzi
+ 4
The problem is due to the fact that in a[i][j], i represents the row number as opposed to the absicca in coordinate system.
When you use one.plotPoint(3,6), the code marks the element in the grid with row number 3 and column number 6 with X. This as per the coordinate system is 6,3, but for the 2D grid, it is 3,6 (Row number 3, column number 6).
The X axis values actually represents the column numbers here, and that is why interchanging the values works.
Even in the constructor, if you pass a size like 10,20 (10 width, 20 height) the code will allocate a grid of 10 rows and 20 columns (20 width, 10 height).
Thus in the constructor and all other functions, you must swap the use of x and y. Allocate new char* [height] first and then allocate new char[width] in a loop of y running from 0 to height.
+ 1
Sorry, my small distraction. The problem is how the grid is printed. In practice actually the grid is printed column by column and is not what you want (because the code allocate the grid column by column and not how usually did row by row). A simple solution is print row by row like:
for(int y=0; y<height; y++) {
for(int x=0; x<width; x++) {
cout << data[x][y];
}
cout << endl;
}
Futhermore you have to adjust the "offsetted" x and y in plotPoint and deletePoint and grid bounds handling.
Here adjusted code
https://code.sololearn.com/cd815CoVlIRK/#cpp