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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:56:13+00:00 2026-05-27T09:56:13+00:00

I need to write condition in while(…) where all three shapes will overlap and

  • 0

I need to write condition in while(...) where all three shapes will overlap and then use in while loop try to find combination of coordinates for shape which will not overlap other shapes. I have 3 while loops in code – each one for choose pair of coordinates for specific shape.

This code freezes program:

                    xRing = (int) ((getWidth() - ringSize) * (Math.random()));
                yRing = (int) ((getHeight() - ringSize) * (Math.random()));
                while( !( 

                           (xSquare + squareSize) < (xRing)
                        || (xSquare) > (xRing + ringSize )
                        || (ySquare + squareSize) < (yRing)
                        || (ySquare) > (yRing + ringSize)
                        )

                        ||

                        !( 

                           (xSquare2 + square2Size) < (xRing)
                        || (xSquare2) > (xRing + ringSize )
                        || (ySquare2 + square2Size) < (yRing)
                        || (ySquare2) > (yRing + ringSize)
                        )

                        ||

                        !( 

                           (xSquare + squareSize) < (xSquare2)
                            || (xSquare) > (ySquare2 + square2Size )
                            || (ySquare + squareSize) < (ySquare2)
                            || (ySquare) > (ySquare2 + square2Size)
                        )

                    ){
                    xRing = (int) ((getWidth() - ringSize) * (Math.random()));
                    yRing = (int) ((getHeight() - ringSize) * (Math.random()));


                }

and this works, but allows to overlap Square and Square2:

        while( !( 

                           (xSquare + squareSize) < (xRing)
                        || (xSquare) > (xRing + ringSize )
                        || (ySquare + squareSize) < (yRing)
                        || (ySquare) > (yRing + ringSize)
                        )

                        ||

                        !( 

                           (xSquare2 + square2Size) < (xRing)
                        || (xSquare2) > (xRing + ringSize )
                        || (ySquare2 + square2Size) < (yRing)
                        || (ySquare2) > (yRing + ringSize)
                        )

As far as I understand, I have to check each pair for overlap, there is 3 shapes, 3 possible overlap. As I mentioned before with my code logic I can check for overlapping of 2 pairs. When I am adding third condition check- it fails to exit while. All logic seems to me perfectly OK. My loop will exit if it finds first coordinate where no overlap of three shapes is possible.

Actually my short version of code should look like this:

while( not(ring NOT overlaps square 1) or 
       not(ring NOT overlaps square 2) or 
       not(square 1 NOT overlaps square 2) ) {
  ...
}

SOLVED:
I should check for 2 combination (NOT 3) while drawing each shape, so for each while() I have to have different conditions.

  • 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-27T09:56:14+00:00Added an answer on May 27, 2026 at 9:56 am

    You’re basically checking random positions in your rectangle and if all of them are contained by at least two shapes (assuming your condition is correct, I didn’t check that) your loop won’t exit.

    It would be better to loop through all the pixels in your rectangle and check them. This way, your loop will stop after all pixels are checked.

    As for your condition: I didn’t thoroughly check it but since you’re using a lot of or conditions I assume at least one of the outer ones is true and thus the loop won’t stop.

    I assume your 3 top level conditions should read as follows:

    while( not(ring overlaps square 1) or 
           not(ring overlaps square 2) or 
           not(square 1 overlaps square 2) ) {
      ...
    }
    

    If, for example, square 1 and 2 don’t overlap the condition will always be true and the loop will run forever.

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

Sidebar

Related Questions

HI, I need to write multiple statements inside a conditional operator. What will be
I need to write a program used internally where different users will have different
I develop against Oracle databases. When I need to manually write (not use an
I need to write a report that on startup shows specific groups expanded while
I need write an update statement that used multiple tables to determine which rows
I need to write a Java Comparator class that compares Strings, however with one
I need to write a Delphi application that pulls entries up from various tables
I need to write a web application using SQL Server 2005, asp.net, and ado.net.
I need to write a java script. This is supposed to validate if the
I need to write code that picks up PGP-encrypted files from an FTP location

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.