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 6555601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:50:25+00:00 2026-05-25T12:50:25+00:00

Need some help here. I’m a UI designer who isn’t good at numbers doing

  • 0

Need some help here. I’m a UI designer who isn’t good at numbers doing an experimental web form design and I need to know which input element is closest to a clicked point on a web page. I know how to do nearest neighbor with points but the input elements are rectangles not points so I’m stuck.

I’m using jQuery. I just need help with this little algo. Once I’m done with my experiment I’ll show you guys what I’m doing.

UPDATE

I thought about how it can work. Look at this diagram:

Nearest

Each rectangle has 8 points (or rather 4 points and 4 lines) which are significant. Only the x value is significant for horizontal points (red dot) and only the y value is significant for vertical points (green dot). Both x and y are significant for the corners.

Orange crosses are the points to be measured against – mouse clicks in my use case. The light purple lines are the distances between the orange cross and it’s possible nearest point.

So… for any given orange cross, loop through each of the 8 points n every rectangle to find the nearest edge or corner closest of each rectangle to the orange cross. The rectangle with the lowest value is the nearest one.

I can conceptualize and visualize it but can’t put it into code. Help!

  • 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-25T12:50:25+00:00Added an answer on May 25, 2026 at 12:50 pm

    Your algorithm is correct. Since you need help in code, and not in the algorithm, here’s the code:

    It may not be the most efficient. But it works.

    // Define the click
    var click = Array(-1, -2); // coodinates in x,y
    
    // Define the buttons
    // Assuming buttons do not overlap
    var button0 = Array(
        Array(0, 0), // bottom-left point (assuming x is horizontal and y is vertical)
        Array(6, 6) // upper-right point
    );
    
    var button1 = Array(
        Array(10, 11),
        Array(17, 15)
    );
    
    var button2 = Array(
        Array(-8, -5),
        Array(-3, -1)
    );
    
    // Which button to trigger for a click
    i = which(click, Array(button0, button1, button2));
    alert(i);
    
    
    function which(click, buttons){
        // Check if click is inside any of the buttons
        for (i in buttons){
            var button = buttons[i];
            var bl = button[0];
            var tr = button[1];
    
            if ( (click[0] >= bl[0] && click[0] <= tr[0]) &&
                 (click[1] >= bl[1] && click[1] <= tr[1]) ){
                return i;
            }
        }
    
        // Now calculate distances
        var distances = Array();
    
        for (i in buttons){
            var button = buttons[i];
            var bl = button[0];
            var tr = button[1];
    
            if ( (click[0] >= bl[0] && click[0] <= tr[0])) {
                distances[i] = Math.min( Math.abs(click[1]-bl[1]), Math.abs(click[1]-tr[1]) );
            }
            else if ( (click[1] >= bl[1] && click[1] <= tr[1])) {
                distances[i] = Math.min( Math.abs(click[0]-bl[0]), Math.abs(click[0]-tr[0]) );
            }
            else{
                distances[i] =  Math.sqrt(
                                    (Math.pow(Math.min( Math.abs(click[0]-bl[0]), Math.abs(click[0]-tr[0]) ), 2)) +
                                    (Math.pow(Math.min( Math.abs(click[1]-bl[1]), Math.abs(click[1]-tr[1]) ), 2))
                                );
            }
        }
    
        var min_id = 0;
        for (j in distances){
            if (distances[j] < distances[min_id]){
                min_id = j;
            }
        }
    
        return min_id;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need some help figuring out what I'm doing wrong here. I am trying
everyone! I'm not very good with JS/JQuery, so I need some help here... I
I need some help with floating point numbers...please! Here's the thing, I have code
Well i need some help here i don't know how to solve this problem.
I need some advanced CSS help here. I have a login button and a
I need some help. Here's what I'm getting right now: img ref I need
I need some help extracting the following bits of information using regular expressions. Here
I'm in need of some basic TSQL help. Here's my table layout: Orders Table
Need some help here. I have deployed spree (0.70.3) on slicehost (ubuntu, ruby1.8.7, Rails
I need some help - here are my requirements. 1: I should be able

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.