I need an algorithm that will return a dot to a list in Python

I am using the Zelle Python graphics library and I need some help creating an algorithm to return a number to a list.

Basically, I have a 5x7 board divided into a grid of 100x100 pixels. This matches a list like this.

| 0| 1| 2| 3| 4| | 5| 6| 7| 8| 9| |10|11|12|13|14| |15|16|17|18|19| |20|21|22|23|24| |25|26|27|28|29| |30|31|32|33|34| 

I need an algorithm that would take the center point of the grid with the mouse and turn it into a number corresponding to the list. For example, the point (50.50) will return 0, and the point (150, 150) will return 6, etc.

Thanks so much for taking the time to help figure out this algorithm!

+4
source share
2 answers
 In [1]: def f(x, y): ...: return y // 100 * 5 + x // 100 ...: In [2]: f(50, 50) Out[2]: 0 In [3]: f(150, 150) Out[3]: 6 
+5
source
 def point_to_xy(x_mouse,y_mouse): x_pos = math.floor(x_mouse/100) #or x_mouse // 100 y_pos = math.floor(y_mouse/100) #or y_mouse // 100 return x_pos,y_pos def xy_to_index(x_pos,y_pos): array_0_width = 5 #the width of the 2d array #position is y*width + x_offset return y_pos*array_0_width+x_pos x,y = point_to_xy(mouse.x,mouse.y) print xy_to_index(x,y) 

I think it would work

 >>> x,y = point_to_xy(150,150) #x,y=1,1 >>> print xy_to_index(x,y) 6.0 >>> x,y = point_to_xy(50,50) # x,y=0,0 >>> print xy_to_index(x,y) 0.0 
+1
source

All Articles