Search code examples
pythonareagenerate

What is the most efficient way to generate dynamic interval switch structure for dividing an area into equal pieces in python?


Suppose you've got a rectangle (x=3456) * (y=1943). These dimension values may vary. What I need to make is dividing this area equally into z pixel square portions and print A1, A2, A3,..,B1, B2, B3..,C1, C2, C3 depending on where my cursor is on.

For example: If the curser is on 123, 85 or in 30, 15 it will Print A1 so on and so forth.

I need to generate this switch case structure automatically according to the the x,y size given dynamically. But each time every portion needs to be definable z pixel square. How this can be managed most efficiently in python?


Solution

  • This can be done with floor division without issues. Make sure it behaves as you expect at boundaries, and modify as needed using remainder == 0 checks.

    max_x, max_y = 3456, 1943
    z = 1000
    x, y = 3,4
    
    if x <= max_x and y <= max_y:
        #chr() takes an int and prints out its ASCII char. chr(65) is 'A'.
        #This assumes you start with 'A1' on top left of page.
        to_print = chr(65 + x//z) + str(1 + y//z)
        print(to_print)
    else:
        print("coordinates out of page")