#### Howdy, Stranger!

We are about to switch to a new forum software. Until then we have removed the registration on this forum.

# How to fill in a grid using Mouse positions.

edited August 2016

I am attempting to use the position of the mouse cursor to fill in the specific section of a grid (as of now that grid is 8x8, with each grid square being 40 pixels wide, and 40 pixels high) that it occupies. I am trying to do this in an efficient manner as this is the beginning of what i would like to be a much larger grid system reaching into the hundreds of sections. I have successfully done this with the first two sections of grid by storing the coordinates of the grid with in an array and comparing the position of the mouse to those coordinates and hard coding the positions into the rect() function. I am looking for advice on how to compare the position of the mouse to the positions within the array, store those specific coordinates, then use them to fill the specific section of the grid. I am looking to avoid writing 64 comparative if/then statements.

below is the code I am working on now.

Any tips appreciated. Thanks!

///////////////////

``````            boolean currentquad=false;//determines the state of quad mouse cursor is currently in

int x1,x2,y1,y2=0;//positions of rect to be drawn

int spc=40;//spacing for grid. used in order for program to scale
int array_size=9;//size of point arrays, used in order to scale

int[] arrayX = new int[array_size];//arrays containing x and y location of all grid intersections
int[] arrayY = new int[array_size];

int x;
int y;
int array_inc=40;//same as spacin

boolean[] current_quad = new boolean;//boolean array in order to keep track of all states of quads

int current_arrayX;//determine the points in which rec should be drawn between, do we need 2 of each?
int current_arrayY;

void setup(){
background(255);//draws background
size(320,320);//size of window
grid();//grid function
array_fill();//array fill function, used for scaleing
}

void draw(){
grid();
background(255);
}

if(mouseX<arrayX && mouseY<arrayY){
fill(255,0,0);
rect(arrayX,arrayY,arrayX,arrayY);
}
if(mouseX>arrayX && mouseY>arrayY&&mouseX<arrayX && mouseY<arrayY){
fill(255,0,0);
rect(arrayX,arrayY,arrayX,arrayY);
//rect(40,0,40,40);
}
for(int i=0; i<array_size; i++){
println(arrayX[i] ," ", arrayY[i]);
}
}

void array_fill() {//inserts cooridnants into array for using to check mouse position
for (int i=0; i<array_size; i++) {//for loop runs length of array[]s
arrayX[i]=x;//variable increase by grid spacing each time
arrayY[i]=x;
x+=array_inc;
}
}

void grid() {//draws grid at setup
for (int i=0; i<9; i++) {//draws y lines
line(0, y1, 320, y2);
y1+=spc;
y2+=spc;
}
for (int i=0; i<9; i++) {//draws x lines
line(x1, 0, x2, 320);
x1+=spc;
x2+=spc;
}
}
``````
Tagged: