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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T11:37:38+00:00 2026-05-19T11:37:38+00:00

Seems like there should be some convenient way to do this? I couldn’t find

  • 0

Seems like there should be some convenient way to do this?

I couldn’t find one, so I threw together the below algorithm. Is it memory/computationally optimal?

Thanks:

Edit: Original algorithm was stupidly wrong, maybe this is better?

public static float minDistance(RectF rect, PointF point)
{
    if(rect.contains(point.x, point.y))
    {
        //North line 
        float distance = point.y - rect.top;

        //East line
        distance = Math.min(distance, point.x - rect.left);

        //South line
        distance = Math.min(distance, rect.bottom - point.y);

        //West line
        distance = Math.min(distance, rect.right - point.x);

        return distance;
    }
    else
    {
        float minX, minY;

        if (point.x < rect.left) 
        {
            minX = rect.left;
        } 
        else if (point.x > rect.right) 
        {
            minX = rect.right;
        } 
        else 
        {
            minX = point.x;
        }

        if (point.y < rect.top) 
        {
            minY = rect.top;
        } 
        else if (point.y > rect.bottom) 
        {
            minY = rect.bottom;
        } 
        else 
        {
            minY = point.y;
        }

        float vectorX = point.x - minX;
        float vectorY = point.y - minY;

        float distance = (float) Math.sqrt((vectorX * vectorX) + (vectorY * vectorY)); 

        return distance;
    }
}
  • 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-19T11:37:39+00:00Added an answer on May 19, 2026 at 11:37 am

    Just take the closest point and then get the distance to that.
    Off the top of my head:

        float closestX, closestY;
    
        if(point.x >= x1 && point.x <= x2 && point.y >= y1 && point.y <= y2)
        {
             float bestDistance = point.y - y1;
             bestDistance = Math.min(distance, y2 - point.y);
             bestDistance = Math.min(distance, point.x - x1);
             bestDistance = Math.min(distance, x2 - point.x);
    
             return bestDistance;
        }
    
        if (point.x < x1) {
            closestX = x1;
        } else if (point.x > x2) {
            closestX = x2;
        } else {
            closestX = point.x;
        }
    
        if (point.y < x1) {
            closestY = y1;
        } else if (point.y > y2) {
            closestY = y2;
        } else {
            closestY = point.y;
        }
    
        float vectorY = point.x - closestX;
        float vectorY = point.Y - closestY;
    
        float distance = sqrtf((vectorX * vectorX) + (vectorY * vectorY));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It seems like there should be something shorter than this: private string LoadFromFile(string path)
It seems like there should be a simpler way than: import string s =
Is there some easy way to pad Strings in Java? Seems like something that
It seems like there should be a removalAllOccuring(Collection) (or similiar) method in Multiset. A
It seems like there used to be way more binary protocols because of the
I been wondering about this for a while. It seems like there are so
cgi.escape seems like one possible choice. Does it work well? Is there something that
I know this probably seems like a little problem. It just feels like there
There was some code like this: // Convenience to make things more legible in
It seems like there is a lot of overhead involved in rapidly opening and

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.