Question

Hello everyone I need to do a code that check if two rectangles are overlapping in...

Hello everyone

I need to do a code that check if two rectangles are overlapping in Java.

So I have Rectangle 1 = x,y,width, depth.

And Rectangle 2 = x,y,width,depth

So how do I calculate that?

Thank you,

0 0
Add a comment Improve this question Transcribed image text
Answer #1

class Point {

   int x, y;
}

class OverLap {
   static boolean isOverlaping(Point l1, Point rect1, Point l2, Point rect2) {
       // If one rectangle is on left side of other
       if (l1.x > rect2.x || l2.x > rect1.x) {
           return false;
       }
       // If one rectangle is above other
       if (l1.y < rect2.y || l2.y < rect1.y) {
           return false;
       }
       return true;
   }

   public static void main(String[] args) {
       Point l1 = new Point();
       Point r1 = new Point();
       Point l2 = new Point();
       Point r2 = new Point();
       l1.x = 0;
       l1.y = 10;
       r1.x = 10;
       r1.y = 0;
       l2.x = 5;
       l2.y = 5;
       r2.x = 15;
       r2.y = 0;

       if (isOverlaping(l1, r1, l2, r2)) {
           System.out.println("Rectangles Overlap with each other");
       } else {
           System.out.println("Rectangles Don't Overlap with each other");
       }
   }
}

Add a comment
Know the answer?
Add Answer to:
Hello everyone I need to do a code that check if two rectangles are overlapping in...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hello everyone I have a question for circuit II course and I need a good expert...

    Hello everyone I have a question for circuit II course and I need a good expert who can helps me to solve that problem. Thank you so much Cs 2. 3 2 IS

  • Hello everyone! I need help on these few questions from my elementary calculus class, I am...

    Hello everyone! I need help on these few questions from my elementary calculus class, I am very confused, It is question 1a,b,c,d,e,f,g I need help on. Thank you so much :) - Anna 1. Find the derivative of the function. 5x7 - 773 + + 4x + 10 (a) f(x) = (b) f(x) = (x+3)(x2 + 2x - 4) (c) g(x) = (3x2 + 4x)" (d) y=2tV2 + 4 2002 - 2 (e) h(x) 1-23 (f) y = (3e' -...

  • Hello I have a question. I would like to check if the code follows this requirement....

    Hello I have a question. I would like to check if the code follows this requirement. Rectangle.h - A complete Rectangle Class including both declaration and definition appRectangle,cpp separate in two different files the class definition and implementation Code: rectangle.h // Rectangle class declaration. class Rectangle { private: double width; double length; public: void setWidth(double); void setLength(double); double getWidth() const; double getLength() const; double getArea() const; }; //************************************************** // setWidth assigns a value to the width member. * //************************************************** void...

  • Hello, I need the correct answer for this please , a quick sort program -java code-...

    Hello, I need the correct answer for this please , a quick sort program -java code- that have 2 arrays first one will have different values , the second one the values will be from smallest to largest number then the program have a quick sort to do it for both of them and will count the time - how long the quick sort is take for each one - which array is faster. Thank you ❤️

  • I have to create a java graphics program which will draw 10 rectangles and 10 ellipses...

    I have to create a java graphics program which will draw 10 rectangles and 10 ellipses on the screen. These shapes are to be stored in an arrayList. I have to do this using 6 different class files. I am not sure whether I successfully created an ellipse in the Oval Class & also need help completing the print Class. I need someone to check whether my Oval Class is correct (it successfully creates an ellipse with x, y, width,...

  • Hello,, I need Code java to : Loading and Saving of Images with Java 2D (Interface...

    Hello,, I need Code java to : Loading and Saving of Images with Java 2D (Interface and buttons to upload and to save image) Thank you, Regareds,

  • please do in java and comments the code so i can understand Requirements: Create a Java...

    please do in java and comments the code so i can understand Requirements: Create a Java class named “MyRectangle2D.java”. Your class will have double two variables named x and y. These will represent the center point of your rectangle. Your class will have two double variables named width and height. These will represent the width and height of your rectangle. Create getter and setter methods for x, y, width, and height. Create a “no argument” constructor for your class that...

  • Hello, I need help answering these two questions. Thank you! 1. How do I make 5...

    Hello, I need help answering these two questions. Thank you! 1. How do I make 5 mL of a 1/10 dilution of a solution? 2. How do I figure out the concentration of an undiluted solution(I have the absorbance but the reading is off) if I am unable to use a standard curve?

  • 1. In IntelliJ create a new project called F1_2 2. In the Project window create a...

    1. In IntelliJ create a new project called F1_2 2. In the Project window create a new Java package called F1_2. This can be done by right clicking on src and going to New ! Package. 3. In package lab1 2 create a class called Rectangle. This can be done by right clicking on the package and going to New ! Java Class 4. At the beginning of Rectangle.java add the line package lab1 2; 5. In Rectangle.java create a...

  • In Java programming language. For this assignment, you must write a class Rectangle and a tester...

    In Java programming language. For this assignment, you must write a class Rectangle and a tester RectangleTest. The Rectangle class should have only the following public methods (you can add other non-public methods): Write a constructor that creates a rectangle using the x, y coordinates of its lower left corner, its width and its height in that order. Creating a rectangle with non-positive width or height should not be allowed, although x and y are allowed to be negative. Write...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT