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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T17:29:27+00:00 2026-05-19T17:29:27+00:00

I need to find an algorithm which determines a relationship between a square and

  • 0

I need to find an algorithm which determines a relationship between a square and rectangle. It must be able to determine if:

  • The square is completely inside the rectangle
  • The square is partially inside (overlaps) the rectangle
  • Square’s corner only touches a rectangle’s corner
  • Square’s edge is on the rectangle’s edge

And here are the inputs (given values) that will help us to extract a mathematical formula for each case:

  • x coordinate of the center of the square = squareX
  • y coordinate of the center of the square = squareY
  • width of the square = squareW
  • x coordinate of the center of the rectangle = recX
  • y coordinate of the center of the rectangle = recY
  • width of the rectangle = recW
  • length of the rectangle = recL

P.S: Rectangle’s sizes are always bigger than the square’s width.

I will write the code in Java once we can extract an algorithm using mathematical operations.

Edit:

For the case of corners in touch, here is the code I wrote, and it works (Math.abs means the absolute value):

((Math.abs(Math.abs(recX-squareX)-(recW+squareW)/2))<=0.001) && ((Math.abs(Math.abs(recY-squareY)-(recL+squareW)/2))<=0.001)
  • 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-19T17:29:28+00:00Added an answer on May 19, 2026 at 5:29 pm

    updated for doubles

    double dx = Math.abs(rectX - squareX);
    double dy = Math.abs(rectY - squarey);
    double dw2 = (rectW + squareW) / 2;
    double dh2 = (rectL + squareW) / 2;
    
    if (Double.compare(dx, dw2) == 0 && Double.compare(dy, dh2) == 0)
        return CORNER_TOUCH;
    else if (Double.compare(dx, dw2) > 0 || Double.compare(dy, dh2) > 0)
        return OUTSIDE;
    else if (Double.compare(dx, dw2) == 0 || Double.compare(dy, dh2) == 0)
        return EDGE_TOUCH;
    else if (Double.compare(dx, rectW - dw2) <= 0 &&
            Double.compare(dy, rectL - dh2) <= 0)
        return INSIDE;
    else 
        return OVERLAPS;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need advice or directions on how to write an algorithm which will find
I need an algorithm (3D), which would determine if point belongs to a triangle.
I am aware that Dijkstra's algorithm can find the minimum distance between two nodes
There is an ArrayList which stores integer values. I need to find the maximum
I need an algorithm to find out all the possible positions of a group
which STL Algorithm should i use if I want to find something but Don't
I need to find out the number of digits which are not divisible by
I'm implementing an algorithm in which I need manipulate a mesh, adding and deleting
Using C, I need to find a substring inside a buffer that may contain
Given an integer array, i need to find which number occurred most number of

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.