# writing own .contains method for rectangles

Page 1 of 1

## 10 Replies - 12802 Views - Last Post: 14 October 2009 - 04:39 PMRate Topic:     //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=131829&amp;s=f89d6784a467df48413acf0240f18fba&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 winterwind Reputation: 0
• Posts: 17
• Joined: 22-September 09

# writing own .contains method for rectangles

Posted 13 October 2009 - 02:29 PM

Hi,

I am having trouble with this problem:

You might notice the contains method that comes with the java.awt.Rectangle class weve been using returns false if the point is on the lower or right edge of the Rectangle (for example the program will refuse to deliver to a customer at (1,8) or (4,2) even though these points are in store 1s range. Instead of using the Rectangles contains method, write your own method that takes a Rectangle, a x value, and a y value, and returns true (and false otherwise) if the point (x,y) is located inside or on ANY edge of the Rectangle parameter. Use your new method in the program wherever you used the Rectangles contains method before.

So basically I have to write a method that will return true or false when I input a rectangle, and a point.

I know that Rectangle store will have a starting point (upper left corner), and a certain width and height. So the input of x and y will have to fall inside of these constraints of this rectangle.

In general, if (a, b ) represents the upper left corner of the rectangle, respectively, and (c,d) represents the height and width of the rectangle, respectively, point (x,y) lies in (or on the border) of the rectangle if and only if:

a <= x <= a + c

AND

b <= y <= b + d

(Please correct me on the above logic if I am indeed incorrect. That is just my approximation.)

I am having trouble coding this. I'm not sure how to extract the (a,b ) [upper left corner coordinates] and (c, d) [height and width] from the input rectangle. Would this even be the right approach?

Any assistance would be much appreciated!

This post has been edited by winterwind: 13 October 2009 - 02:33 PM

Is This A Good Question/Topic? 0

## Replies To: writing own .contains method for rectangles

### #2 theautokustomizer Reputation: 16
• Posts: 250
• Joined: 20-September 09

## Re: writing own .contains method for rectangles

Posted 13 October 2009 - 02:42 PM

We have a policy here at D.I.C., we need to see what you have to this point in code. Thank you.

### #3 winterwind Reputation: 0
• Posts: 17
• Joined: 22-September 09

## Re: writing own .contains method for rectangles

Posted 13 October 2009 - 03:30 PM

This is the code for what I basically explained above:

```
public static boolean newContains(Rectangle(a, b, c, d), int x, int y)
{
if (a <= x <= (a + c) && b <= y <= (b + d))
{
return true;
}
else
{
return false;
}
}
```

Am I heading in the right direction? Is this how I would write a method for use with the rectangle object?

This post has been edited by winterwind: 13 October 2009 - 03:33 PM

### #4 pbl Reputation: 8381
• Posts: 31,956
• Joined: 06-March 08

## Re: writing own .contains method for rectangles

Posted 13 October 2009 - 06:39 PM

You cannot pass a parameter like that

public static boolean newContains(Rectangle(a, b, c, d), int x, int y);

Assuming that you have a class like that

```class Rectangle {
int a, b, c, d;

// constructor
Rectangle(int x, int y, int width, int height) {
this.x = x;
this.y = y;
c = width;
d = height;
}

// then your Rectangle can have a contain method that uses your logic
public boolean newContains(int x, int y)
{
return (a <= x <= (a + c) && b <= y <= (b + d))
}
}

```

### #5 winterwind Reputation: 0
• Posts: 17
• Joined: 22-September 09

## Re: writing own .contains method for rectangles

Posted 13 October 2009 - 08:25 PM pbl, on 13 Oct, 2009 - 05:39 PM, said:

You cannot pass a parameter like that

public static boolean newContains(Rectangle(a, b, c, d), int x, int y);

Assuming that you have a class like that

```class Rectangle {
int a, b, c, d;

// constructor
Rectangle(int x, int y, int width, int height) {
this.x = x;
this.y = y;
c = width;
d = height;
}

// then your Rectangle can have a contain method that uses your logic
public boolean newContains(int x, int y)
{
return (a <= x <= (a + c) && b <= y <= (b + d))
}
}

```

It says that it can't find the variable x when I try to compile this...?

### #6 pbl Reputation: 8381
• Posts: 31,956
• Joined: 06-March 08

## Re: writing own .contains method for rectangles

Posted 13 October 2009 - 08:30 PM

Sorry... I went to fast

```   // constructor
Rectangle(int x, int y, int width, int height) {
a = x;
b = y;
c = width;
d = height;
}

```

### #7 winterwind Reputation: 0
• Posts: 17
• Joined: 22-September 09

## Re: writing own .contains method for rectangles

Posted 13 October 2009 - 09:18 PM pbl, on 13 Oct, 2009 - 07:30 PM, said:

Sorry... I went to fast

```   // constructor
Rectangle(int x, int y, int width, int height) {
a = x;
b = y;
c = width;
d = height;
}

```

Thanks. It still doesn't compile correctly though. The compiler faults this line:

return (a <= x <= (a + c) && b <= y <= (b + d));

This error message is returned with the above line highlighted:

operator <= cannot be applied to boolean,int

Is <= (or >= or > or <, etc., etc.) not allowed in booleans??

### #8 pbl Reputation: 8381
• Posts: 31,956
• Joined: 06-March 08

## Re: writing own .contains method for rectangles

Posted 13 October 2009 - 09:26 PM

Right... you cannot 2 test 2 conditions in Java using

x < y < 10

you have to separate the test and use the and && operator

This should do the trick

return ((a <= x && x <= (a + c)) && (b <= y && y <= (b + d)));

### #9 winterwind Reputation: 0
• Posts: 17
• Joined: 22-September 09

## Re: writing own .contains method for rectangles

Posted 13 October 2009 - 09:55 PM

Thanks! Works like a charm. ### #10 alunduil Reputation: 0
• Posts: 1
• Joined: 13-October 09

## Re: writing own .contains method for rectangles

Posted 14 October 2009 - 12:19 PM winterwind, on 13 Oct, 2009 - 02:30 PM, said:

```
public static boolean newContains(Rectangle(a, b, c, d), int x, int y)
{
if (a <= x <= (a + c) && b <= y <= (b + d))
{
return true;
}
else
{
return false;
}
}
```

```static class NewRectangle extends Rectangle {
public static boolean NewContains(int x, int y) {
return (this.x <= x <= (this.width + this.x) && this.y <= y <= (this.height + this.y));
}
}

```

Sorry this response took so long. My computer has been having some difficulties.

What this does is inherits from the default Rectangle class so we have all of the functionality of it and then simply create our own function for checking if a point (specified with <x, y>) is in the rectangle. Any questions post back.

Regards,

Alunduil

This post has been edited by alunduil: 14 October 2009 - 12:20 PM

### #11 pbl Reputation: 8381
• Posts: 31,956
• Joined: 06-March 08

## Re: writing own .contains method for rectangles

Posted 14 October 2009 - 04:39 PM alunduil, on 14 Oct, 2009 - 11:19 AM, said: winterwind, on 13 Oct, 2009 - 02:30 PM, said:

```static class NewRectangle extends Rectangle {
public static boolean NewContains(int x, int y) {
return (this.x <= x <= (this.width + this.x) && this.y <= y <= (this.height + this.y));
}
}

```

Sorry this response took so long. My computer has been having some difficulties.

What this does is inherits from the default Rectangle class so we have all of the functionality of it and then simply create our own function for checking if a point (specified with <x, y>) is in the rectangle. Any questions post back.

Regards,

Alunduil

Still wrong.. you need the && and there is no this.x Actually, in a problem like that,I prefer to eliminate the wrong candidates instead of putting a lot of &&

```boolean contains(int x, int y) {
if(x < a)
return false;
if(x > a + c)
return false;
if(y < b)
return false;
if(y > b + d)
return false;
// ok I am in
return true;
}

```

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }