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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:05:29+00:00 2026-05-20T08:05:29+00:00

If it was the dist to a point it would be dist(mouseX, mouseY, x,

  • 0

If it was the dist to a point it would be

dist(mouseX, mouseY, x, y)

for

point(x,y)

but how can I calculate dist() from the mouse’s current position to

rectMode(CORNERS);
rect(x1,y2,x2,y2);

Thanks

  • 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-20T08:05:29+00:00Added an answer on May 20, 2026 at 8:05 am

    Something like this should do it:

    float distrect(float x, float y, float x1, float y1, float x2, float y2){
      float dx1 = x - x1;
      float dx2 = x - x2;
      float dy1 = y - y1;
      float dy2 = y - y2;
    
      if (dx1*dx2 < 0) { // x is between x1 and x2
        if (dy1*dy2 < 0) { // (x,y) is inside the rectangle
          return min(min(abs(dx1), abs(dx2)),min(abs(dy1),abs(dy2)));
        }
        return min(abs(dy1),abs(dy2));
      }
      if (dy1*dy2 < 0) { // y is between y1 and y2
        // we don't have to test for being inside the rectangle, it's already tested.
        return min(abs(dx1),abs(dx2));
      }
      return min(min(dist(x,y,x1,y1),dist(x,y,x2,y2)),min(dist(x,y,x1,y2),dist(x,y,x2,y1)));
    }
    

    Basically, you need to figure out if the closes point is on one of the sides, or in the corner. This picture may help, it shows the distance of a point from a rectangle for different positions of the point:
    enter image description here

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

Sidebar

Related Questions

I'm trying to calculate new velocities for 2 colliding balls, but can't really do
I have a class Point and a class Rect . I want to have
Looking in my /usr/local/lib/python.../dist-package directory, I have .egg directories and .egg files. Why does
So I've got a nice Java project, build with Ant to a /dist folder.
I'm doing the following code that construct a distance matrix between each point and
I am trying to implement a simpler version of this algorithm but which works
I'm implementing a distance matrix that calculates the distance between each point and all
Simple problem, how to find the first non-zero digit after decimal point. What I
I have an Ant script that I would like to execute using a button
I have an array like the following: people = [{'node': 'john', 'dist': 3}, {'node':

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.