SFML community forums

Help => Graphics => Topic started by: svento92 on December 04, 2019, 08:55:29 pm

Title: What is the best way to handle interactive grid?
Post by: svento92 on December 04, 2019, 08:55:29 pm
I'm in the planning phase of doing a Sudoku. Should i move all 81 shapes individually or is there a better way?
Title: Re: What is the best way to handle interactive grid?
Post by: eXpl0it3r on December 04, 2019, 11:02:00 pm
You shouldn't forget that you're dealing with a computer here, iterating some 81 rectangles will only bore your CPU. ;)

However since your grid is evenly spaced you don't actually need to iterate anything, but you can calculate within which cell the cursor is.

- Get the mouse position relative to the window
- Subtract the spacing around the grid (if there's any)
- Divide width and height by the width or height of a cell
- Round result down

Now you should end up with something like (0,0) for the top left cell, (0, 1) for the middle left cell, (2, 2) for the bottom right cell, etc.