0

I'm using Processing to create a four-by-four grid with a specified width and height, as follows. enter image description hereMy ultimate goal is to be able to check when the user clicks inside a particular square. In my example, the width and height will both be set to 480 units, but this will eventually grow. So I'd like to do things in a formulaic fashion rather than manual input.

My initial thought was to obtain the vertices of every corner of every square in the grid, set it to a variable name, and then have a bunch of if conditions to check if the user clicks inside a bounded region. Here's what I have so far.

WIDTH = 480
HEIGHT = 480

rows = [WIDTH * 0.0, WIDTH * 0.25, WIDTH * 0.50, WIDTH * 0.75, WIDTH * 1.00]
columns = [HEIGHT * 0.0, HEIGHT * 0.25, HEIGHT * 0.50, HEIGHT * 0.75, HEIGHT * 1.00]
coordinates = [(i, j) for i in rows for j in columns]

# "coordinates" is a list of tuples of all the vertices:
# [(0.0, 0.0), (0.0, 120.0), (0.0, 240.0), (0.0, 360.0), (0.0, 480.0), (120.0, 0.0), (120.0, 120.0), (120.0, 240.0), (120.0, 360.0), (120.0, 480.0), ...]

So I'm able to get a list of tuples of every possible vertex, and now I'd like to assign them all to variables.

For example, for row 1, column 1 (top left square in the grid) the user would need to click in an area less than (0, 120) and (120, 0), but greater than (0,0) and (0,0). These are the vertices that ascribe the top-left square in the grid. I'd do this for each square.

I could give each of these vertices a variable name. For instance, (0, 0) could be vertex_one, (0, 120) could be vertex_two and so on. I know how to iterate through a list of tuples, but I don't know how to assign them to a changing string named vertex_n -- where n represents the vertex number -- while iterating.

But I read from this thread that it's apparently not a good idea to do this. So now I don't know if I should just input each vertex manually or if there's a more programmatic/automatic way to do this, like maybe a dictionary lookup. Because when the grid gets larger, that'll require me to manually type in an even larger number of vertices, which is undesirable.

Any ideas and suggestions on what approach I should take would be appreciated!

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Alureon
  • 179
  • 1
  • 3
  • 14
  • 2
    `click.x // cell_width` should get you the column... Similar for the row using y coordinate – OneCricketeer Nov 17 '18 at 05:36
  • @cricket_007 Do you mean [mouseClicked()](https://processing.org/reference/mouseClicked_.html)? I'm looking at the [reference page](https://processing.org/reference/) and don't see a command `click.x` anywhere. Sorry, I'm new to Processing. Also I'm using the python mode. – Alureon Nov 17 '18 at 05:46
  • I don't know processing, sorry – OneCricketeer Nov 17 '18 at 05:46

1 Answers1

1

I could give each of these vertices a variable name

Even if you had variables for each vertex, you would need to know which vertex is "above and left" of the click coordinate. So, you wouldn't get much use from them, I think; you only need where the mouse has clicked, then perform math based on that.

Since I don't know Processing library, I'm not sure how to gather clicks, but once you get the coordinates, it's a pretty simple division problem

CELL_WIDTH = 120

def get_cell(x, y):
    global CELL_WIDTH
    return {"col":x // CELL_WIDTH, "row": y // CELL_WIDTH}

Samples

print(get_cell(0, 0))     # col=0, row=0
print(get_cell(0, 119))   # col=0, row=0
print(get_cell(0, 120))   # col=0, row=1
print(get_cell(120, 120)) # col=1, row=1
print(get_cell(120, 240)) # col=1, row=2 
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245