[[+content_image]]
D
D
Dmitry Mushtat2014-03-21 22:56:15
JavaScript
Dmitry Mushtat, 2014-03-21 22:56:15

How to find the nearest element in a 2D array?

Hello!
I have an array (matrix) of points 0 and 1 of the form (schematically, because the real matrix is ​​much larger):

[[0,0,0,0,0,0,1,0,0],
[0,1,0,0,0,0,0,0,0],
[0,0,0,1,0,0,0,1,0],
[0,0,0,0,0,0,0,0,0],
[0,0,1,0,0,0,0,0,0]]

and there are coordinates of the point, of the form :
[3,4]
The task is to find the non-empty element of the array-matrix closest to the coordinates of the point.
I solve the problem in js, but I would be grateful for pseudocode or a verbal description of the solution.
I suspect that the solution is on the surface and quite logical, but I have not found it yet.
Thank you.

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
J
jcmvbkbc, 2014-03-21
@Mushtat

find non-empty element closest to point coordinates

Decide on a metric to determine the "nearest" element. If it is, for example, the Manhattan distance (|dx| + |dy|), then consider increasing diamonds centered at your point, until a non-zero element is found.

A
AM5800, 2014-03-21
@AM5800

Try
http://ru.wikipedia.org/wiki/Search_in_width

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question