J
J
JackShcherbakov2019-01-30 19:51:33
Mathematics
JackShcherbakov, 2019-01-30 19:51:33

How to determine the coordinates of a cell, knowing its number?

Hello!
I am developing an application that iterates through a set of colors and paints a cell on the field with this color. This is an extremely simplified description of the application, but for the essence of this it will be enough.
So, let's say we have a field (in my case, this is a canvas on which the colors from the set are applied):

#  #  #  #  # 
#  #  #  #  # 
#  #  #  #  # 
#  #  #  =  # 
#  #  #  #  #

The resolution of our screen is 5 by 5 pixels, with an equal sign (=) I marked the place where the script stopped. The script knows that the pixel number is 18 (the loop went through all the pixels and stopped there).
So, the script needs to somehow understand that the = sign has coordinates (4, 4), knowing that there are only 25 pixels and the pixel number is 18. How can it do this?
Thanks in advance to everyone who answers the question.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Sokolov, 2019-01-30
@JackShcherbakov

The row number is the integer part of the cell number divided by the number of elements in the row.
The column number is the remainder of dividing the number by the same number.

Math.ceil( 18 / 5 ) === 3; // строка с индексом 3, считая от 0
18 % 5 === 3; // колонка с индексом 3, считая, опять же, от 0

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question