0

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?

ybaylav
  • 41
  • 8
  • 2
    Don't think of it as a switch. You just need to use division and remainders. – Paritosh Singh Dec 03 '18 at 10:45
  • I think it as a switch as I need to print out something depending 2 dimensional areas. Doing it with static values and manually it is easy, but I need a conditional structure that is generated automatically with x,y and z. What kind of structure I can use? – ybaylav Dec 03 '18 at 10:52

1 Answers1

1

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")
Paritosh Singh
  • 6,034
  • 2
  • 14
  • 33
  • This is quite useful for just generating chess notation results. But by saying A1,..A10; B1..B10 I meant it could be any string. Suppose a touchable surface. I need to split that surface into equal rectangles and map each rectangle to some words. I gave the chess notation in order to ease dreaming how it is going to be. When somebody touches at some point in the board, I need to print out some words. – ybaylav Dec 03 '18 at 11:58
  • use the "to_print" variable and build your logic on it. You just need a dictionary to get going. @ybaylav – Paritosh Singh Dec 03 '18 at 12:03