Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: What is the best way to handle interactive grid?  (Read 1458 times)

0 Members and 1 Guest are viewing this topic.

svento92

  • Newbie
  • *
  • Posts: 6
    • View Profile
What is the best way to handle interactive grid?
« 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?
« Last Edit: December 06, 2019, 09:04:41 am by svento92 »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: What is the best way to handle interactive grid?
« Reply #1 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/