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?

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:

Code java:

/**
* 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);
}

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

Homework?

If not, just use Area#subtract

db

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

Quote:

Originally Posted by

**copeg**
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:

Code java:

/**
* 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?

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.

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

Quote:

Originally Posted by

**Norm**
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?

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.

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

Code :

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

Code :

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:x=-4, y=-3, width=6, height=4

Rectangle 2:x=0, y=0, width=5, height=3

Rectangle 3:x=8, y=2, width=5, height=5

Rectangle 4:x=1, y=-5, width=8, height=4

Rectangle 5:x=-2, y=5, width=3, height=3

Rectangle 6:x=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:

Code :

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.

>:P>:P>:P^:)^^:)^^:)^^:)^^:)^^:)^^:

Somehow it turned x's into smilies at one point.

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.