How to fill in a grid using Mouse positions.

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
            boolean newquad;//used to write over old quad indicarot color

            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[64];//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();
              if(newquad!=currentquad){
                background(255);
              }


              if(mouseX<arrayX[1] && mouseY<arrayY[1]){
                  current_quad[0]=true;
                  fill(255,0,0);
                  rect(arrayX[0],arrayY[0],arrayX[1],arrayY[1]);
              }
                if(mouseX>arrayX[1] && mouseY>arrayY[0]&&mouseX<arrayX[2] && mouseY<arrayY[1]){
                  current_quad[1]=true;
                  fill(255,0,0);
                  rect(arrayX[1],arrayY[0],arrayX[1],arrayY[1]);
                  //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:

Answers

Sign In or Register to comment.