Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Udate</strong></p> <p>With new information: </p> <blockquote> <p>Assuming that a coordinate diagonally is 2 away</p> </blockquote> <p>This algorithm would work. The algorithm searches outwards in a spiral kinda way testing each point in each 'ring' started from the inside.</p> <p>Note that it does not handle out of bounds situations. So you should change this to fit your needs. </p> <pre><code>int xs, ys; // Start coordinates // Check point (xs, ys) for (int d = 1; d&lt;maxDistance; d++) { for (int i = 0; i &lt; d + 1; i++) { int x1 = xs - d + i; int y1 = ys - i; // Check point (x1, y1) int x2 = xs + d - i; int y2 = ys + i; // Check point (x2, y2) } for (int i = 1; i &lt; d; i++) { int x1 = xs - i; int y1 = ys + d - i; // Check point (x1, y1) int x2 = xs + d - i; int y2 = ys - i; // Check point (x2, y2) } } </code></pre> <p><strong>Old version</strong></p> <p>Assuming that in your 2D grid the distance between (0, 0) and (1, 0) is the same as (0, 0) and (1, 1). This algorithm would work. The algorithm searches outwards in a spiral kinda way testing each point in each 'ring' started from the inside.</p> <p>Note that it does not handle out of bounds situations. So you should change this to fit your needs. </p> <pre><code>int xs, ys; // Start coordinates if (CheckPoint(xs, ys) == true) { return (xs, ys); } for (int d = 0; d&lt;maxDistance; d++) { for (int x = xs-d; x &lt; xs+d+1; x++) { // Point to check: (x, ys - d) and (x, ys + d) if (CheckPoint(x, ys - d) == true) { return (x, ys - d); } if (CheckPoint(x, ys + d) == true) { return (x, ys - d); } } for (int y = ys-d+1; y &lt; ys+d; y++) { // Point to check = (xs - d, y) and (xs + d, y) if (CheckPoint(x, ys - d) == true) { return (xs - d, y); } if (CheckPoint(x, ys + d) == true) { return (xs - d, y); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload