<font size=2>!_NEWLINE_!<p>!_NEWLINE_!java programming quesition, involving the use of a class method!_NEWLINE_!</p></font>
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   ahmed_khan
Posted On:   Wednesday, October 17, 2001 01:47 PM

Suppose you have Rectangle Objects defined by the following fields class Rectangle { private int left; // x-coordinate of left edge of rectangle private int bottom; //y-coordinate of bottom edge of rectangle private int width; //width of rectangle private int height; //height of rectangle the question is to write a class method, intersection, that has two Rectangle parameters. The method should return the rectangle formed by the area common to the two rectangles. If they do not intersect, the method should return the rectangle whose instance fields are all zero. If the rectangles touch (ie if they have common points only on the   More>>


			
Suppose you have Rectangle Objects defined
by the following fields

class Rectangle
{
private int left; // x-coordinate of left edge of rectangle
private int bottom; //y-coordinate of bottom edge of rectangle
private int width; //width of rectangle
private int height; //height of rectangle

the question is to write a class method, intersection, that has two Rectangle parameters. The
method should return the rectangle formed by the area common to the two rectangles.
If they do not intersect, the method should return the rectangle whose instance fields are all zero.
If the rectangles touch (ie if they have common points only on their perimeters), the method
should return a "rectangle" of zero width or zero length.


I am a first year university student and
this question was posed by my professor. I can solve this problem
using about 30 if statements and 100 lines of code, however someone
told me that it can be done in about 5 lines

Any ideas would be greatly appreciated?

   <<Less

Re: <font size=2>!_NEWLINE_!<p>!_NEWLINE_!java programming quesition, involving the use of a class method!_NEWLINE_!</p></font>

Posted By:   joro_simeonov  
Posted On:   Thursday, January 17, 2002 08:01 AM

or you just use the method in java.awt.Rectangle

Rectangle intersection(Rectangle r);

/you may see the method source from the JDK source/

Re:java programming quesition, involving the use of a class method!_NEWLINE_!

Posted By:   Jeff_Hubbach  
Posted On:   Wednesday, October 17, 2001 04:21 PM

You could do the following (it isn't 5 lines, but it isn't 100 lines either):


public void Intersection(Rectangle rect1, Rectangle rect2)
{
if(rect1.left <= rect2.left && rect1.left + rect1.width >= rect2.left)
{
left = rect2.left;
}
else if(rect2.left <= rect1.left && rect2.left + rect2.width >= rect1.left)
{
left = rect1.left;
}
else
{
left = 0;
}
if(rect1.bottom >= rect2.bottom && rect1.bottom - rect1.height <= rect2.bottom)
{
bottom = rect2.bottom;
}
else if(rect2.bottom >= rect1.bottom && rect2.bottom - rect2.height <= rect1.bottom)
{
bottom = rect1.bottom;
}
else
{
bottom = 0;
}
if(left == bottom == 0)
{
width = height = 0;
}
else
{
width = Math.min(rect1.left + rect1.width, rect2.left + rect2.width) - left;
height = bottom - Math.max(rect1.bottom - rect1.height, rect2.bottom - rect2.height);
}
}
About | Sitemap | Contact