Help w/ a Grid Display Program
in
Programming Questions
•
1 year ago
Right now, I'm trying to get a simple program to display a grid. I would like the grid to display based on input, i.e., if 3 is pressed, the grid should divide the screen into thirds (a 3x3 grid). I'm doing this with some relatively simple but (I think) clever use of variables and recursion. I change the number that the height or width is divided by based on user input (2 - 0) by having an integer variable (called "div") that changes based on the number pressed.
However, at the moment, I have an issue, that being that the variable for "div" isn't updating correctly and the grid isn't changing after the initial input. For example, if I run the program and press 3, the grid divides the screen into thirds - however, if, after pressing three, I attempt to press another number, the grid does not change. I've got the variable "div" printing when the space bar is pressed, and right now, it appears that it's only changed for the split second after the second key is pressed. After that, div seems to revert to 2, regardless of what it was previously set to.
After about an hour or two of fiddling with it, I'm getting a bit frustrated and can't quite figure out the problem. I hope my problem was clear enough, and thanks in advance for the help, here's the code (it's a bit hefty, not too much, though):
- int cols = 0;
- int rows = 0;
- int div = 1;
- void drawCols() {
- pushMatrix();
- line(0,0,0,height);
- translate(width/div,0);
- cols += width/div;
- if (cols < width) {
- drawCols();
- }
- popMatrix();
- }
- void drawRows() {
- pushMatrix();
- line(0,0,width,0);
- translate(0,height/div);
- rows += height/div;
- if (rows < height) {
- drawRows();
- }
- popMatrix();
- }
- void drawGrid() {
- drawCols();
- drawRows();
- }
- void setup() {
- size(500,500);
- background(255);
- }
- void draw() {}
- void keyTyped() {
- switch(key) {
- case ' ':
- println(div);
- case '2':
- div = 2;
- drawGrid();
- break;
- case '3':
- div = 3;
- drawGrid();
- break;
- case '4':
- div = 4;
- drawGrid();
- break;
- case '5':
- div = 5;
- drawGrid();
- break;
- case '6':
- div = 6;
- drawGrid();
- break;
- case '7':
- div = 7;
- drawGrid();
- break;
- case '8':
- div = 8;
- drawGrid();
- break;
- case '9':
- div = 9;
- drawGrid();
- break;
- case '0':
- div = 10;
- drawGrid();
- break;
- }
- }
1