Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 951369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:43:31+00:00 2026-05-15T23:43:31+00:00

Say you have a 2D grid with each spot on the grid having x

  • 0

Say you have a 2D grid with each spot on the grid having x number of objects (with x >=0). I am having trouble thinking of a clean algorithm so that when a user specifies a coordinate, the algorithm finds the closest coordinate (including the one specified) with an object on it.

For simplicity’s sake, we’ll assume that if 2 coordinates are the same distance away the first one will be returned (or if your algorithm doesn’t work this way then the last one, doesn’t matter).

Edit: A coordinate that is 1 away must be either 1 up, down, left or right. Coordinates that are away diagonally are 2.

As a side note, what is a great, free, online reference for algorithms?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T23:43:31+00:00Added an answer on May 15, 2026 at 11:43 pm

    Update

    With new information:

    Assuming that a coordinate diagonally
    is 2 away

    This algorithm would work. The algorithm searches outwards in a spiral kinda way testing each point in each ‘ring’ started from the inside.

    Note that it does not handle out of bounds situations. So you should change this to fit your needs.

    int xs, ys; // Start coordinates
    
    // Check point (xs, ys)
    
    for (int d = 1; d<maxDistance; d++)
    {
        for (int i = 0; i < 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 < d; i++)
        {
            int x1 = xs - i;
            int y1 = ys + d - i;
    
            // Check point (x1, y1)
    
            int x2 = xs + i;
            int y2 = ys - d + i;
    
            // Check point (x2, y2)
        }
    }
    

    Old version

    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.

    Note that it does not handle out of bounds situations. So you should change this to fit your needs.

    int xs, ys; // Start coordinates
    
    if (CheckPoint(xs, ys) == true)
    {
        return (xs, ys);
    }
    
    for (int d = 0; d<maxDistance; d++)
    {
        for (int x = xs-d; x < 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 < 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);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a grid of a variable number of elements. Say 5 images per
Say I have a grid of 5x5, with each cell in the grid numbered
Let's say I have circular objects. Each object has a diameter of 64 pixels.
Let's say I have a grid of squares, each with their coordinates (x and
Lets say we have a grid containing cells, and that we want to return
Say I have a grid, I click an object and it displays in a
Lets say I have 2 columns in my data Grid: Column A: Selected, and
Let's say i have a 2d linear grid and a point in said grid.
I have a list of say, 20 items. Each of them are boxes in
Say I have 2 tiles in a grid. Tile1[x,y] //stationary thing Tile2[x,y] //movable thing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.