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

  • Home
  • SEARCH
  • 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 8475445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:53:40+00:00 2026-06-10T17:53:40+00:00

I am wondering what’s the best solution for this. Return and show the nearest

  • 0

I am wondering what’s the best solution for this. Return and show the nearest hyperlink on a webpage when the mouse is clicked.

There are 3 DIVs in this example. each of them carries a hyperlink inside. There’s also another hyperlink (hyperlink D) by itself without a DIV. And lets say the red dot is mouse click.

For Example

The solution I can think of is just iterate through all the links by doing

    var a_list = document.getElementsByTagName("a");

and then compute the distance by using distance equation c^2 = a^2 + b^2, so simply

    var a_list = Array.prototype.slice.call(document.getElementsByTagName("a"))
    for( var i = 0 ; i < a_list.length; i++){
          Math.sqrt(Math.pow(mouseX - a_list[i].getBoundingClientRect().left,2) + 
                    Math.pow(mouseY - a_list[i].getBoundingClientRect().top,2))

    }

This approach certainly takes about O(N) time complexity where N is the number of links that we have. Can we do better ?

  • 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-06-10T17:53:42+00:00Added an answer on June 10, 2026 at 5:53 pm

    This looks like a nearest neighbor questions like you have a set S of n points in 2 dimensions, and a query point q. Which point in S is the closest one to q.

    I think if you are dealing with not that many links (less than hundred links), then simple approach is the best (the one that you just have. Scan through each of them and calculate the distance, and take the minimal one), however if thousand or millions of links (barely happen) then you definitely need to cache it for later querying purposes, and that certainly speed up the time for querying.

    • One way (your way) is to get all the links and then sort them by X coordinate, you don’t care about Y coordinate just for now, and then do a binary search on X, and you will end up with one of the link, but that may not be the closest one, you still need to compare with the previous one and the next one by distance (now use Y coordinate). For instance, From your example, when you do a binary search on X since the X coordinate of that red dotted (mouse clicked position) is greater than hyperlink D, so it will search everything after that, but hyperlink D could be the closest, so you need to take into consideration. This approach takes O(N log N) for sorting, O(N) space, and O(log N) for querying.

    • You can also use K-d Tree. It works pretty well with small number of dimensions (in your case it’s only two dimensions x and y), and it can efficiently search for the cell containing the query point p (your mouse clicked position).
      Construction time O(N log N), Space O(N) and Query Time: O(n^1/2 + k)

    • Another way I can think of is to construct the Voronoi diagram, which provide an efficient data structure for nearestneighbor queries and best for two dimension.
      Construction time O(N log N), Space O(N) and Query Time: O(log n)

    So pretty much all the approaches are the same.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Wondering what the best / good way of doing this would be in jQuery.
Wondering if anyone has a good solution for this. My app is displaying nothing
Wondering if there’s a plugin or best way of setting up an ActiveRecord class
Wondering what others do / best practice for communicating between layers. This question relates
Wondering if there is tool to assist this kind of refactoring.
Wondering if anyone out there has ran into this before.... I'd like to use
Wondering what the best way to make this more efficient, perhaps with jQuery. I
Wondering if there is any way to get the lambda expressions that result from
Wondering what is the best or most popular database client tool. Similar to Microsoft's
Wondering if there is a good way to generate temporary URLs that expire in

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.