Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: How to form a rectangle from the union of two Rectangles.

  1. #1
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Exclamation How to form a rectangle from the union of two Rectangles.

    I'm trying to make a method called intersect that makes a Rectangle, my Rectangle class, which has a lower left corner point, a width, and a height, Rectangle(double x, double y, double width, double height);

    I'm trying to figure out how to make a Rectangle that is is in common when the two intersect. i.e. where the Rectangle goes into another Rectangle, the Rectangle that forms. How do you get it's lower left point?

    Also, how do you set it up to sorta copy the code for the Java Rectangle class for the method union, where I would get the union, or the smallest Rectangle that'll hold both Rectangles?


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: How to form a rectangle from the union of two Rectangles.

    Check out the source code for Rectangle.java. You'll be able to see how the Sun folks accomplished many of the Rectangle methods, including union...most notably:

          /**
          * Computes the union of this <code>Rectangle</code> with the
          * specified <code>Rectangle</code>. Returns a new
          * <code>Rectangle</code> that
          * represents the union of the two rectangles
          * @param r the specified <code>Rectangle</code>
          * @return the smallest <code>Rectangle</code> containing both
          * the specified <code>Rectangle</code> and this
          * <code>Rectangle</code>.
          */
         public Rectangle union(Rectangle r) {
             int x1 = Math.min(x, r.x);
             int x2 = Math.max(x + width, r.x + r.width);
             int y1 = Math.min(y, r.y);
             int y2 = Math.max(y + height, r.y + r.height);
             return new Rectangle (x1, y1, x2 - x1, y2 - y1);
         }

  3. The Following User Says Thank You to copeg For This Useful Post:

    javapenguin (September 2nd, 2010)

  4. #3
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: How to form a rectangle from the union of two Rectangles.

    Homework?

    If not, just use Area#subtract

    db

  5. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: How to form a rectangle from the union of two Rectangles.

    Quote Originally Posted by copeg View Post
    Check out the source code for Rectangle.java. You'll be able to see how the Sun folks accomplished many of the Rectangle methods, including union...most notably:

          /**
          * Computes the union of this <code>Rectangle</code> with the
          * specified <code>Rectangle</code>. Returns a new
          * <code>Rectangle</code> that
          * represents the union of the two rectangles
          * @param r the specified <code>Rectangle</code>
          * @return the smallest <code>Rectangle</code> containing both
          * the specified <code>Rectangle</code> and this
          * <code>Rectangle</code>.
          */
         public Rectangle union(Rectangle r) {
             int x1 = Math.min(x, r.x);
             int x2 = Math.max(x + width, r.x + r.width);
             int y1 = Math.min(y, r.y);
             int y2 = Math.max(y + height, r.y + r.height);
             return new Rectangle (x1, y1, x2 - x1, y2 - y1);
         }
    How would it have to be altered for a Rectangle with the parameters (x, y, width, height) but (x,y) was the bottom-left, not the upper-right, corner?

  6. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How to form a rectangle from the union of two Rectangles.

    If you thought for just a second longer, you could come up with an equation to compute that.
    Have you had any algebra in school yet?

    You are given the x,y for a corner and the width and height. Right.
    Draw a diagram and see if you can come up with the equation you need.

  7. The Following User Says Thank You to Norm For This Useful Post:

    javapenguin (September 2nd, 2010)

  8. #6
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: How to form a rectangle from the union of two Rectangles.

    Quote Originally Posted by Norm View Post
    If you thought for just a second longer, you could come up with an equation to compute that.
    Have you had any algebra in school yet?

    You are given the x,y for a corner and the width and height. Right.
    Draw a diagram and see if you can come up with the equation you need.
    ]

    Compute what, the alteration to the Java Rectangle class source code, or a way to write the intersect and union methods without having to use the source code for the Java Rectangle class at all?

  9. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How to form a rectangle from the union of two Rectangles.

    Whatever you want. Given a corner of a rectangle and the width and height, you can compute the x,y of the other 3 corners.

  10. #8
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Smile Re: How to form a rectangle from the union of two Rectangles.

    public Rectangle intersect(Rectangle a)
    	{
    		if (((this.getBottomLeftPosition().getX() <= a.getBottomLeftPosition().getX()) && (a.getUpperRightPosition().getX() <= this.getUpperRightPosition().getX()))
    				&& ((this.getBottomLeftPosition().getY() <= a.getBottomLeftPosition().getY()) && (a.getUpperRightPosition().getY() <= this.getUpperRightPosition().getY())))
    				{
    				return(a);
    				}
     
    				else if (this.overlaps(a) == false)
    				{
    				return(null);
    				}
     
    		// return(new Rectangle(innerRectangle.getBottomLeftPosition().getX(), innerRectangle.getBottomLeftPosition().getY(), innerRectangle.getUpperRightPosition().getX() - innerRectangle.getBottomLeftPosition().getX(), innerRectangle.getUpperRightPosition().getY() - innerRectangle.getBottomLeftPostion().getY()));
    				else
     
     
    		{
    			double tx1 = this.getBottomLeftPosition().getX() + this.height;
    		    double ty1 = this.getBottomLeftPosition().getY() + this.height;
    		   double rx1 = a.getBottomLeftPosition().getX() + a.height;
    		    double ry1 = a.getBottomLeftPosition().getY() + a.height;
    		    double tx2 = tx1; tx2 += this.width;
    		    double ty2 = ty1; ty2 += this.height;
    		     double rx2 = rx1; rx2 += a.width;
    		     double ry2 = ry1; ry2 += a.height;
    		     if (tx1 < rx1) tx1 = rx1;
    		    if (ty1 < ry1) ty1 = ry1;
    		     if (tx2 > rx2) tx2 = rx2;
    		     if (ty2 > ry2) ty2 = ry2;
    		    tx2 -= tx1;
    		     ty2 -= ty1;
     
    		 if (tx2 < Integer.MIN_VALUE) tx2 = Integer.MIN_VALUE;
    		    if (ty2 < Integer.MIN_VALUE) ty2 = Integer.MIN_VALUE;
    		    return (new Rectangle (tx1, ty1, (int) tx2, (int) ty2));
    		    }
    		}
    x, y is now the bottom-left coordinate instead of the top-left. How do I change the

     double tx2 = tx1; tx2 += this.width;
    		    double ty2 = ty1; ty2 += this.height;
    		     double rx2 = rx1; rx2 += a.width;
    		     double ry2 = ry1; ry2 += a.height;
    		     if (tx1 < rx1) tx1 = rx1;
    		    if (ty1 < ry1) ty1 = ry1;
    		     if (tx2 > rx2) tx2 = rx2;
    		     if (ty2 > ry2) ty2 = ry2;
    		    tx2 -= tx1;
    		     ty2 -= ty1;

    to reflect that?

    They want:

    Enter no. of rectangles:
    Enter x of rectangle 1:Enter y of rectangle 1:Enter width of rectangle 1:Enter height of rectangle 1:
    Enter x of rectangle 2:Enter y of rectangle 2:Enter width of rectangle 2:Enter height of rectangle 2:
    Enter x of rectangle 3:Enter y of rectangle 3:Enter width of rectangle 3:Enter height of rectangle 3:
    Enter x of rectangle 4:Enter y of rectangle 4:Enter width of rectangle 4:Enter height of rectangle 4:
    Enter x of rectangle 5:Enter y of rectangle 5:Enter width of rectangle 5:Enter height of rectangle 5:
    Enter x of rectangle 6:Enter y of rectangle 6:Enter width of rectangle 6:Enter height of rectangle 6:Rectangle 1=-4, y=-3, width=6, height=4
    Rectangle 2=0, y=0, width=5, height=3
    Rectangle 3=8, y=2, width=5, height=5
    Rectangle 4=1, y=-5, width=8, height=4
    Rectangle 5=-2, y=5, width=3, height=3
    Rectangle 6=0, y=6, width=10, height=3
    Area of rectangle 1:24
    Area of rectangle 2:15
    Area of rectangle 3:25
    Area of rectangle 4:32
    Area of rectangle 5:9
    Area of rectangle 6:30
    Rectangles 0 and 1 overlap.
    Intersection rectangle is x=0, y=0, width=2, height=1
    Rectangles 0 and 3 overlap.
    Intersection rectangle is x=1, y=-3, width=1, height=2
    Rectangles 2 and 5 overlap.
    Intersection rectangle is x=8, y=6, width=2, height=1
    Rectangles 4 and 5 overlap.
    Intersection rectangle is x=0, y=6, width=1, height=2
    No. of overlaps: 4
    The Bounding box is: x=-4, y=-5, width=17, height=14

    oh, how do you get all the Rectangles, for n Rectangles, to perform the union method on each other like this:

    Rectangle superRect = Rectangle1.union(Rectangle2.union(Rectangle3.union(Rectangle4.union(Rectangle5.union(etc)))))
    ?

    That'll get the bounding box.

    I'm getting for output with my method intersect and all the rest:

    Enter the number of rectangles.
    6
    Enter the x-coordinate for Rectangle 1
    -4 -3
    Enter the y-coordinate for the Rectangle 1
    Enter the width for the Rectangle 1
    6 4
    Enter the height for the Rectangle 1
    Rectangle 1: x:-4.0, y:-3.0 width is: 6.0; height is: 4.0
    Enter the x-coordinate for Rectangle 2
    0 0 5 3
    Enter the y-coordinate for the Rectangle 2
    Enter the width for the Rectangle 2
    Enter the height for the Rectangle 2
    Rectangle 2: x:0.0, y:0.0 width is: 5.0; height is: 3.0
    Enter the x-coordinate for Rectangle 3
    8 2 5 5
    Enter the y-coordinate for the Rectangle 3
    Enter the width for the Rectangle 3
    Enter the height for the Rectangle 3
    Rectangle 3: x:8.0, y:2.0 width is: 5.0; height is: 5.0
    Enter the x-coordinate for Rectangle 4
    1 -5 8 4
    Enter the y-coordinate for the Rectangle 4
    Enter the width for the Rectangle 4
    Enter the height for the Rectangle 4
    Rectangle 4: x:1.0, y:-5.0 width is: 8.0; height is: 4.0
    Enter the x-coordinate for Rectangle 5
    -2 5 3 3
    Enter the y-coordinate for the Rectangle 5
    Enter the width for the Rectangle 5
    Enter the height for the Rectangle 5
    Rectangle 5: x:-2.0, y:5.0 width is: 3.0; height is: 3.0
    Enter the x-coordinate for Rectangle 6
    0 6 10 3
    Enter the y-coordinate for the Rectangle 6
    Enter the width for the Rectangle 6
    Enter the height for the Rectangle 6
    Rectangle 6: x:0.0, y:6.0 width is: 10.0; height is: 3.0
    The area of Rectangle 1 is 24.0.
    The area of Rectangle 2 is 15.0.
    The area of Rectangle 3 is 25.0.
    The area of Rectangle 4 is 32.0.
    The area of Rectangle 5 is 9.0.
    The area of Rectangle 6 is 30.0.
    Rectangle 0 and Rectangle 1 overlap.
    x:3.0, y:3.0 width is: 3.0; height is: 2.0
    Rectangle 0 and Rectangle 3 overlap.
    x:5.0, y:1.0 width is: 1.0; height is: 2.0
    Rectangle 2 and Rectangle 5 overlap.
    x:13.0, y:9.0 width is: 0.0; height is: 3.0
    Rectangle 4 and Rectangle 5 overlap.
    x:3.0, y:9.0 width is: 1.0; height is: 2.0
    The number of overlaps is: 4

    Basically just my intersection rectangles are varying, though some parts match up.

    ^:

    Somehow it turned x's into smilies at one point.
    Last edited by javapenguin; September 2nd, 2010 at 09:09 PM. Reason: What the...?!

  11. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How to form a rectangle from the union of two Rectangles.

    Your code is missing comments describing the logic of what/why it is doing at each step.

    Same for your printed output. Does it show problems?
    Where are your added comments saying: At this line it output x but it should be y.

Similar Threads

  1. Fill in rectangle partially (clip?¿)
    By OBLITERATOR in forum AWT / Java Swing
    Replies: 5
    Last Post: March 27th, 2010, 01:38 PM
  2. Java Swing :Back Button from one form to another form
    By srinivasan_253642 in forum AWT / Java Swing
    Replies: 1
    Last Post: December 26th, 2009, 09:51 AM
  3. help: union of two array,.. kinda problem
    By sanfor in forum Collections and Generics
    Replies: 2
    Last Post: November 29th, 2009, 02:33 PM
  4. centering a label inside a rectangle
    By Brain_Child in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 19th, 2009, 09:08 AM
  5. Dropping to graphic element dragged from JList
    By tua1 in forum AWT / Java Swing
    Replies: 1
    Last Post: November 29th, 2008, 08:22 AM